swig mysql: undefined symbol: mysql_init' - c++

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 :-)

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.

Failure to link v8 in a native Dart extension

I am trying to use v8 in a Dart native extension.
The v8 getting started guide says to compile the hello world example like this.
g++ -I. -Iinclude samples/hello-world.cc -o hello-world -Wl,--start-group \
out.gn/x64.release/obj/{libv8_{base,libbase,external_snapshot,libplatform,libsampler},\
third_party/icu/libicu{uc,i18n},src/inspector/libinspector}.a \
-Wl,--end-group -lrt -ldl -pthread -std=c++0x
Dart says to compile native extensions like so:
g++ -fPIC -I{path to SDK include directory} -DDART_SHARED_LIB -c sample_extension.cc
gcc -shared -m32 -Wl,-soname,libsample_extension.so -o libsample_extension.so sample_extension.o
This is the hybrid I came up with
g++ -fPIC -I{path to SDK include directory} -Iinclude -DDART_SHARED_LIB -c sample_extension.cc -std=c++0x
gcc -shared -Wl,-soname,libsample_extension.so -Wl,--start-group out.gn/x64.release/obj/{libv8_{base,libbase,external_snapshot,libplatform,libsampler},third_party/icu/libicu{uc,i18n},src/inspector/libinspector}.a -Wl,--end-group -o libsample_extension.so sample_extension.o -lrt -ldl -pthread -std=c++0x
However, while trying to run my application, I get an error stating that v8 is not linked properly.
dart: symbol lookup error: /mnt/c/Users/zvacu/Documents/Code/Dart/require/libsample_extension.so: undefined symbol: _ZN2v82V828InitializeICUDefaultLocationEPKcS2_
I can manage to link it properly when using the hello world example provided.
Doing a little research it seems like the problem it is on the -shared property on the second command. You need to pay attention with C++ and shared libraries, so check if your library get's all its dependencies by:
ldd /mnt/c/Users/zvacu/Documents/Code/Dart/require/libsample_extension.so
After this you will get a list of all dependencies, then you will need to search if there is anyone missing.
If this does not answer your question, see this related answer:
Undefined symbol when loading a shared library

Cross Compile OpenCV C++ application ARM

I have read all of the related questions with no success trying anything mentioned anywhere. I am new to cross-compiling and have been working on this for over a week with no progress. So please forgive me if you think I am stupid or have overlooked something.
So I have an application running in C++ that works great on my development computer running Ubuntu 14.04 x64. I am trying to cross compile for my Banana Pro running Lubuntu. Based on the documentation from Lemaker I am supposed to cross compile using "arm-linux-gnueabihf-"
So far the farthest I have been able to get is to :
/usr/local/opencv-arm/usr/local/lib/libopencv_calib3d.so: file not recognized: File format not recognized
collect2: error: ld returned 1 exit status
I get this error regardless of what command I run, Here is a list of commands I have tried:
arm-linux-gnueabihf-g++ `arm-linux-gnueabihf-pkg-config arm-opencv --cflags` -Wall test.cpp -o vis-300 `arm-linux-gnueabihf-pkg-config arm-opencv --libs`
arm-linux-gnueabihf-g++ `pkg-config arm-opencv --cflags` -Wall test.cpp -o vis-300 `pkg-config arm-opencv --libs`
arm-linux-gnueabihf-gcc `pkg-config arm-opencv --cflags` -Wall test.cpp -o vis-300 `pkg-config arm-opencv --libs`
arm-linux-gnueabihf-g++ `pkg-config arm-opencv --cflags` test.cpp -o vis-300 `pkg-config arm-opencv --libs`
And there have been many more commands before those with different errors such as:
arm-linux-gnueabihf-gcc: error trying to exec 'cc1plus': execvp: No such file or directory
arm-linux-gnueabihf-cpp fatal error too many input files
I have tried with just normal arm-linux-gnueabihf-gcc/g++, 4.6, 4.7, and 4.8
I have built opencv making small changes for hf using these 2 guides and both produced the same results:
http://processors.wiki.ti.com/index.php/Building_OpenCV_for_ARM_Cortex-A8
http://www.ridgesolutions.ie/index.php/2013/05/24/building-cross-compiling-opencv-for-linux-arm/
and not included in either I install it using this command because it will conflict with my current x86_64 install:
sudo make install DESTDIR=/usr/local/opencv-arm
Also the above pkg-config lines point to my custom pkg config file named arm-opencv.pc
# Package Information for pkg-config
prefix=/usr/local/opencv-arm/usr/local
exec_prefix=${prefix}
libdir=
includedir_old=${prefix}/include/opencv
includedir_new=${prefix}/include
Name: OpenCV-arm
Description: Open Source Computer Vision Library
Version: 2.4.10
Libs: ${exec_prefix}/lib/libopencv_calib3d.so ${exec_prefix}/lib/libopencv_contrib.so ${exec_prefix}/lib/libopencv_core.so ${exec_prefix}/lib/libopencv_features2d.so ${exec_prefix}/lib/libopencv_flann.so ${exec_prefix}/lib/libopencv_gpu.so ${exec_prefix}/lib/libopencv_highgui.so ${exec_prefix}/lib/libopencv_imgproc.so ${exec_prefix}/lib/libopencv_legacy.so ${exec_prefix}/lib/libopencv_ml.so ${exec_prefix}/lib/libopencv_nonfree.so ${exec_prefix}/lib/libopencv_objdetect.so ${exec_prefix}/lib/libopencv_ocl.so ${exec_prefix}/lib/libopencv_photo.so ${exec_prefix}/lib/libopencv_stitching.so ${exec_prefix}/lib/libopencv_superres.so ${exec_prefix}/lib/libopencv_ts.a ${exec_prefix}/lib/libopencv_video.so ${exec_prefix}/lib/libopencv_videostab.so -lrt -lpthread -lm -ldl
Cflags: -I${includedir_old} -I${includedir_new}
Anyways I have tried a lot of stuff short of just installing everything on the board itself and compiling there. Any help is much appreciated and keep in mind I have never successfully cross-compiled before. I always give up and compile on the board.

Resolving undefined references with MySQL C++ Connector

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.

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.