Solved

C App with C++ Lib

Posted on 2006-06-20
7
313 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

813 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

18 Experts available now in Live!

Get 1:1 Help Now