2 years ago
#72241
GGS
Issues when statically linking with libmysqlclient.a. Seems to be intended for only programs written in C++
Writing a program in C. Compiles and runs just fine when linking with dynamic (shared) libraries. Need to create a version linked statically to make this program as portable as possible.
Using simple "make" in Ubuntu (ver 18). Below is the "build" part of the "makefile". The important library is "libmysqlclient.a" static library. Have used 3 methods to determine what other static libraries need to be included:
- mysql_config
- pkg-config
- ldd (technically this is for dynamic linking but worth trying to be complete)
Found several needed static libraries (see below). In researching all of this, I also found comments like:
- the order of the static libraries in the make command is important. It did fix or break attempts at building until I arrived with the order below.
- in some cases a static library may need to be included than once because of other involved static libraries. this also fixed some errors
These are the reasons why they are ordered as shown and why some (like lpthread are included more than once). I also included complete paths for the static libraries (even though there is another way to deal with that).
Ran "make" and got hundreds of errors. A large number of them like the ones below:
undefined reference to operator new(unsigned long)' undefined reference to
operator delete(void*)'
One of the actual errors looked like this:
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libmysqlclient.a(my_file.cc.o): in function MyFileInit()': (.text._Z10MyFileInitv+0x1c): undefined reference to
operator new(unsigned long)'
So the static library causing the error seems to be libmysqlclient.a
This seemed odd. These are C++ constructs and my code is just plain C, neither is ever called in my code (I use malloc and free). The compiler I specified (cc) is a simple C compiler. So as a wild guess, I added the following static libraries:
/usr/lib/gcc/x86_64-linux-gnu/9/libstdc++.a /usr/lib/gcc/x86_64-linux-gnu/9/libgcc.a
And this resolved the errors. My program now links (with a few warnings). But "seg faults" when I run the program.
Is there a "libmysqlclient.a" static library for just C? Or some other way around this problem? Or am I totally missing something else? Thanks in advance for any help.
Here's the makefile (with SOURCES and OUT sections removed due to proprietary information):
CXX = cc
CFLAGS = -Wno-unused-variable -Bstatic INCLUDE = -I/usr/local/include -I/usr/include -Iinclude -I/usr/include/mysql LDFLAGS = -L/usr/local/lib -L/usr/lib -L/usr/lib/x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/9
Here's the build part of the makefile:
$(CXX) -o $(OUT) $(INCLUDE) $(LDFLAGS) $(CFLAGS) $(SOURCES) /usr/lib/x86_64-linux-gnu/libmysqlclient.a /usr/lib/x86_64-linux-gnu/libpthread.a /usr/lib/x86_64-linux-gnu/libdl.a /usr/lib/x86_64-linux-gnu/libz.a /usr/lib/x86_64-linux-gnu/libssl.a /usr/lib/x86_64-linux-gnu/libcrypto.a /usr/lib/x86_64-linux-gnu/libpthread.a /usr/lib/x86_64-linux-gnu/libdl.a /usr/lib/x86_64-linux-gnu/libresolv.a /usr/lib/x86_64-linux-gnu/libm.a /usr/lib/x86_64-linux-gnu/librt.a /usr/lib/x86_64-linux-gnu/libc.a
mysql
c
linux
static-linking
0 Answers
Your Answer