Project Home
Project Home
Trackers
Trackers
Documents
Documents
Wiki
Wiki
Discussion Forums
Discussions
Project Information
Project Info
Forum Topic - Linking a C library from C++ project : (12 Items)
   
Linking a C library from C++ project  
I'm trying to use the MySQL 5 client to access a database running on another machine.  I'm hosting on Windows Vista 
Momentics 4.01 and targeting 6.3.2 Neutrino running on a separate x86 box.  I've tried both the 2.95 and 3.3 compiler 
tool sets.

When I link from a C project everything is fine.  When I link from a C++ project I get a number of undefined reference 
errors (obviously due to name mangling).  In the MySQl header file there is the expected #ifdef __cplusplus extern "C" {
 code.  I would have expected this to work correctly, but it isn't.

I have run into this with gcc previously with another library.  Not sure what is going on.

Any ideas of how to fix this?

Thanx
Tim
Re: Linking a C library from C++ project  
Here is the command line for the linker:

C:/QNX632/host/win32/x86/usr/bin/qcc -V3.3.5,gcc_ntox86  -lang-c++ -lang-c++    -oC:/Users/Tim.DELTA/Documents/
EclipseWorkspace/Test/x86/o/Test    Test.o  -L. -LC:/QNX632/target/qnx6/x86/lib/mysql -LC:/QNX632/target/qnx6/x86/lib -
LC:/QNX632/target/qnx6/x86/lib/gcc/3.3.5 -LC:/QNX632/target/qnx6/x86/lib -LC:/QNX632/target/qnx6/x86/usr/lib    -Bstatic
    -lmysqlclient_r    -Bdynamic     

I can't see any problems (though the double -lang-c++ seems unusual)

Thanx
RE: Linking a C library from C++ project  
Hi Tim,

You'll want to extern "C" your function implementation as well. I've
attached a sample applcation that does the following:

Main program links in shared library, which links in a static library.
Main program links in a C++ shared library (jtlcpp.cc and jtlcpp.h).

// Idle question: I wonder how many bounces I'll get for sending a .zip
file.

Cheers,
-Brian 

-----Original Message-----
From: Tim Gessner [mailto:tim@deltacompsys.com] 
Sent: Monday, April 28, 2008 12 28
To: momentics-community
Subject: Re: Linking a C library from C++ project 

Here is the command line for the linker:

C:/QNX632/host/win32/x86/usr/bin/qcc -V3.3.5,gcc_ntox86  -lang-c++ -lang-c++
-oC:/Users/Tim.DELTA/Documents/EclipseWorkspace/Test/x86/o/Test    Test.o
-L. -LC:/QNX632/target/qnx6/x86/lib/mysql -LC:/QNX632/target/qnx6/x86/lib
-LC:/QNX632/target/qnx6/x86/lib/gcc/3.3.5 -LC:/QNX632/target/qnx6/x86/lib
-LC:/QNX632/target/qnx6/x86/usr/lib    -Bstatic    -lmysqlclient_r
-Bdynamic     

I can't see any problems (though the double -lang-c++ seems unusual)

Thanx

_______________________________________________
QNX Momentics Community Support
http://community.qnx.com/sf/go/post7399

Attachment: Text jtl-test-project.zip 16.15 KB
Re: Linking a C library from C++ project  
> I'm trying to use the MySQL 5 client to access a database running on another 
> machine.  I'm hosting on Windows Vista Momentics 4.01 and targeting 6.3.2 
> Neutrino running on a separate x86 box.  I've tried both the 2.95 and 3.3 
> compiler tool sets.
> 
> When I link from a C project everything is fine.  When I link from a C++ 
> project I get a number of undefined reference errors (obviously due to name 
> mangling).  In the MySQl header file there is the expected #ifdef __cplusplus 
> extern "C" { code.  I would have expected this to work correctly, but it isn't
> .
> 
> I have run into this with gcc previously with another library.  Not sure what 
> is going on.

Hi Tim,

Are the undefined references errors for the symbols that should have C linkage? Can you use nm on both the C++ object 
and the library to confirm they have C linkage ? 

Can you post the linker output as well?

Regards,

Ryan Mansfield


RE: Linking a C library from C++ project  
And my example was completely bass-ackwards. Sorry about that. 

-----Original Message-----
From: Ryan Mansfield [mailto:rmansfield@qnx.com] 
Sent: Monday, April 28, 2008 12 37
To: momentics-community
Subject: Re: Linking a C library from C++ project 

> I'm trying to use the MySQL 5 client to access a database running on
another 
> machine.  I'm hosting on Windows Vista Momentics 4.01 and targeting 6.3.2 
> Neutrino running on a separate x86 box.  I've tried both the 2.95 and 3.3 
> compiler tool sets.
> 
> When I link from a C project everything is fine.  When I link from a C++ 
> project I get a number of undefined reference errors (obviously due to
name 
> mangling).  In the MySQl header file there is the expected #ifdef
__cplusplus 
> extern "C" { code.  I would have expected this to work correctly, but it
isn't
> .
> 
> I have run into this with gcc previously with another library.  Not sure
what 
> is going on.

Hi Tim,

Are the undefined references errors for the symbols that should have C
linkage? Can you use nm on both the C++ object and the library to confirm
they have C linkage ? 

Can you post the linker output as well?

Regards,

Ryan Mansfield




_______________________________________________
QNX Momentics Community Support
http://community.qnx.com/sf/go/post7402
Re: RE: Linking a C library from C++ project  
Thanx!
Re: Linking a C library from C++ project  
Interestingly if I undefine __cplusplus before including the mysql header the only undefined references are to the mysql
 functions I call.

If I do not undefine _cplusplus before including the mysql header the undefined references are to a number of other 
libraries including socket, m, <taocrypt?>, etc. along with __pure virtual and __builtin_delete.

The output of nm for the app object is (with __cplusplus defined)
C:\Users\Tim.DELTA\Documents\EclipseWorkspace\Test\x86\o-g>\QNX632\host\win32\x86\usr\ntox86\bin\nm.exe --no-demangle 
test.o
         U __gxx_personality_v0
00000000 T main
         U mysql_close
         U mysql_error
         U mysql_init
         U mysql_real_connect

and the library is similar for these functions.  There are some mangled names in the library from what appears to be the
 taocrypt library.

I have attached the output of nm for the library as it is very long.

I'm not very familiar with using nm so please let me know if the output is not what you are looking for.

Thanx
Tim
Attachment: Text nm.out 317.19 KB
Re: Linking a C library from C++ project  
Here is the linker output

make -k EXTRA_SUFFIXES=cpp  clean all --file=C:/Users/Tim.DELTA/Documents/EclipseWorkspace/Test/QMakefile51860.tmp 
make -j 1 -Cx86 -fMakefile clean
make[1]: Entering directory `C:/Users/Tim.DELTA/Documents/EclipseWorkspace/Test/x86'
make -j 1 -Co -fMakefile clean 
make[2]: Entering directory `C:/Users/Tim.DELTA/Documents/EclipseWorkspace/Test/x86/o'
C:/QNX632/host/win32/x86/usr/bin/rm -f  Test  *.pinfo *.o *.err *.map mapfile *.sym *.i   
make[2]: Leaving directory `C:/Users/Tim.DELTA/Documents/EclipseWorkspace/Test/x86/o'
make -j 1 -Co-g -fMakefile clean
make[2]: Entering directory `C:/Users/Tim.DELTA/Documents/EclipseWorkspace/Test/x86/o-g'
C:/QNX632/host/win32/x86/usr/bin/rm -f  Test_g  *.pinfo *.o *.err *.map mapfile *.sym *.i   
make[2]: Leaving directory `C:/Users/Tim.DELTA/Documents/EclipseWorkspace/Test/x86/o-g'
make[1]: Leaving directory `C:/Users/Tim.DELTA/Documents/EclipseWorkspace/Test/x86'
make -j 1 -Cx86 -fMakefile all
make[1]: Entering directory `C:/Users/Tim.DELTA/Documents/EclipseWorkspace/Test/x86'
make -j 1 -Co -fMakefile all 
make[2]: Entering directory `C:/Users/Tim.DELTA/Documents/EclipseWorkspace/Test/x86/o'
C:/QNX632/host/win32/x86/usr/bin/qcc -V3.3.5,gcc_ntox86 -c -Wc,-Wall -Wc,-Wno-parentheses  -O          -DNDEBUG   -I. -
IC:/Users/Tim.DELTA/Documents/EclipseWorkspace/Test/x86/o -IC:/Users/Tim.DELTA/Documents/EclipseWorkspace/Test/x86 -IC:/
Users/Tim.DELTA/Documents/EclipseWorkspace/Test -IC:/QNX632/target/qnx6/usr/include       C:/Users/Tim.DELTA/Documents/
EclipseWorkspace/Test/Test.cpp 
C:/Users/Tim.DELTA/Documents/EclipseWorkspace/Test/Test.cpp: In function `int 
   main(int, char**)':
C:/Users/Tim.DELTA/Documents/EclipseWorkspace/Test/Test.cpp:21: warning: unused 
   variable `const char*str'
C:/Users/Tim.DELTA/Documents/EclipseWorkspace/Test/Test.cpp:23: warning: unused 
   variable `long int l'
C:/QNX632/host/win32/x86/usr/bin/rm -f  C:/Users/Tim.DELTA/Documents/EclipseWorkspace/Test/x86/o/Test
C:/QNX632/host/win32/x86/usr/bin/qcc -V3.3.5,gcc_ntox86  -lang-c++ -lang-c++    -oC:/Users/Tim.DELTA/Documents/
EclipseWorkspace/Test/x86/o/Test    Test.o  -L. -LC:/QNX632/target/qnx6/x86/lib/mysql -LC:/QNX632/target/qnx6/x86/lib -
LC:/QNX632/target/qnx6/x86/lib/gcc/3.3.5 -LC:/QNX632/target/qnx6/x86/lib -LC:/QNX632/target/qnx6/x86/usr/lib    -Bstatic
    -lmysqlclient_r    -Bdynamic     
C:/QNX632/target/qnx6/x86/lib/mysql/libmysqlclient_r.a(client.o): In function `my_connect':
client.o(.text+0x2b): undefined reference to `connect'
client.o(.text+0x65): undefined reference to `connect'
C:/QNX632/target/qnx6/x86/lib/mysql/libmysqlclient_r.a(client.o): In function `wait_for_data':
client.o(.text+0x1bf): undefined reference to `getsockopt'
C:/QNX632/target/qnx6/x86/lib/mysql/libmysqlclient_r.a(client.o): In function `mysql_real_connect':
client.o(.text+0x1c42): undefined reference to `socket'
client.o(.text+0x1dc5): undefined reference to `socket'
C:/QNX632/target/qnx6/x86/lib/mysql/libmysqlclient_r.a(viosocket.o): In function `vio_fastsend':
viosocket.o(.text+0x1fe): undefined reference to `setsockopt'
viosocket.o(.text+0x220): undefined reference to `setsockopt'
C:/QNX632/target/qnx6/x86/lib/mysql/libmysqlclient_r.a(viosocket.o): In function `vio_keepalive':
viosocket.o(.text+0x27a): undefined reference to `setsockopt'
C:/QNX632/target/qnx6/x86/lib/mysql/libmysqlclient_r.a(viosocket.o): In function `vio_close':
viosocket.o(.text+0x320): undefined reference to `shutdown'
C:/QNX632/target/qnx6/x86/lib/mysql/libmysqlclient_r.a(viosocket.o): In function `vio_peer_addr':
viosocket.o(.text+0x3d1): undefined reference to `getpeername'
C:/QNX632/target/qnx6/x86/lib/mysql/libmysqlclient_r.a(viosocket.o): In function `vio_timeout':
viosocket.o(.text+0x47e): undefined reference to `setsockopt'
C:/QNX632/target/qnx6/x86/lib/mysql/libmysqlclient_r.a(ssl.o): In function `TaoCrypt::Block<unsigned char, TaoCrypt::
AllocatorWithCleanup<unsigned...
View Full Message
Re: Linking a C library from C++ project  
> Here is the linker output
> 

Are you using the binary distribution of the MySQL Client 5.0? I believe that was compiled for a QNX 6.2.1 system which 
means it was compiled with a gcc 2.95.3 compiler. If you are using the binary distribution, then you will not be able to
 link your 3.3.5 project against the mysql libs as the the C++ ABI was completely changed (including the name mangling 
scheme) between 2.95.3 and 3.3.5. 

What errors did you encounter when you tried to using 2.95.3?

Regards,

Ryan Mansfield

Re: Linking a C library from C++ project  
Yes I am using the binaries and they are compiled for 6.2.1.

Switching to the 2.95 toolset removes the __pure_virtual and __buildin_delete references.

The other 'undefined references' are removed by adding socket and 'm' libraries.

So now it links and works.

Not sure why I have to add the socket and 'm' libraries with the C++ compiler and not with the C compiler?  

Thanx for the help.
Tim
Re: Linking a C library from C++ project  
> Not sure why I have to add the socket and 'm' libraries with the C++ compiler 
> and not with the C compiler?  

The C compiler doesn't implicitly link against libm/libsocket so they're not being automatically brought in. My guess is
 that some C++ code creates a reference to an object(e.g ssl.o)  in libmysqlclient_r.a that then has undefined 
references to libm/libsocket.

You can compare what objects are being pulled from the libmysqlclient.a by linking with -Wl,--verbose.

Regards,

Ryan Mansfield
Re: Linking a C library from C++ project  
Thanx for your help!