Help with Extern Linkage

Hi Everyone,

I am new to visual c/c++ and i am using visual studio. I am working on a project that compiles with C. Now, i have to use some header files which have structures, templates, etc which are written in C++. I have no idea how to link these two languages together. The command that i give to run the compiler for my project (Ethereal) is "nmake -f makefile.nmake" and it compiles the file i have for C, but gives errors since the included files are in C++ . I would truely appriciate if someone could guide me on what to do and how!
Thanks a ton.

Best regards,
Sahil

[587 byte] By [help_m_with_extern_linkage] at [2008-2-20]
# 1
Hi Sahil:

While a C++ file can, with a little bit of work, include a header file that is written in C the opposite is not true. This is because C++ is, more or less, a super-set of C.

There two ways to combine C and C++ code.

The best way, in my opinion, is to compile the C++ code into a separate module (either a DLL or a LIB) and then to link this module into your C application. You will need to ensure that the entry points to the C++ application are compiled as extern "C" otherwise the C program will not be able to link to them.

The alternative (and this works best if there are only a few of either C++ or C files) is just to compile the C files to *.OBJ file and then to compile the C++ files to *.OBJ files and then link them all together. The is bascially the same as creating a *.LIB for the C++ files except it skips the step of actually creating the *.LIB. Again you will need to ensure the the entries points into the C++ code are compiled as extern "C".

JonathanCavesMSFT at 2007-9-9 > top of Msdn Tech,Visual C++,Visual C++ Language...
# 2

Thank you! :-)

the good part is that the C++ header files that i have already have the extern C and #ifdef __cpluscplus and CLINKAGE defined!
But, the problem is that, i am not sure how to compile the C++ headers into a separate module (either a DLL or a LIB) and then how to link it to my C program!
I know i am asking a whole lot here, but this is where I stand, and you are the only one I can look up to for help at the moment.
Thanks.

Best regards,
Sahil