PDA

View Full Version : Adding a shared library to a qt application



bravestar
26th April 2017, 15:13
Hi,
I am trying to add an external library to a qt application. But after building the application its showing cannot recognize file format. Library is in .so format and I am working on linux. Please help.

high_flyer
26th April 2017, 16:10
Not sure I follow.

But after building the application
It sound like you can build the application.
If this is true, then your lib is linked as it should, otherwise the linker would complain.

I guess what is not so clear is what do you mean with "adding" the lib.
More information would help.

It sound more like your are trying the open an *.so file with your application.
Is that the case?

bravestar
27th April 2017, 08:25
Currently the application has some cpp files which are included in the pro file. (It will give a successful build)
Instead what I am trying to do is to add those cpp files as a shared library.(On trying to build will show file format not recognised build failed)

Added after 1 43 minutes:

Please help me on how to add the cpp files as a shared library to a qt application.

high_flyer
27th April 2017, 11:36
Ok, what you want is to build a shared lib.
You should do it in a separate project, either a completely separate project, or a sub-project of the one you have now.
If you are using QtCreator simply do: File->New File or project
Then select "Library" from the possible options on the left, and and C++ library on the right side.
Also have a look at this:
https://wiki.qt.io/How_to_create_a_library_with_Qt_and_use_it_in_an_a pplication

bravestar
2nd May 2017, 14:50
Thanks that really helped!
Is there anyway to know which all shared libraries are linked to an application in qt?

high_flyer
2nd May 2017, 16:16
If you have the source code, and this is a qmake project (using *.pro files) you can look for the 'LIBS' variables in your *.pro/*.pri files.
Uder linux you can use the ldd command on the executable : http://man7.org/linux/man-pages/man1/ldd.1.html
For windows, you can use dependency walker: http://www.dependencywalker.com/

bravestar
9th May 2017, 08:44
Thanks
Now the shared library has been made.
I am making the shared library for a qt application which has to run on a target board. How will I link the shared library to the executable as it is running on a board.

bravestar
9th May 2017, 11:03
Please help

d_stranz
9th May 2017, 18:02
If your executable is running on a board, then you have built it using a cross-compiler tool chain for that board, right? Your shared library has to be built with the same cross-compiler tool chain and symbolically linked into the executable (that is, the executable has been told that there are functions it will be calling that can be found in a shared library of a certain name).

You now need to install the shared library into the board's OS. Most of the time this is done by copying the file into a known location in the file system used by the board - the same folder as the executable or a known location for shared libraries.

If you are directly downloading the executable into the board's memory (like with an Arduino sketch), then you're going to have to consult the Arduino manuals for how to add this.

The question is, if you are running this on an embedded board, why do you need a shared library in the first place?

high_flyer
10th May 2017, 10:21
@bravestar
What OS is running on your board?
If it is some sort of linux, you can put your shared lib in a path of your choice, and add that path to LD_LIBRARY_PATH. (in addition to putting it next to the application executable as d_stranz said)
The linker will look the path for the libs from that system variable.
Note: if your shared lib is doing write operations, or other operations that need elevated rights, and the application you are running is a user space application, make sure the location of your shared lib is not a location needing elevated rights.

bravestar
11th May 2017, 08:38
Thanks a lot.
I have added the LD_LIBRARY PATH, and tried to run the application. Its not working. Do I have to use subdirs for handling dependencies.

Added after 8 minutes:

@high_flyer OS is linux

high_flyer
11th May 2017, 15:31
show the ldd output on your executable

bravestar
16th May 2017, 14:10
Thank you very much guys. I tried making libraries using cmake and it worked!