extern "C" error #2040: expected an identifier
Asked Answered
B

1

14

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

Byrom answered 24/4, 2013 at 13:8 Comment(1)
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.Howzell
A
35

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
Antiquary answered 24/4, 2013 at 13:11 Comment(8)
as I know it C and C++method can be used on the both language using the linkage mechanismByrom
@Byrom 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.Antiquary
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!Byrom
@Byrom The C compiler is complaining because you are using a construct not in the C language. It's as simple as that.Antiquary
:) I'll try to add the #ifdef __cplusplus and i'll c what happen thanks manByrom
@ 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 :)Byrom
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 :)Byrom
@Byrom 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.Antiquary

© 2022 - 2024 — McMap. All rights reserved.