• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 356
  • Last Modified:

C App with C++ Lib

i wrote a library in C++ and compiled with VC6.0

now i write a C program in VC6.0 and want to link the C++ lib to it.

but i get always undefined references at linking time... if i change my app to C++ then it works...

can someone tell me and help me to solve the problem?
1 Solution
If C++ library exports functions, the problem is in C++ names mangling. Use extern "C" in function definitions to prevent this.
If C++ library exports classes, you need to write C+ wrapper library which translates C++ interface to API interface.
woiglAuthor Commented:
I think you got me wrong.

Extern "C" i have to use in case if i want to use a C-function in C++, but i am doing it in the opposite way...

Use dumpbin utility to see list of functions exported from C++ library:
dumpbin /exports yourlib.dll

If you see your functions like this:

this is C++ name mangling. Add extern "C" before function definition in h-file and rebuild the library. Run dumpbin again and output should be:

Live webcast with Pinal Dave

Pinal Dave will teach you tricks to help identify the real root cause of database problems rather than red herrings. Attendees will learn scripts that they can use in their environment to immediately figure out their performance Blame Shifters and fix them quickly.

The reason you can't link to the external methods is because they're mangled, as everyone else has mentioned. The only way to do it is to change your library to allow C access. You can retain the C++ character of the lib, and simply add extra functions that are flagged as extern "C". These C functions would then access the C++ functionality, allowing you to have a DLL that's both C and C++.
Hi woigl,

I assume you have guessed that you wont be able to access classes and objects etc.

Write a simple interface module for the library. Call it xxx.c (not .cpp) and it should compile usin C standard syntax.

In xxx.c, write a function that invokes each C++ function you want access to in C.


Say in module aaa.cpp you have:

 In your xxx.c use:

char * CFunction ( char * string )
 return (CPlusPlusFunction ( string ));

extern "C" works both ways. I've done that several times.
You need to make sure that the .h file is perfectly legal in C and doesn't contain anything C++ (that is only for the .h file, of-course, the .cpp file can include any C++ code). Just try #include-ing it from a .c file and check if it causes any compilation errors (Press Ctrl+F7 to compile the current file only).
Then you simply add the following to the beginning of the .h file:
#ifdef __cplusplus
extern "C" {
and in the end of the file:
#ifdef __cplusplus
It should compile and work perfectly.

Featured Post

Will You Be GDPR Compliant by 5/28/2018?

GDPR? That's a regulation for the European Union. But, if you collect data from customers or employees within the EU, then you need to know about GDPR and make sure your organization is compliant by May 2018. Check out our preparation checklist to make sure you're on track today!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now