extern "C" error #2040: expected an identifier

jamel picture jamel · Apr 24, 2013 · Viewed 18.2k times · Source

I still struggling to compile a C console application, the compiling procedure still failing with the error below:

"Main.c", line 51: error #2040: expected an identifier
  extern "C" void TreatReceivedSignal( int NoSignal ) ;
         ^
1 error detected in the compilation of "Main.c".
gmake: *** [Main.o] Error 2

below the declaration of the extern method on the C code :

extern "C" void TreatReceivedSignal( int NoSignal ) ;

I am using HP-UX aCC compiler [HP C/aC++ B3910B A.06.26], also I switched on the compilation flag -Ae to enable C99 support. Seems that the compiler cannot recognize the 'extern "C"' as C reserved word, may some other compilation flag need to be set. Any idea please that can solve this kind of issue? Thank you very much in advance. Regards

Answer

Some programmer dude picture Some programmer dude · Apr 24, 2013

The extern "C" construct is a C++ specific thing, it can't be used in C. And the compiler treats your source file as a C source file since it has the extension .c.

The most common thing to do is to use the preprocessor to conditionally add this for C++ compilations:

#ifdef __cplusplus
extern "C" {
#endif

/* Standard C prototypes */

#ifdef __cplusplus
}
#endif