CMake build - ld 报告 collect2 错误,找不到库,但正确设置了库的路径

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

我正在使用 CMake 3.19

我正在编译一个 C 应用程序,它在 ${PROJECT_SOURCE_DIRECTORY}/lib 文件夹中导入第三方共享库。

这是我的 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})  

控制台上的消息显示 link 路径是正确的,并且正确的库也已正确添加。

最后但同样重要的是,我使用ldd检查第三方库中是否有任何丢失的符号(没有丢失的符号)。

这里是 ldd 输出:

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)

当我在 CMake Makefile 上 运行 make 时,出现以下错误(在 link 阶段):

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

为什么 ld 找不到我的共享库?我该如何解决?

[[编辑]]

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

在 Linux 上,当您要求 linker 使用库 coolfoo(例如 -lcoolfoo)link 时,linker将查找名为 libcoolfoo.so.

的文件

使用 -l 选项查找库时,必须使用 lib 前缀。

将您的库重命名(或构建)为 libcoolfoo.so,link 用户应该可以找到它。或者使用实际库文件的完整路径 (${PROJECT_SOURCE_DIRECTORY}/lib/coolfoo.so).