C/C++ linkage issues

  • Thread starter Generic Usenet Account
  • Start date
G

Generic Usenet Account

I am implementing a library that can be linked in with C as well as C++
code. Obviously, the library API is functional, but in my
implementation I am using STL. Because of this, my code is compiled
using a C++ compiler (gcc version 3.3.1). Given the differences in the
way C and C++ code gets linked in, what should I do to ensure that the
C code that links in the library does not give any linker error?

Thanks,
Gus
 
R

Rolf Magnus

Hello Generic, your question is off-topic in all the newsgroups you posted,
since it isn't about Standard C++, Standard C, and not about D either.
I am implementing a library that can be linked in with C as well as C++
code. Obviously, the library API is functional,

What do you mean by that? It works?
but in my implementation I am using STL. Because of this, my code is
compiled using a C++ compiler (gcc version 3.3.1). Given the differences
in the way C and C++ code gets linked in, what should I do to ensure that
the C code that links in the library does not give any linker error?

Link it with g++.

xpost&f'up2 gnu.g++.help
 
R

Robbie Hatley

Generic said:
I am implementing a library that can be linked in with C as well as C++
code. Obviously, the library API is functional, but in my
implementation I am using STL. Because of this, my code is compiled
using a C++ compiler (gcc version 3.3.1). Given the differences in the
way C and C++ code gets linked in, what should I do to ensure that the
C code that links in the library does not give any linker error?

Well, Generic, section 32 of the FAQ for comp.lang.c++
covers those issues quite well. You can read it at:

http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html

I've had to struggle with those issues myself a lot at work
(the software there is a Frankenstein of C and C++) but I
found the link above quite helpful.

--
Cheers,
Robbie Hatley
Tustin, CA, USA
email: lonewolfintj at pacbell dot net
web: home dot pacbell dot net slant earnur slant
 
G

Generic Usenet Account

Rolf said:
Hello Generic, your question is off-topic in all the newsgroups you posted,
since it isn't about Standard C++, Standard C, and not about D either.

I DISAGREE. The whole topic pertaining to the interworking between C
and C++ code is of great interest to an entire community of
programmers. IMHO it is not OT for comp.lang.c or comp.lang.c++. As
far as comp.sources.d goes, any posting that pertains to source code
falls within its realm.
What do you mean by that? It works?

I though that my statement was pretty obvious. If the API needs to be
invoked by C as well as C++, the API cannot be defined in terms of
public methods on a class. It has to be defined in terms of
"free-standing" functions.
xpost&f'up2 gnu.g++.help

I don't know what makes you feel that this topic is more relevant to
the gnu.g++.help newsgroup. My query was general, and not pertaining
to g++. I volunteered the compiler version, because often times fellow
posters have asked for this kind of information in their response. I
submit that the issue of interoperability between C and C++ code has
less to do with the compiler that is being used and more to do with the
difference in C and C++ linkage. I am therefore "undoing" the f'up.
 
M

Martin Ambuhl

Generic said:
I DISAGREE. The whole topic pertaining to the interworking between C
and C++ code is of great interest to an entire community of
programmers.

Whether one prefers Chinese food to Italian food is of great interest to
an entire community of programmers. You are in error.
IMHO it is not OT for comp.lang.c or comp.lang.c++.

Your ill-informed opinion has nothing to do with the question.
 
R

Rolf Magnus

Generic said:
I DISAGREE. The whole topic pertaining to the interworking between C
and C++ code is of great interest to an entire community of
programmers.

comp.lang.c and comp.lang.c++ are about the C resp. C++ language as defined
by the ISO standards. Those don't define how the code is linked or how code
compiled from different languages interacts, and so problems you might find
when linking C and C++ code together are mostly (*) platform/compiler
specific, which means that such questions are best dealt with in a
platform/compiler specific programming newsgroup.

(*) One thing that the C++ standard does mention (but not define) is "C"
linkage. When writing a function in C++ that is supposed to be called from
a C function, you should declare it as extern "C". However, since there is
no specific linkage defined by the C standard, this will only work if the C
compiler actually uses the same linkage. For gcc/g++ (and here we're
compiler specific again), this is true.
IMHO it is not OT for comp.lang.c or comp.lang.c++. As
far as comp.sources.d goes, any posting that pertains to source code
falls within its realm.

It seems to me that postings that ask for or offer source code (neither of
which you were doing) are on-topic in comp.sources.d, but I haven't looked
well enough to judge that. I was wrong to assume that it's about the
programming language D, so now I dare only speak for comp.lang.c and
comp.lang.c++.
I though that my statement was pretty obvious. If the API needs to be
invoked by C as well as C++, the API cannot be defined in terms of
public methods on a class. It has to be defined in terms of
"free-standing" functions.

Ah. I see.
I don't know what makes you feel that this topic is more relevant to
the gnu.g++.help newsgroup. My query was general, and not pertaining
to g++.

However, the answers are mostly compiler specific, like mine that adviced
you to link everything with g++.
I submit that the issue of interoperability between C and C++ code has
less to do with the compiler that is being used and more to do with the
difference in C and C++ linkage.

The problem is just that C and C++ don't define a specific linkage. The
compiler does that.
I am therefore "undoing" the f'up.

I won't fight with you over f'ups. ;-)
 
L

Lawrence Kirby

I am implementing a library that can be linked in with C as well as C++
code. Obviously, the library API is functional, but in my
implementation I am using STL. Because of this, my code is compiled
using a C++ compiler (gcc version 3.3.1). Given the differences in the
way C and C++ code gets linked in, what should I do to ensure that the
C code that links in the library does not give any linker error?

C has no facilities for inter-language linking, C++ has extern "C". Your
best bet would probably be to discuss that in comp.lang.c++ only.
Followups set.

Lawrence
 
C

CBFalconer

Generic said:
I DISAGREE. The whole topic pertaining to the interworking
between C and C++ code is of great interest to an entire community
of programmers. IMHO it is not OT for comp.lang.c or
comp.lang.c++. As far as comp.sources.d goes, any posting that
pertains to source code falls within its realm.
.... snip ...

I don't know what makes you feel that this topic is more relevant
to the gnu.g++.help newsgroup. My query was general, and not
pertaining to g++. I volunteered the compiler version, because
often times fellow posters have asked for this kind of information
in their response. I submit that the issue of interoperability
between C and C++ code has less to do with the compiler that is
being used and more to do with the difference in C and C++ linkage.
I am therefore "undoing" the f'up.

The .lang groups deal with the language, not the nitty gritty of
moving the code into suitable places on end machines. What makes
you think you can walk in from the street and postulate what is and
is not on-topic in these groups? F'ups set.
 

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

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,163
Latest member
Sasha15427
Top