Solved

C App with C++ Lib

Posted on 2006-06-20
7
291 Views
Last Modified: 2010-04-15
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?
0
Comment
Question by:woigl
7 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 16940472
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.
0
 

Author Comment

by:woigl
ID: 16940485
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...

0
 
LVL 48

Accepted Solution

by:
AlexFM earned 250 total points
ID: 16940525
Use dumpbin utility to see list of functions exported from C++ library:
dumpbin /exports yourlib.dll

If you see your functions like this:
 ?Function@@3HA

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:

Function
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 15

Expert Comment

by:bpmurray
ID: 16941720
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++.
0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 16947329
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.

e.g.

Say in module aaa.cpp you have:

 In your xxx.c use:

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

Paul
0
 
LVL 7

Expert Comment

by:Chatable
ID: 16963386
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" {
#endif
and in the end of the file:
#ifdef __cplusplus
}
#endif
It should compile and work perfectly.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now