   C vs. C++ extern
  Hi, I have a problem using extern declarations in a C/C++ mixed solution.The following code works perfectly if both files are .cpp//main.cppint myVar;//client.cppextern int myVar;void test(){myVar=99;}But if I rename client.cpp into client.c, the linker gives this error:test.obj : error LNK2001: unresolved external symbol _myVar   
  You need to give myVar C linkage.extern "C" {  int myVar;};  This is because C++ uses name mangling.   ;  Thanks for your help guys !In the header.h file I have put the following code as you suggested, but with a little difference:#ifdef __cplusplus extern "C" {#endif int myVar;#ifdef __cplusplus}#endifI ran a test including the header.h file in a file1.c and file1.cpp file. It works and this way I can share the myVar value.However, if I want to access myVar also in other cpp files, I get an error. I included the header.h file in file2.cpp, but linker says that "_myVar has already been defined in file1.obj".I'm getting nut with this ...   ;Quote:Original post by SiCraneYou need to give myVar C linkage.extern "C" {  int myVar;}Alternatively, there's the shorter formextern "C" int myVar;;Quote:Original post by Alessandro... but linker says that "_myVar has already been defined in file1.obj".The problem is with this: #ifdef __cplusplusextern "C" {#endifint myVar;#ifdef __cplusplus}#endifWhen __cplusplus is *not* defined, the var is no longer declared extern, and as such gets compiled into multiple obj files.One possible solution.... #ifdef __cplusplusextern "C" {#endifextern int myVar;#ifdef __cplusplus}#endif;  Thank you very much, again !   ;#ifdef __cplusplus#define C "C"#else#define C#endifextern C int myVar;(don't do that).;Quote:Original post by NotAYakk *** Source Snippet Removed ***(don't do that).That's a little tricky, but I wouldn't have a problem with#ifdef __cplusplus#define EXTERN(x) extern "C" { x; }#else#define EXTERN(x) extern x;#endifEXTERN(int myVar) // a semicolon here is unnecessary but should be harmless;Quote:Original post by ZahlmanThat's a little tricky, but I wouldn't have a problem with*** Source Snippet Removed ***Personally I'd prefer to omit the argument:#ifdef __cplusplus#define EXTERN_C extern "C"#else#define EXTERN_C extern#endifEXTERN_C int my_var;That way the macro acts more like any other storage class, and it's also less likely to confuse your IDE (this is actually a real problem, many editors have problems even with basic things like functions declared through typedefs.)Plus most projects tend to end up with a number of other similar compiler-specific attributes for things like compiler hints, shared library exposure, or calling conventions. Besides all that, though, something seriously odd is going on if you find yourself importing individual C functions and variables rather than entire C headers.   