Hello,

I have a C file and .h file which i can easily compile on a 64 bit linux platform.( Through eclipse IDE. In the compiler option, i enable -std=c99.

Now, with the same file, i want to compile it on c++ compiler.

So i add extern c to the .h files and rename the .c file to c++.
I used g++ compiler when i got the error " enable c99 compiler option.

I tried all the options given in the gcc manual but without success.

I tried all of the below

-std=
Determine the language standard. This option is currently only supported when compiling C or C++. A value for this option must be provided; possible values are
`c89'
`iso9899:1990'
ISO C90 (same as -ansi).
`iso9899:199409'
ISO C90 as modified in amendment 1.
`c99'
`c9x'
`iso9899:1999'
`iso9899:199x'
ISO C99. Note that this standard is not yet fully supported; see http://gcc.gnu.org/gcc-4.0/c99status.html for more information. The names `c9x' and `iso9899:199x' are deprecated.
`gnu89'
Default, ISO C90 plus GNU extensions (including some C99 features).
`gnu99'
`gnu9x'
ISO C99 plus GNU extensions. When ISO C99 is fully implemented in GCC, this will become the default. The name `gnu9x' is deprecated.
`c++98'
The 1998 ISO C++ standard plus amendments.
`gnu++98'


Am i missing something. ?

thanks.

If you call g++ with -std=c99 or any type of C standard specification, you must be compiling C code. It doesn't matter that the compiler is called "g++", if you tell it to compile C code, it will expect C code and only accept that.

The problem here is that if you added the extern "C" to your header file, but not to your .cpp file, then your code is neither in C++ nor in C, so neither a C compiler or a C++ compiler will accept it. The extern "C" is specific to C++ and tells to compiler to apply C linking rules (including no name-mangling) to functions contained in the curly-braces following it or the function directly following it. This specification has to appear in both the header file and the cpp file, and, it is not valid in C. So, either you put the extern "C" in both files and compile with a C++ compiler, or you don't use extern "C" anywhere and compile with a C compiler (or, equivalently, with g++ -std=c99 ). Also note that extern "C" only affects linking, so it doesn't tell the C++ compiler to compile that chunk of code as C code, it only instructs it to create a C-compatible external interface (for linking purposes) for that code. So, you need to code to be valid C++ code, which may not be the case since C++ is not entirely backward compatible with C, but generally it will be the case. So, this also means, that you should be able to compile the C code using the C++ compiler without the extern "C" appearing anywhere, but in that case, linking will not be compatible with C.

If you have further errors, you should post them. Error messages may seem incomprehensible to you, but they can be very helpful to us when trying to help you.

Thanks for the reply mike.

Actually, the error was due to a check in an other file.( checking the std args..)I commented it out and got the code to compile and link properly.

It also runs correctly.

Thanks!.

If you call g++ with -std=c99 or any type of C standard specification, you must be compiling C code. It doesn't matter that the compiler is called "g++", if you tell it to compile C code, it will expect C code and only accept that.

The problem here is that if you added the extern "C" to your header file, but not to your .cpp file, then your code is neither in C++ nor in C, so neither a C compiler or a C++ compiler will accept it. The extern "C" is specific to C++ and tells to compiler to apply C linking rules (including no name-mangling) to functions contained in the curly-braces following it or the function directly following it. This specification has to appear in both the header file and the cpp file, and, it is not valid in C. So, either you put the extern "C" in both files and compile with a C++ compiler, or you don't use extern "C" anywhere and compile with a C compiler (or, equivalently, with g++ -std=c99 ). Also note that extern "C" only affects linking, so it doesn't tell the C++ compiler to compile that chunk of code as C code, it only instructs it to create a C-compatible external interface (for linking purposes) for that code. So, you need to code to be valid C++ code, which may not be the case since C++ is not entirely backward compatible with C, but generally it will be the case. So, this also means, that you should be able to compile the C code using the C++ compiler without the extern "C" appearing anywhere, but in that case, linking will not be compatible with C.

If you have further errors, you should post them. Error messages may seem incomprehensible to you, but they can be very helpful to us when trying to help you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.