Tag Archive | QT

C++ user library path for Linux

I have just started to learn programming in C++ for Linux platform. I am using QT to create the shared library project and tried to link it with the QT GUI application. As I came from Windows platform, so I was assuming that I will compile the library and it will generate some (.a) file, just like the (.dll) file on Windows platform. But my first mistake was that I started with “Shared LIbrary” project and compiling it doesn’t generate a (.a) file and I didn’t know about the (.so) files. Well it took me quite a while to learn about it through googling but I was glade to finally fix the linking of static and shared linked library on Linux.
In the example below, I will be using the following library file name and path.

Library Name: libTestLogic
Library Extensions: (.a) for static library and (.so) for shared library
Library Path: /home/MyUser/TestPro/TestLogicProject

Static Library Reference in QT .pro file
Open the .pro file of QT GUI App and add following lines. It will be enough to use the static file:

INCLUDEPATH += /home/MyUser/TestPro/TestLogicProject
LIBS += -L/home/MyUser/TestPro/TestLogicProject -lTestLogic

Shared Library
Open the .pro file of QT GUI App and add following lines. This is same as static library:

INCLUDEPATH += /home/MyUser/TestPro/TestLogicProject
LIBS += -L/home/MyUser/TestPro/TestLogicProject -lTestLogic

then we need to open the “MakeFile” in the GUI app folder and find the line for “-rpath”. If it already has library path then add a “:” and the shared library path there. An example of this will be:

LFLAGS        = -Wl,-rpath,/home/MyUser/qtsdk2010/qt/lib:/home/MyUser/TestPro/TestLogicProject

This -rpath is passed to gcc to link the .so and .so.1 files.

Another option for shared library is to specify this library path in the ld.so.conf. That will make sure that we don’t have to modify the MAKE file every time when we reference our same shared library from more projects.

sudo /etc/ld.so.conf

after adding the path to library on a new line in that file, save the file and running following command to configure the linker run-time bindings.

sudo ldconfig

Configure MySQL Database Driver for QT

I am working on a QT project on Ubuntu and I needed to use the MySql database driver for it. In previous versions of QT, it used to be the default installed library but with QT 2010.2 installation (4.6 and above), it is not the default installation. So, it requires to configure the MySql header files and then configure the QT to use the file. Here are the steps which worked for me.

1. First of all, we need to install the MySql Deveoper Libraries on Ubuntu. This will be required by QT to compile the MySql driver.

sudo apt-get install libmysqlclient15-dev

2. After this installation is successful. It will create the folder “/usr/incluce/mysql” in the Ubuntu installation and it will also install the lib files in “/usr/lib/mysql”. We will need this to compile the QT MySql driver:

cd $QTDIR/src/plugins/sqldrivers/mysql
qmake "INCLUDEPATH+=/usr/include/mysql" "LIBS+=-L/usr/lib/mysql -lmysqlclient_r" mysql.pro
make

3. After this, just need to compile the QT code using the “QMYSQL” driver.