Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C referencing C++ extern

I have a header file in a Windows compile using the Intel compiler. The header looks something like this:

#ifdef _MAIN
    Loggerp        logger;
#else
    extern Loggerp logger;
#endif

The _MAIN macro is defined in a C++ file and there is a C file that includes the header. This generates a "...LNK2019: unresolved external symbol..." because the C++ compile decorates (mangles) the 'logger' name such that the linker can't match the undecorated C name with the decorated C++ name.

The MSVC docs state that the MS compiler will support both 'extern "C"' and 'extern "C++"'. However, the Intel compiler marks the quote mark of 'extern "' as an error.

Anyone know how to get the Intel compiler to mark this reference so that it can be linked to both C++ and C ?

like image 939
Dweeberly Avatar asked Jan 20 '26 02:01

Dweeberly


1 Answers

// when compiling C++ code, use the non-mangled C names
#ifdef __cplusplus
# define EXTERN extern "C"
#else
# define EXTERN extern 
#endif

EXTERN Loggerp logger;
like image 65
robthebloke Avatar answered Jan 22 '26 14:01

robthebloke