Trouble linking cpp object with some library
Hi, Is there any special steps to link a *.cpp to a library created and compiled with C compiler? I have a class that calls a C function in an existing library (libXX.so). This library is written in C. I got the following error while compiling, and I think it happens during linking: =========== cincai@haha:~/tmp/msg++/src> g++ -I../include \ -L/home/cincai/tmp/grimpsim/lib -lsimgrid Host.cpp /tmp/ccfjw59W.o(.text+0x76): In function `Host::Host[not-in-charge](std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, double)': : undefined reference to `MSG_host_create(char const*, char*, double, void*)' ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ /tmp/ccfjw59W.o(.text+0x15a): In function `Host::Host[in-charge](std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, double)': : undefined reference to `MSG_host_create(char const*, char*, double, void*)' ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ collect2: ld returned 1 exit status =========== The content of /home/cincai/tmp/grimpsim/lib is: =========== libsimgrid.a libsimgrid.la libsimgrid.so libsimgrid.so.0 libsimgrid.so.0.0.1 =========== -- -- Verdi March --
Verdi March wrote:
Hi,
Is there any special steps to link a *.cpp to a library created and compiled with C compiler?
No. The error means that you're failing to link something. Unfortunately, it doesn't tell you where that something is. It could be another library or it could be an object file. Whatever the something is, it is Host.cpp and not the library that needs it. Look at the #include lines in Host.cpp (or possibly Host.h if Host.cpp contains a line #include"Host.h") for clues. -- JDL Non enim propter gloriam, diuicias aut honores pugnamus set propter libertatem solummodo quam Nemo bonus nisi simul cum vita amittit.
On Fri, 6 Jun 2003 15:58:09 +0800 Verdi March <cincaipatron@gmx.net> wrote:
Hi,
Is there any special steps to link a *.cpp to a library created and compiled with C compiler?
I have a class that calls a C function in an existing library (libXX.so). This library is written in C. I got the following error while compiling, and I think it happens during linking: Make sure that the prototype for the function you are calling is declared as a C function (as an example): #if defined __cplusplus extern "C" { int foo(int a, int b); } #endif /* __cplusplus */ Most of the standard C functions are already taken care of when including things like <cstdio>.
-- Jerry Feldman <gaf@blu.org> Boston Linux and Unix user group http://www.blu.org PGP key id:C5061EA9 PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9
On Saturday 07 June 2003 20:17, Jerry Feldman wrote:
Make sure that the prototype for the function you are calling is declared as a C function (as an example):
Ok, so I declare all the C functions as: #if defined __cplusplus extern "C" { m_host_t MSG_host_create(const char* name, char* trace_file, double cpu, void *data); /* other functions... */ } #endif /* __cplusplus */ Now, g++ complains about conflicting declarations the C libary header file (msg.h). What am I supposed to do now? I have the source to the library, is there anything I need to modify (presummably the header files)? ========== ../include/msg.h:40: error: previous declaration of `s_m_host* MSG_host_create(const char*, char*, double, void*)' with C++ linkage Host.cpp:21: error: conflicts with new declaration with C linkage ../include/msg.h:48: error: previous declaration of `MSG_error_t MSG_host_destroy(s_m_host*)' with C++ linkage Host.cpp:24: error: conflicts with new declaration with C linkage Host.cpp:25: error: parse error before `char' ========== -- -- Verdi March --
On Mon, 9 Jun 2003 18:38:38 +0800 Verdi March <cincaipatron@gmx.net> wrote:
On Saturday 07 June 2003 20:17, Jerry Feldman wrote:
Make sure that the prototype for the function you are calling is declared as a C function (as an example):
Ok, so I declare all the C functions as:
#if defined __cplusplus extern "C" { m_host_t MSG_host_create(const char* name, char* trace_file, double cpu, void *data); /* other functions... */ } #endif /* __cplusplus */
Now, g++ complains about conflicting declarations the C libary header file (msg.h). What am I supposed to do now? I have the source to the library, is there anything I need to modify (presummably the header files)?
========== ../include/msg.h:40: error: previous declaration of `s_m_host* MSG_host_create(const char*, char*, double, void*)' with C++ linkage Host.cpp:21: error: conflicts with new declaration with C linkage ../include/msg.h:48: error: previous declaration of `MSG_error_t MSG_host_destroy(s_m_host*)' with C++ linkage Host.cpp:24: error: conflicts with new declaration with C linkage Host.cpp:25: error: parse error before `char' Rather than wrapping your own prototype, wrap the entire include: extern "C" { #include <sys/msg.h> }
Also, while this should not be the problem, do not include names in prototypes: m_host_t MSG_host_create(const char*, char*, double, void *); The extern "C" needs to surround any name that will be used in your code to prevent C++ from mangling it. It's been a few years since I've done extensive mixed language programming. g++ provides the c header files (eg. cstdio) as a way to provide access to the standard C runtime library from C++. -- Jerry Feldman <gaf@blu.org> Boston Linux and Unix user group http://www.blu.org PGP key id:C5061EA9 PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9
On Monday 09 June 2003 21:00, Jerry Feldman wrote:
Rather than wrapping your own prototype, wrap the entire include: extern "C" { #include <sys/msg.h> }
This one works. Thanks alot Jerry. -- -- Verdi March --
On Mon, 9 Jun 2003 23:32:57 +0800 Verdi March <cincaipatron@gmx.net> wrote:
On Monday 09 June 2003 21:00, Jerry Feldman wrote:
Rather than wrapping your own prototype, wrap the entire include: extern "C" { #include <sys/msg.h> }
This one works. Thanks alot Jerry. IMHO, those who write C header files should make them compatible with C++. We did this with every C header file on Tru64 Unix.
-- Jerry Feldman <gaf@blu.org> Boston Linux and Unix user group http://www.blu.org PGP key id:C5061EA9 PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9
Hi, What Java IDE are developers using on SuSE? We have been looking a JBuilder from Borland. Thank you. -- __________________________ DJ
I use SunOne Studio on Suse 8.1. I liked it verymuch. I use it to program C/C++ and also do XML development. Servlets, JSP and J2EE development. It has integrated build environments like ANT, and source control like cvs, testing framework like JUnit etc. It is based on the Netbeans IDE. It is very modular. You can plug-in a custom module. I also use it on Windows at work. The community edition (which does not have J2EE) is free. I have heard about eclipse, but haven't used it. Hope this helps. Jay --- Thorsten Haude <linux@thorstenhau.de> wrote:
Hi,
* DJ <linux_programmer@hotmail.com> [2003-06-10 17:52]:
What Java IDE are developers using on SuSE?
Have a look at Eclipse.
Thorsten -- When a thing has been said and said well, have no scruple. Take it and copy it. - Anatole France
ATTACHMENT part 2 application/pgp-signature
__________________________________ Do you Yahoo!? The New Yahoo! Search - Faster. Easier. Bingo. http://search.yahoo.com
Hi
What Java IDE are developers using on SuSE?
We have been looking a JBuilder from Borland.
Well, personally I use NetaBeans. It's a great IDE and it's free. http://www.netbeans.org Ben. _____________________________________________________________________ This e-mail has been scanned for viruses by MCI's Internet Managed Scanning Services - powered by MessageLabs. For further information visit http://www.mci.com
And don't forget IntelliJ IDEA (it isn't free though). -- -- Verdi March --
participants (7)
-
Ben Hills
-
DJ
-
jay s
-
Jerry Feldman
-
John Lamb
-
Thorsten Haude
-
Verdi March