Thread View: comp.unix.programmer
4 messages
4 total messages
Started by Anon
Tue, 19 Nov 2002 15:21
static/shared linking with C and C++
Author: Anon
Date: Tue, 19 Nov 2002 15:21
Date: Tue, 19 Nov 2002 15:21
24 lines
2415 bytes
2415 bytes
My question relates to the complexities of cross-linking object files and libraries between C and C++ (specifically gcc and g++) compiled code. Details: I'm developing a suite of Unix applications. Each app uses at least one shared library and at least one executable which links to it. I've also got a collection of utility routines in C which are used by the entire suite, including the shared libraries. What I've been doing, and which works quite well, is to compile the utility routines into a little static library (libutil.a), making sure to compile with -fpic. This way executables can link to it statically, which I prefer for simplicity of delivery, and the shared libraries can do the same, in which case these symbols become part of the shlib. In the latter case I make the utility symbols private to the shlib with a version script. Now I'm trying to add a program written in C++ to the suite. I find that if I compile libutil.a with gcc the C++ program can't link with it (unresolved references) and if I compile it with g++ the C programs can't use it. Do I need to keep two versions around or is there some magic switch I'm missing? Thanks in advance DD
Re: static/shared linking with C and C++
Author: Jean-Marc Bourguet
Date: Tue, 19 Nov 2002 15:39
Date: Tue, 19 Nov 2002 15:39
31 lines
2075 bytes
2075 bytes
"@please@no.spam" <""ddrav\"@please@no.spam"> writes:
> Now I'm trying to add a program written in C++ to the suite. I find
> that if I compile libutil.a with gcc the C++ program can't link with
> it (unresolved references) and if I compile it with g++ the C
> programs can't use it. Do I need to keep two versions around or is
> there some magic switch I'm missing?
This is a pure C++ question. You should put
#ifdef __cplusplus
extern "C" {
#endif
...
#ifdef __cplusplus
}
#endif
around your header. After this you should be able to compile the
library with the C compiler and link it to C++ programs (always using
the C++ compiler to do the link).
Without the extern "C" stuff, C++ compiler "mangles" the function
names (for several raisons, most notable beeing: preventing linking
code compiled using different ABI and beeing able to do overloading).
Yours,
--
Jean-Marc
Re: static/shared linking with C and C++
Author: Andre P. Lemos
Date: Tue, 19 Nov 2002 15:46
Date: Tue, 19 Nov 2002 15:46
54 lines
2972 bytes
2972 bytes
The problem is that you have to put on the header files of your library
the following code before and after the declarations of the functions and
structures:
#ifdef __cplusplus
extern "C"{
#endif
// Declarations of the functions and structures
void f();
void g();
#ifdef _cplusplus
}
#endif
In that way the g++ compiler will known that you are using some C code and
the undefined references will not occour again.
[]' s
Andre Paim Lemos
CS Student - UFMG - Brazil
On Tue, 19 Nov 2002, @please@no.spam wrote:
> My question relates to the complexities of cross-linking object files
> and libraries between C and C++ (specifically gcc and g++) compiled code.
>
> Details: I'm developing a suite of Unix applications. Each app uses at
> least one shared library and at least one executable which links to it.
> I've also got a collection of utility routines in C which are used by
> the entire suite, including the shared libraries.
>
> What I've been doing, and which works quite well, is to compile the
> utility routines into a little static library (libutil.a), making sure
> to compile with -fpic. This way executables can link to it statically,
> which I prefer for simplicity of delivery, and the shared libraries can
> do the same, in which case these symbols become part of the shlib. In
> the latter case I make the utility symbols private to the shlib with a
> version script.
>
> Now I'm trying to add a program written in C++ to the suite. I find that
> if I compile libutil.a with gcc the C++ program can't link with it
> (unresolved references) and if I compile it with g++ the C programs
> can't use it. Do I need to keep two versions around or is there some
> magic switch I'm missing?
>
> Thanks in advance
> DD
>
>
Re: static/shared linking with C and C++
Author: Anon
Date: Tue, 19 Nov 2002 16:16
Date: Tue, 19 Nov 2002 16:16
21 lines
1936 bytes
1936 bytes
Jean-Marc Bourguet wrote:
> "@please@no.spam" <""ddrav\"@please@no.spam"> writes:
>
>
>>Now I'm trying to add a program written in C++ to the suite. I find
>>that if I compile libutil.a with gcc the C++ program can't link with
>>it (unresolved references) and if I compile it with g++ the C
>>programs can't use it. Do I need to keep two versions around or is
>>there some magic switch I'm missing?
>
>
> This is a pure C++ question. You should put
>
> #ifdef __cplusplus
> extern "C" {
> #endif
Oh, I should have known that (and did in some dark corner of my brain).
Thanks and sorry for the dumb question.
DD
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads