Linking a C program to a C++ library which uses STL

J

Joe.pHsiao

Hi,

I tried to link a C program to a library which is written by me in C+
+.
I read some posts about linking a C program to C++ libraries. It seems
doable by adding extern "C" to the C++ head file and to the function
body modifier. However, in my test, it still doesn't work. The linking
error messages are like undefined reference for new operator, and
undefined reference from dequeue.tcc. ( sorry I don't know how to copy
lines from terminals under solaris.)

I used STL vector and queue in the library, and I guess they uses new
operator to put items inside them.
The C program fails to link if gcc is used.
If I use g++ to build the file, there's no problem.

So I thought the code in the C++ library should also be recognized in
C. Keywords like "new," " delete," and "vector" are not allowed.
But then I thought if the code is recognized in C is the first place,
what is extern "C" for?

Can somebody explain this to me? Thanks.
 
K

Keith Thompson

I tried to link a C program to a library which is written by me in C++.
I read some posts about linking a C program to C++ libraries.
[...]

The C++ standard defines mechanisms for calling C from C++,
and for calling C++ from C. The C standard does not define any
such mechanisms. Therefore, your question is more appropriate to
comp.lang.c++ than to comp.lang.c.

You'll find, not surprisingly, that you're not the first person to
have asked this kind of question. So your first stop, before posting
to comp.lang.c++, should be the C++ FAQ,
<http://www.parashift.com/c++-faq-lite/>, particularly section 32.
 
D

Default User

Hi,

I tried to link a C program to a library which is written by me in C+
+.
I read some posts about linking a C program to C++ libraries. It seems
doable by adding extern "C" to the C++ head file and to the function
body modifier. However, in my test, it still doesn't work. The linking
error messages are like undefined reference for new operator, and
undefined reference from dequeue.tcc. ( sorry I don't know how to copy
lines from terminals under solaris.)

extern "C" doesn't magically allow you to compile C++ code as C. You
need to have the library compiled with a C++ compiler, with a C-style
API, and link with the C code.

The newsgroup comp.lang.c++ is the correct one for this question. I
have crossposted this reply and set follow-ups.




Brian
 
A

Anand Hariharan

( sorry I don't know how to copy
lines from terminals under solaris.)

<ot>
Just select the text you want copied. Where you want it pasted, click the
middle mouse button. No keyboard interaction is necessary for
copy-pasting.
</ot>

- Anand
 
I

Ian Collins

Hi,

I tried to link a C program to a library which is written by me in C+
+.
I read some posts about linking a C program to C++ libraries. It seems
doable by adding extern "C" to the C++ head file and to the function
body modifier. However, in my test, it still doesn't work. The linking
error messages are like undefined reference for new operator, and
undefined reference from dequeue.tcc. ( sorry I don't know how to copy
lines from terminals under solaris.)
The short answer is you have to link with the C++ compiler, otherwise
you won't get the required C++ libraries.
 
M

Malcolm McLean

Hi,

I tried to link a C program to a library which is written by me in C+
+.
I read some posts about linking a C program to C++ libraries. It seems
doable by adding extern "C" to the C++ head file and to the function
body modifier. However, in my test, it still doesn't work. The linking
error messages are like undefined reference for new operator, and
undefined reference from dequeue.tcc. ( sorry I don't know how to copy
lines from terminals under solaris.)

I used STL vector and queue in the library, and I guess they uses new
operator to put items inside them.
The C program fails to link if gcc is used.
If I use g++ to build the file, there's no problem.

So I thought the code in the C++ library should also be recognized in
C. Keywords like "new," " delete," and "vector" are not allowed.
But then I thought if the code is recognized in C is the first place,
what is extern "C" for?

Can somebody explain this to me? Thanks.
extern "C" allows you to define a C-style wrapper for your functions that is
callable from both C and C++.
What you cannot do is include C++ only syntax like templates in the
C-callable prototypes. C simply won't recognise them.
 
I

Ian Collins

Malcolm said:
extern "C" allows you to define a C-style wrapper for your functions
that is callable from both C and C++.
What you cannot do is include C++ only syntax like templates in the
C-callable prototypes. C simply won't recognise them.
And C++ won't compile them!
 

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,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top