Breakdown
Breakdown

Reputation: 1053

Error when static linking g++

I have a problem, i want to compile my application with static linking of mysql connector.

My command line:

g++ -o newserver stdafx.cpp ... -lboost_system -lboost_thread -lpthread -lmysqlcppconn -static /usr/lib/libmysqlcppconn-static.a -std=c++0x

But i have error:

/usr/bin/ld: cannot find -lmysqlcppconn
/tmp/ccxpOfdZ.o: In function `IsEqualsDns(unsigned long, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
Server.cpp:(.text+0x356e): warning: Using 'gethostbyname' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
collect2: ld returned 1 exit status

How can i fix this? Thanks!

Upvotes: 2

Views: 3305

Answers (2)

James Kanze
James Kanze

Reputation: 153899

Where is the library libsqlcppconn.a or libsqucppconn.so (static or dynamic)? The compiler is looking for it, and doesn't find it.

Presumably, this is the same library as /usr/lib/mysqlcppconn-static.a. If so, just drop the -lmysqlcppconn. Or just use -lmysqlcppconn-static (no spaces), and forget about the /usr/lib/libmysqlconn-static.a. With a name like that, there shouldn't be a corresponding .so, which means that g++ will link it statically, even without the -static. You only need the -static if there is both a libmysqlconn-static.so and a libmysqlconn-static.a in the same directory.

With regards to the second error (which is just a warning, but will cause problems if you try to run the linked program on other machines, or even after an upgrade of your machine): if you use -static anywhere in your command line (as you currently do), then it applies to all files linked afterwards. Including the system libraries, which you don't want to link statically. My guess is that the -static isn't necessary (see above); if it is, place it immediately before the library you want to link statically, and place a -dynamic immediately after (so that any following libraries, including the system libraries, will be dynamically linked).

Upvotes: 3

TemplateRex
TemplateRex

Reputation: 70516

You could try g++ -static YOUR ARGUMENTS.

If you are coming from a Windows platform, linking against Boost can give a few surprises. The typicall Boost installation (e.g. after ./b2 install) will make both dynamic and static libraries and put them in the same directory. Typically, the two library forms only differ in their extension (.so or .a).

Windows supports auto-linking, which basically means that library files contain some flags in their first few bytes indicating whether they are for dynamic or for static linking. On Linux platforms, this is not the case and the linker gets confused which file to load (since you don't provide the extension of the library name). Therefore, you need to tell your linker which form of linking you want.

Upvotes: 1

Related Questions