473,405 Members | 2,154 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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

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.

Feb 12 '08 #1
6 2460
Jo********@gmail.com writes:
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.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Feb 12 '08 #2
Jo********@gmail.com wrote:
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
Feb 12 '08 #3
On Tue, 12 Feb 2008 15:03:27 -0800, Joe.pHsiao wrote:
( 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

--
ROT-13 email address to reply.
Feb 13 '08 #4
Jo********@gmail.com wrote:
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.

--
Ian Collins.
Feb 13 '08 #5

<Jo********@gmail.comwrote in message
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.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Feb 13 '08 #6
Malcolm McLean wrote:
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!

--
Ian Collins.
Feb 13 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jeffrey Holle | last post by:
I am working on a Linux system using gcc v3.3 and python v2.3.2 I have a problem linking a cpp program to a shared library that uses the Python C API. With just referencing this shared library, I...
7
by: Steven T. Hatton | last post by:
Is there anything that gives a good description of how source code is converted into a translation unit, then object code, and then linked. I'm particularly interested in understanding why putting...
20
by: Steven T. Hatton | last post by:
I just read this in the description of how C++ is supposed to be implemented: "All external object and function references are resolved. Library components are linked to satisfy external...
5
by: Sam Steingold | last post by:
I have a main program and an add-on module that uses some functionality in the main program. E.g., the main program main.c has function foo(). The add-on module in file addon.c calls foo(). I...
0
by: zhangrusi | last post by:
I have a static library that is compiled using the latest version of VC 6. It uses the multithreaded DLL versions of the run-time library, and it uses STL classes. I would like to make use of this...
2
by: Nithya | last post by:
Hi, I encountered a problem when compiling multiple files with make file on solaris. The program uses a thrid party library file(.a) and when linking it to the program with -l option i get a error...
10
by: Julian | last post by:
I get the following error when i try to link a fortran library to a c++ code in .NET 2005. LINK : fatal error LNK1104: cannot open file 'libc.lib' the code was working fine when built using...
7
by: Hal Vaughan | last post by:
I have a problem with port forwarding and I have been working on it for over 2 weeks with no luck. I have found C programs that almost work and Java programs that almost work, but nothing that...
1
by: Jaco Naude | last post by:
Hi, I'm using a static library in my application which links fine except for a few global variables. The static library only contains a bunch of .cpp and .h files and the global variables are...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.