Linking a C++ library to a C program.

A

a2x

Hi,

I am writing a C program which interfaces with a C++ library. The C++
library works fine with C. I was wondering if I need to use a C++
compiler (g++ in my case) to compile the C program or can I use a C
compiler (gcc).

Thanks.
 
J

Jack Klein

Hi,

I am writing a C program which interfaces with a C++ library. The C++
library works fine with C. I was wondering if I need to use a C++
compiler (g++ in my case) to compile the C program or can I use a C
compiler (gcc).

Thanks.

C does not define linkage to any other language at all. C++, which is
off-topic in comp.lang.c, does not define the mechanics of linkage.
You need to ask on either one of the gnu.gcc.* support groups, or one
specific to you platform.
 
W

websnarf

a2x said:
I am writing a C program which interfaces with a C++ library. The C++
library works fine with C. I was wondering if I need to use a C++
compiler (g++ in my case) to compile the C program or can I use a C
compiler (gcc).

Ok, first, learn what extern "C" means, and learn how to use and apply
it to interface C and C++. Then consider that you have two options: 1)
You can simply put extern "C" around every interface that you wish to
share with C and 2) You can compile the C code with your C++ compiler
and actually make everying actually C++. The first is the cleanest way
but might require more intrusive source modifications. The second is
easiest so long as you aren't doing weird things in C like making
declarations like: struct foo foo;
 
M

Malcolm

a2x said:
I am writing a C program which interfaces with a C++ library. The C++
library works fine with C. I was wondering if I need to use a C++
compiler (g++ in my case) to compile the C program or can I use a C
compiler (gcc).
If you want to call a function written in C++ from a C file your best bet is
to write the C code in the common subset of the two languages and run it
through a C++ compiler. The alternative is to mess about with C++ name
mangling and linkage conventions.
 
C

CBFalconer

Malcolm said:
If you want to call a function written in C++ from a C file your
best bet is to write the C code in the common subset of the two
languages and run it through a C++ compiler. The alternative is to
mess about with C++ name mangling and linkage conventions.

For which alternative, AFAICT, there exists nothing even remotely
resembling a standard, so that everything is totally non-portable.
 
D

Default User

CBFalconer said:
For which alternative, AFAICT, there exists nothing even remotely
resembling a standard, so that everything is totally non-portable.


Correct, but there is a C++ standard way to specify C-style linkage
(extern "C"). That prevents you from calling overloaded functions from
the C module, of course.

Whether a C object file can actually be linked with a C++ object file
or library is still implementation dependent. In practice it works
pretty well.

All of this should be discussed on comp.lang.c++, not here.



Brian
 
C

CBFalconer

Default said:
Correct, but there is a C++ standard way to specify C-style linkage
(extern "C"). That prevents you from calling overloaded functions
from the C module, of course.

Whether a C object file can actually be linked with a C++ object
file or library is still implementation dependent. In practice it
works pretty well.

All of this should be discussed on comp.lang.c++, not here.

Not quite. I am quite willing for C++ users to call my code, so I
habitually insert the #ifdef __cplusplus__ guards for the 'extern
"C" {}' in the header files. That __cplusplus__ is not allowed to
be defined in the C system, for just this purpose.
 
S

S.Tobias

CBFalconer said:
Not quite. I am quite willing for C++ users to call my code, so I
habitually insert the #ifdef __cplusplus__ guards for the 'extern
"C" {}' in the header files. That __cplusplus__ is not allowed to
be defined in the C system, for just this purpose.

ITYM: __cplusplus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top