13

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

j0k
  • 22,600
  • 28
  • 79
  • 90
jamel
  • 303
  • 2
  • 7
  • 16
  • C and C++ aren't *similar*. Don't treat them as though they are. Learn one, or learn the other. Don't learn them simultaneously. Don't write C code for the purpose of being "compilable as C++". There are many reasons why that's a bad idea. Write in C, or write in C++. If you want to link C code to a C++ project, compile the C code with a C compiler and use your C++ linker to link the object code. – autistic Apr 24 '13 at 15:50

1 Answers1

34

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
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • as I know it C and C++method can be used on the both language using the linkage mechanism – jamel Apr 24 '13 at 13:14
  • @jamel You can use the function from both C and C++, it's the `extern "C"` thing the compiler is complaining about. That construct is not in the C language. – Some programmer dude Apr 24 '13 at 13:15
  • yeah I dunno why it is compalining, however I compiled the same application with an older version of aCC! this version maybe need some additional flag to be set during the compilation! – jamel Apr 24 '13 at 13:16
  • @jamel The C compiler is complaining because you are using a construct not in the C language. It's as simple as that. – Some programmer dude Apr 24 '13 at 13:19
  • :) I'll try to add the #ifdef __cplusplus and i'll c what happen thanks man – jamel Apr 24 '13 at 13:21
  • @ Joachim Pilebirg it is an old application compiled many times before now we just upgraded our C compiler and we have this issue, I was just trying to avoid code modification :) – jamel Apr 24 '13 at 13:22
  • well I added the "#ifdef __cplusplus" the error#2040 disappeared but some precompiler error occurs PCC-I-02106, Userid only used when SQLCHECK = FULL, userid ignored. Semantic error at line 252, column 9, file ToolBoxDb.pcpp: EXECUTE ........1 PCC-S-02345, SQLCHECK=SEMANTICS must be given when embedded PL/SQL blocks are used Hope it is not related to the last change :) – jamel Apr 24 '13 at 13:27
  • @jamel I know this is very old by now and that you probably solved the last problem, but I give a comment for future readers: It have nothing to do with C or C++, and it's all about the embedded SQL and the pre-processor system for handling that. It's a totally separate issue, and as such should be asked in a new question. – Some programmer dude Jun 24 '19 at 18:05