Home > database >  CMake build - ld reports collect2 error, cannot find library, but paths to library are correctly set
CMake build - ld reports collect2 error, cannot find library, but paths to library are correctly set

Time:10-09

I am using CMake 3.19

I am compiling a C app that imports a third party shared library in the ${PROJECT_SOURCE_DIRECTORY}/lib folder.

Here is the relevant snippet of my CMakeLists.txt:

target_link_directories(TargetApp PUBLIC "/usr/local/lib"
                                     ${PROJECT_SOURCE_DIRECTORY}/lib )  


get_target_property(OUT TargetApp LINK_DIRECTORIES)
#message("directories:" ${OUT})                                     


target_link_libraries(TargetApp 
  "m"
  "coolfoo"
)  


get_target_property(OUT TargetApp LINK_LIBRARIES)
#message("linked libs:" ${OUT})  

The messages on the console show that the link paths are correct, and that the correct libraries are also added correctly.

Last, but not least, I used ldd to check whether there were any missing symbols in the the third party library (there were no missing symbols).

Here is the ldd output:

ldd  ../lib/coolfoo.so 
linux-vdso.so.1 (0x00007ffd63d5f000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f81966c5000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f81964d3000)
/lib64/ld-linux-x86-64.so.2 (0x00007f81969ac000)

When I run make on the CMake Makefile, I get the following error (at the link stage):

[ 16%] Linking C executable TargetApp
/usr/bin/ld: cannot find -lcoolfoo
collect2: error: ld returned 1 exit status

Why is ld not able to find my shared library? and how do I resolve this?

[[Edit]]

Output of make V=1

[Fri, 08Oct21 12:49 minime@yourbox]:build$ make V=1
/home/minime/.local/bin/cmake -S/path/to/proj -B/path/to/proj/build --check-build-system CMakeFiles/Makefile.cmake 0
/home/minime/.local/bin/cmake -E cmake_progress_start /path/to/proj/build/CMakeFiles /path/to/proj/build//CMakeFiles/progress.marks
make  -f CMakeFiles/Makefile2 all
make[1]: Entering directory '/path/to/proj/build'
make  -f CMakeFiles/TargetApp.dir/build.make CMakeFiles/TargetApp.dir/depend
make[2]: Entering directory '/path/to/proj/build'
cd /path/to/proj/build && /home/minime/.local/bin/cmake -E cmake_depends "Unix Makefiles" /path/to/proj /path/to/proj /path/to/proj/build /path/to/proj/build /path/to/proj/build/CMakeFiles/TargetApp.dir/DependInfo.cmake --color=
make[2]: Leaving directory '/path/to/proj/build'
make  -f CMakeFiles/TargetApp.dir/build.make CMakeFiles/TargetApp.dir/build
make[2]: Entering directory '/path/to/proj/build'
[ 16%] Linking C executable TargetApp
/home/minime/.local/bin/cmake -E cmake_link_script CMakeFiles/TargetApp.dir/link.txt --verbose=1
/usr/bin/cc CMakeFiles/TargetApp.dir/src/server/example-server.c.o CMakeFiles/TargetApp.dir/src/server/dirlookup.pb-c.c.o CMakeFiles/TargetApp.dir/src/server/query.pb-c.c.o -o TargetApp   -L/usr/local/lib  -L/path/to/proj/lib  -Wl,-rpath,/usr/local/lib:/path/to/proj/lib -lm -lprotobuf-c -lprotobuf-c-rpc -lcoolfoo 
/usr/bin/ld: cannot find -lcoolfoo
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/TargetApp.dir/build.make:151: TargetApp] Error 1
make[2]: Leaving directory '/path/to/proj/build'
make[1]: *** [CMakeFiles/Makefile2:95: CMakeFiles/TargetApp.dir/all] Error 2
make[1]: Leaving directory '/path/to/proj/build'
make: *** [Makefile:103: all] Error 2

CodePudding user response:

On Linux when you ask the linker to link with library coolfoo (with e.g. -lcoolfoo) the linker will look for a file named libcoolfoo.so.

The lib prefix is mandatory when looking for libraries using the -l option.

Rename (or build) your library as libcoolfoo.so and the linker should find it. Or use the full path to the actual library file (${PROJECT_SOURCE_DIRECTORY}/lib/coolfoo.so).

  • Related