2

In my sample C code, I use the mysqlclient to connect to a MySQL Server. Here is the Makefile.

example: example.c
    $(CC) $< -o $@ `mysql_config --cflags --libs`

It works fine. But the produced example is dynamically linked, which is not what I want. What I want do is to link against libmysqlclient statically, while linking against other libraries dynamically, such as libz, libcrypto.

FYI. mysql_config's output with --cflags --libs:

$ mysql_config --cflags --libs
-I/usr/include/mysql  -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions \
-fstack-protector --param=ssp-buffer-size=4 -m64 -D_GNU_SOURCE \
-D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -fno-strict-aliasing -fwrapv

-rdynamic -L/usr/lib64/mysql -lmysqlclient -lz -lcrypt -lnsl -lm \
-L/usr/lib64 -lssl -lcrypto
bitmask
  • 32,434
  • 14
  • 99
  • 159
Dutor
  • 483
  • 1
  • 4
  • 7

1 Answers1

0

Luckily I have some lightning to zap this dead post with. Maybe it'll live...

Your MySQL installation will come with two libraries, one shared, and one static. All you need to do is to explicitly link against the static library rather than the shared one. Unfortunately mysql_config probably can't help here, so you'll have to find it yourself, but it'll probably be in `/usr/lib/libmysqlclient.a'. So do this:

$(CC) $< -o $@ /usr/lib/libmysqlclient.a `mysql_config --cflags` \
    -lz -lcrypt ...etc...

(where ...etc... is all the rest of the output that mysql_config --libs emits.)

The .a extension indicates it's a static library; we use the full pathname, not a -lmysqlclient, to force the compiler to use the static version not the dynamic version next to it which it will normally prefer.

David Given
  • 13,277
  • 9
  • 76
  • 123