Resolving undefined references with MySQL C++ Connector - c++

I'm trying to compile this (also listed in the mysql c++ connector documentation): http://pastebin.com/HLv4zR0r
But I get these errors: http://pastebin.com/3t0UbeFy
This is how I tried compiling:
g++ -o test test.cpp `mysql_config --cflags --libs` -I./include/cppconn -L./lib -lmysqlcppconn-static
The result of running mysql_config --cflags --libs is:
-I/usr/include/mysql -DBIG_JOINS=1 -fno-strict-aliasing -g
-L/usr/lib/x86_64-linux-gnu -lmysqlclient -lpthread -lz -lm -lrt -ldl
Edit:
After running Jonathan Wakely's suggested command with properly-ordered linker arguments,
g++ -o test test.cpp -I./include/cppconn -L./lib -lmysqlcppconn-static `mysql_config --cflags --libs`
I get different errors: http://pastebin.com/4EWNgy9i

The mysqlcppcon library depends on the mysqlclient C libraries, so you need to put the mysqlclient libs after -lmysqlcppconn-static
g++ -o test test.cpp -I./include/cppconn -L./lib -lmysqlcppconn-static `mysql_config --cflags --libs`
The order of linker arguments matters. The linker looks at each file in order and decides if it needs any symbols from it. By the time it sees the libmysqlcppconn-static.a file it has already looked at (and ignored) the libmysqlclient.so library, and doesn't go back to look at it again.

Related

how to link specific version of shared library in g++

I have the following shared libraries:
libssl.so, libssl.so.1.1, libcurl.so, libcurl.so.4, libcurl.so.4.4.0, libcrypto.so, libcrypto.so.1.1.
All of the libraries are in the openssl folder.
My question is, how can I link version 1.1 of libssl? Is it done automatically?
I've tried the following:
g++ -c my_file_name.cpp -std=c++11 -w -fpermissive -lpthread --coverage $(INCLUDES) `pkg-config --cflags glib-2.0` `pkg-config --libs glib-2.0` -O0 -Lopenssl -lcrypto -lcurl -lssl
g++ my_file_name.o -o ex -std=c++11 -w -fpermissive -lpthread --coverage -lgtest -lgtest_main -lpthread `pkg-config --cflags glib-2.0` `pkg-config --libs glib-2.0` -O0 -Lopenssl -lcrypto -lcurl -lssl
But it seems that the link doesn't happen. As I still get errors like:
error: ‘X509_STORE_CTX_get0_chain’ was not declared in this scope
Later edit
nm libssl.so.1.1 | grep X509_STORE_CTX_get0_chain results in 0000000000218210 T X509_STORE_CTX_get0_chain. That would mean that the link that I've done does not happen.
It's worth mentioning that the error comes from a .c file included in the .cpp file.
Probably the libssl.so is a symbolic link to libssl.so.1.1 etc. etc.
The problem seems to be related to the missing implementation in one of these library of this function :X509_STORE_CTX_get0_chain.
Now you have to check if this symbol is defined in one of these library you link, or if you need to link someone else.
To check this you can execute the following command over each library:
nm libToCheck.so | grep X509_STORE_CTX_get0_chain
if no one have this symbol defined maybe you missing some library to link.
If exist, check the scope of the utilizzation of the function, it could not correspond to the scope declared in the library. Check the namespace or similar.

Compiling with -static causes undefined references to functions in other libraries

I'm trying to statically link glibc in order to run my application on an older OS, but when I use the -static flag I get "undefined reference" errors for other libraries I'm using that I don't get without using -static. How do I fix this issue?
My Makefile produces the following commands:
g++ -static -Wall -O3 -w -std=c++11 -I/storage/home/PA/libs -I/storage/home/PA/libs/xerces -fopenmp -c Utilities.cpp
gcc -static -Wall -O3 -w -std=c++11 -I/storage/home/PA/libs -I/storage/home/PA/libs/xerces -fopenmp -c ccvt.c
gcc -static -Wall -O3 -w -std=c++11 -I/storage/home/PA/libs -I/storage/home/PA/libs/xerces -fopenmp -c client.c
g++ -static -Wall -O3 -w -std=c++11 -I/storage/home/PA/libs -I/storage/home/PA/libs/xerces -fopenmp -c XML_Params.cpp
g++ -static -Wall -O3 -w -std=c++11 -I/storage/home/PA/libs -I/storage/home/PA/libs/xerces -fopenmp -c main.cpp
g++ -static -Wall -O3 -std=c++11 -L/storage/home/PA/libs/gsl -fopenmp -lgsl -lgslcblas -lm -L/storage/home/PA/libs/xerces -lxerces-c -o App main.o Utilities.o XML_Params.o ccvt.o client.o
After the last line I get a huge wall of errors complaining about undefined references to Xerces and gsl functions. However, if I remove the -static from the makefile, everything builds fine. What is the proper way to link these libraries when I'm using -static?
according to gcc manual:
-llibrary
It makes a difference where in the command you write this option; the
linker searches and processes libraries and object files in the order
they are specified. Thus, foo.o -lz bar.o searches library z after
file foo.o but before bar.o. If bar.o refers to functions in z,
those functions may not be loaded.
Move -lxerces after *.o might solve your problem.
I think you don't need to add -static except for the last line, correct me if i'm wrong.

C++ Shared library gives floating point exception when linked with C application

Background:
A C++ library for etcd client having APIs to communicate with etcd server, to use this library in C application, we wrote C++ wrappers over this library so that it can be called by C application.
Created a shared library using the below command:
g++-7 -ggdb -fPIC -shared -o libetcd_c++.so etcd_client_wrapper.cc etcd_client_txn_wrapper.cc etcd_client.cc etcd_client_txn.cc utils/string.cc pb/*.cc -std=c++1z -I ./pb/etcd -rdynamic -Wl,-call_shared -lglog -lprotobuf -lgrpc++ -lgrpc -Wl,-call_shared -lpthread -ldl -lc
placed this library to default library path /usr/local/lib and load the library with sudo ldconfig.
Now using this etcd_c++ library APIs, wrote a C code to insert simple key-value to etcd keyspace. Compiled using below command:
gcc -ggdb -o cwrap sample_wrapper.c -rdynamic -pthread -static-libstdc++ -Wl,-non_shared -lglog -lprotobuf -pthread -lz -lgrpc++ -lprotobuf -lgrpc -lz -lcares -lssl -lcrypto -lunwind -llzma -lgflags -Wl,-call_shared -lpthread -ldl -letcd_c++ -lstdc++
The compilation goes fine. But while executing the resulted binary, it gives floating point exception in grpc++ library.
Questions:
What floating point exception has to do with the library?
We thought it might be an issue with C to C++ transition, but when converted the same C code to C++ with same wrapper API. Gives the floating point exception.Now If we replace the wrapper API with direct grpc++ library API, in the C++ code, it works fine. Is it a linking issue?
Compiling a C++ application:
g++-7 -ggdb -o wrap example.cc -std=c++1z -rdynamic -pthread -static-libstdc++ -Wl,-non_shared -lglog -lprotobuf -pthread -lz -lgrpc++ -lprotobuf -lgrpc -lz -lcares -lssl -lcrypto -lunwind -llzma -lgflags -Wl,-call_shared -lpthread -ldl -letcd_c++
EDIT: Some findings, https://bugs.launchpad.net/ubuntu/+source/grpc/+bug/1797000
Though We are not using -Wl,-Bsymbolic-functions option, but the issue is somewhat similar to above.

Reordering gnu autotools linker flags

I'm using gnu autotools with my project in C++ (the autotools config is automatically generated by eclipse cdt, but this does not matter i think). I'm using LLVM libs and right now I'm facing a problem with order of linker flags.
Basically, when building a project, eclipse executes "make". Make executes a lot of commands, but lastly it executes g++ compiler as follows:
g++ -DPACKAGE_NAME=\"test\" -DPACKAGE_TARNAME=\"test\" -DPACKAGE_VERSION=\"1.0\" -DPACKAGE_STRING=\"test\ 1.0\" -DPACKAGE_BUGREPORT=\"\" -DPACKAGE_URL=\"\" -DPACKAGE=\"test\" -DVERSION=\"1.0\" -I. `llvm-config --cppflags --ldflags --libs core` -g -O2 -MT test.o -MD -MP -MF .deps/test.Tpo -c -o test.o test.cpp
mv -f .deps/test.Tpo .deps/test.Po
and then the linker:
g++ `llvm-config --cppflags --ldflags --libs core` -g -O2 -o a.out test.o
The problem is, that the linker fails if the argument "test.o" is not on the beginning of line, so it should be:
g++ test.o `llvm-config --cppflags --ldflags --libs core` -g -O2 -o a.out
How to change it in Makefile.am or any config file for gnu autotools?
I didn't found the answer, but so far I have found out that LLVM is bundled with a project template containing custom automake system, which handles all the stuff the correct way.
If you want to access the template, you can find it in $LLVMSRC/projects/sample folder.

swig mysql: undefined symbol: mysql_init'

I am trying to create a swig package for python that offers mysql connectivity. But when I try to import the package in python I get the following error:
-> _mod = imp.load_module('_IMysqlConnection', fp, pathname, description)
(Pdb) s
ImportError: './_IMysqlConnection.so: undefined symbol: mysql_init'
I am creating the package using the following commands (probably this is where I am making a blunder)
$swig -c++ -python -o IMysqlConnection_wrap.cc IMysqlConnection.i
$ gcc -fPIC -c IMysqlConnection_wrap.cc -o IMysqlConnection_wrap.o -I /usr/include/python2.6/ `mysql_config --cflags` `mysql_config --libs`
g$ gcc -fPIC -c IMysqlConnection.cc -o IMysqlConnection.o -I . /usr/include/python2.6/ `mysql_config --cflags` `mysql_config --libs`
$ g++ -shared IMysqlConnection_wrap.o IMysqlConnection.o -o _IMysqlConnection.so
I am able to do mysql connectivity when I use this class (IMysqlConnection.cc) in C++ main.
Any help and guidance will me much appreciated.
You can use
http://sourceforge.net/projects/mysql-python/
This does not exactly answer your connection though.
Solved: I was missing mysql_config --cflags mysql_config --libs while creating the shared library _IMysqlConnection.so
g++ -shared CMysqlConnection_wrap.o CMysqlConnection.o -o _CMysqlConnection.so mysql_config --cflags mysql_config --libs
Above command linked mysql library to the shared library _IMysqlConnection.so and it worked fine.
Thanks Ugo for your help :-)