473,804 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2494
Jo********@gmai l.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.c om/c++-faq-lite/>, particularly section 32.

--
Keith Thompson (The_Other_Keit h) <ks***@mib.or g>
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********@gmai l.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********@gmai l.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********@gma il.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
1245
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 get unresolved references like: PySequence_DelSlice PyNumber_InPlaceXor PyInt_FromLong _PyObject_New This is just a sample, there are actually 130 of similarly named functions.
7
5127
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 normal functions in header files results in multiple definition errors even when include guards are used. -- STH Hatton's Law: "There is only One inviolable Law" KDevelop: http://www.kdevelop.org SuSE: http://www.suse.com Mozilla:...
20
3245
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 references to functions and objects not defined in the current translation. All such translator output is collected into a program image which contains information needed for execution in its execution environment." What I'm wondering is what exactly...
5
1693
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 want to compile addon.c into libaddon.so (aka addon.dll) so that it can later be loaded into a running main program if necessary. When I try to do that, I get this error: $ gcc -fPIC -Wl,-export-dynamic -shared -o addon.dll addon.o
0
1477
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 static library in a VS .NET 2003 managed C++ project. Currently, I am just linking the compiled .lib file into my VC 7 project. (This is the simplest approach, as it is not practical to move the static library project to VC 7 at this time.) My VC...
2
4196
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 stating: ld: elf error: file library.a(lib_obj.o): elf_strptr: Request error: no string table ld: elf error: file library.a: elf_begin: Format error: archive fmag ld: fatal: File processing errors. No output written to Main collect2: ld...
10
2694
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 .NET2003. also, when I do not try to link the fortran library (just to see if that was the cause), it builds the exe without any problems. i don't even know how to begin addressing this problem...any help would be
7
2397
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 does what I need. I've even tried writing a port forwarder in Java and found problems that nobody seems to have the answer to in forums. I need to make it work essentially the same on both Windows and Linux. There is one program, in C, that...
1
9926
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 defined as follows: extern unsigned mgl_numg, mgl_cur; extern float mgl_fact; extern long mgl_gen_fnt; extern short mgl_buf_fnt;
0
9712
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9594
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10343
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7634
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5530
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4308
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.