Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C App with C++ Lib

Posted on 2006-06-20
7
Medium Priority
?
347 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 750 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

704 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