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

Tags: ,

About prres

I have been working with Microsoft Tools and Technologies for 9 years. Most of my work include architecting and developing with C#, ASP.Net, Silverlight and SQL Server.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: