Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use C Headers in a C++ Program?

I am working on a project in Visual Studio 2010 which is to produce a win 32 dll file. The examples I have are C files and the compile and work well. I would like to incorporate some functionality from C++ function I've written but I have hit a bit of a wall.

If I attempt to link the C++ functions to the C program, it knows nothing about strings, etc and just doesn't work at all (of course).

So I am left with changing the example into a C++ program, then I can use my other files with impunity. When I attempt to do this I get a link error that I don't understand and am uncertain about how to resolve.

The examples use vendor provided headers, which include statements such as

 typedef void ( __cdecl *BINDING_PROC_BEVNT)(WORD_T choice, INT_T * pStatus, 
            I_EVNT_T  * pIn, O_EVNT_T  * pOut);

In the body of the main code, following the examples:

extern BINDING_PROC_BEVNT       b_evnt;

Which then allows you to write

b_evnt(choice, &status, &inpEvent, &outpEvent);

In a vendor provided C file, these are again referenced as:

BINDING_PROC_BEVNT      b_evnt; 
b_evnt = (BINDING_PROC_BEVNT)GetProcAddress(hCNCMod, "bevnt");

The linker error I am seeing is:

error LNK2001: unresolved external symbol "void (__cdecl* b_evnt)(unsigned short,short *,union I_EVNT_T *,union O_EVNT_T *)" (?b_evnt@@3P6AXGPAFPATI_EVNT_T@@PATO_EVNT_T@@@ZA)

If I rename my main file and recompile as a C program, and omit my C++ functions, everything compiles perfectly. Even when the main file is processed as a C++ file, Intellisense seems to recognize the definitions (hovering over shows the correct definitions).

Additionally I attempted to add extern "C" to a few different locations but it didn't seem to make a difference in the C++ files, and generated a compile error in the C files (about not knowing about strings).

Any insight would be appreciated, I may have simply stared at this too long today to be picking up on something obvious, or it may be something I'm completely unaware of.

Thanks for the help!

like image 202
Stephen Avatar asked Jul 06 '11 20:07

Stephen


People also ask

Do you include C file in header file?

The header file contains only declarations, and is included by the . c file for the module. Put only structure type declarations, function prototypes, and global variable extern declarations, in the . h file; put the function definitions and global variable definitions and initializations in the .


1 Answers

If you are compiling against a library that has C-language bindings, you have to tell C++ explicitly that the header files for the library reference C-objects, not C++ objects, or C++ name mangling will prevent correct linking. Often you can do this like so:

extern "C" {
#include "vendor.h"
}

This will tell the C++ compiler that the symbols between the braces are C symbols, and should not have name mangling applied.

like image 77
antlersoft Avatar answered Oct 09 '22 12:10

antlersoft