c++cgcc

How to ensure my code is ansi C, no C++ involved. Could compiler like gcc can be set to prompt error when meeting feature in C++?


I programmed C++ for a while. Now I want to program a ANSI C program, don't want any "C++ only" feature in the code. I'm using cygwin 64 bit with gcc installed. Are there any settings to let gcc prompt compiler error if encounter C++ features? E.g. the stl.


Solution

  • The following is taken from the gcc documentation:

    The original ANSI C standard (X3.159-1989) was ratified in 1989 and published in 1990. This standard was ratified as an ISO standard (ISO/IEC 9899:1990) later in 1990. There were no technical differences between these publications, although the sections of the ANSI standard were renumbered and became clauses in the ISO standard. This standard, in both its forms, is commonly known as C89, or occasionally as C90, from the dates of ratification. The ANSI standard, but not the ISO standard, also came with a Rationale document. To select this standard in GCC, use one of the options -ansi, -std=c90 or -std=iso9899:1990; to obtain all the diagnostics required by the standard, you should also specify -pedantic (or -pedantic-errors if you want them to be errors rather than warnings).

    So, compile using the following flags:

    gcc myfile.c -ansi -pedantic-errors
    

    Notice that -ansi and -std=c90 are synonyms. For the full list of options, refer to Options Controlling C Dialect.