PDA

View Full Version : linker cannot find shared library on Ubuntu



feraudyh
28th September 2017, 20:04
Hi,
I usually develop in Windows, so when trying to compile in Linux I feel like a total newbie.
I have a link problem in a project, so to simplify my problem to the maximum, I created a console project with just a main.cpp
which besides the user boilerplate code contains an include to ftrScanApi.h (a finger scanner's api header file)
and a single call to the scanner initialisation routine ftrScanOpenDevice(); The file main.cpp links
The problem is that there is a linker problem.
The project file contains

LIBS += -L/the/absolute/path/to/the/library -lScanAPI.so
and the shared object library supplied is libScanAPI.so
ls reports it's a file that takes 180K, so it does not seem to be a link to another file.

and despite the simplicity of all of this I have the message

cannot find -lScanAPI.so
collect2: error: ld returned 1 exit status

Now the file is obviously there, so what could go wrong?

Hi,
I usually develop in Windows, so when trying to compile in Linux I feel like a total newbie.
I have a link problem in a project, so to simplify my problem to the maximum, I created a console project with just a main.cpp
which besides the user boilerplate code contains an include to ftrScanApi.h (a finger scanner's api header file)
and a single call to the scanner initialisation routine ftrScanOpenDevice(); The file main.cpp links
The problem is that there is a linker problem.
The project file contains

LIBS += -L/the/absolute/path/to/the/library -lScanAPI.so
and the shared object library supplied is libScanAPI.so
ls reports it's a file that takes 180K, so it does not seem to be a link to another file.

and despite the simplicity of all of this I have the message

cannot find -lScanAPI.so
collect2: error: ld returned 1 exit status

Now the file is obviously there, so what could go wrong?

Added after 1 38 minutes:

If I type

LIBS += /the/absolute/path/to/the/library/libScanAPI.so
then the linker problem goes away.
If I want a relative path, then I must take into account that the linker is being called inside a directory that may or may not be inside the sources, and I need to account for that...

d_stranz
28th September 2017, 23:36
Try it this way: -lScanAPI

In linux, the "lib" prefix will be prepended to the front of the filename, and the appropriate ".lib", ".a", or ".so" will be appended. With your syntax, it is probably looking for libScanAPI.so.something, and of course can't find it.

And I learned this the hard way: unlike in Windows, where you can specify libraries in any order to the linker, you must specify the libraries in linux in order of their dependencies. So if libA depends on libB, then you must tell the linker first about libA, then about libB. If you do it in the reverse order, then you'll get all kinds of unresolved dependencies from libA evn though you have also specified libB. The linker only drags in symbols it is -currently- looking for and then forgets about the library. If a library that comes afterward depends on the first library, too bad.

For such a popular OS, linux toolchains seem quite stupid at times.