Article View: comp.unix.programmer
Article #3553Re: static/shared linking with C and C++
From: 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
Technical Details
Message-ID:
<pxbwun9zi07.fsf@news.bourguet.org>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!bloom-beacon.mit.edu!nycmny1-snh1.gtei.net!washdc3-snf1!news.gtei.net!cyclone1.gnilink.net!news-out.nuthinbutnews.com!propagator2-sterling!news-in-sterling.newsfeed.com!newsfeed.icl.net!newsfeed.fjserv.net!proxad.net!feeder2-1.proxad.net!news2-1.free.fr!not-for-mailReferences:
<HFsC9.52551$__1.29668@rwcrnsc51.ops.asp.att.net>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 11318c,68b09b041b067487 X-Google-Attributes: gid11318c,public X-Google-Thread: fc89c,68b09b041b067487 X-Google-Attributes: gidfc89c,public X-Google-ArrivalTime: 2002-11-19 07:39:07 PST Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!bloom-beacon.mit.edu!nycmny1-snh1.gtei.net!washdc3-snf1!news.gtei.net!cyclone1.gnilink.net!news-out.nuthinbutnews.com!propagator2-sterling!news-in-sterling.newsfeed.com!newsfeed.icl.net!newsfeed.fjserv.net!proxad.net!feeder2-1.proxad.net!news2-1.free.fr!not-for-mail Newsgroups: comp.unix.programmer,gnu.gcc.help Subject: Re: static/shared linking with C and C++ References: <HFsC9.52551$__1.29668@rwcrnsc51.ops.asp.att.net> From: Jean-Marc Bourguet <jm@bourguet.org> Message-ID: <pxbwun9zi07.fsf@news.bourguet.org> Date: 19 Nov 2002 16:39:04 +0100 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Lines: 31 Organization: Guest of ProXad - France NNTP-Posting-Date: 19 Nov 2002 16:39:06 MET NNTP-Posting-Host: 158.140.208.29 X-Trace: 1037720346 news2-1.free.fr 2144 158.140.208.29 X-Complaints-To: abuse@proxad.net