[英]CMake linker cannot find library that does not begin with "lib"
I am using CMake to compile an application which uses the HSImage library on github.我正在使用 CMake 编译一个使用 github 上的HSImage库的应用程序。 After installing with pip, the HSI library generates a shared library file, in my case it is created at
/usr/src/HSI/HSI.cpython-36m-aarch64-linux-gnu.so
使用 pip 安装后,HSI 库生成一个共享库文件,在我的例子中它是在
/usr/src/HSI/HSI.cpython-36m-aarch64-linux-gnu.so
创建的
I am trying to link this library to my application with CMake, but the CMake find_library
method is having some trouble finding the library.我正在尝试使用 CMake 将此库链接到我的应用程序,但是 CMake
find_library
方法在查找库时遇到了一些麻烦。 Here is the relevant part of my CMakeLists.txt file:这是我的 CMakeLists.txt 文件的相关部分:
CMakeLists.txt CMakeLists.txt
set(HSI_DIR /usr/src/HSI)
find_library(HSI_LIB HSI.cpython-36m-aarch64-linux-gnu PATHS ${HSI_DIR})
message(STATUS "HSI: ${HSI_LIB}") # outputs /usr/src/HSI/HSI.cpython-36m-aarch64-linux-gnu.so
add_executable(${TARGET_NAME} <sources...>)
target_link_directories(${TARGET_NAME} PUBLIC ${HSI_DIR})
target_link_libraries(${TARGET_NAME}
${HSI_LIB}
<other libs...>
-Wl,--unresolved-symbols=ignore-in-shared-libs
)
When building, this produces the following error message:构建时,这会产生以下错误消息:
cd /home/nvidia/projects/HsiInference/build;/usr/local/bin/cmake --build "/home/nvidia/projects/HsiInference/build" --target hsi_inference_onnx -- ;
Scanning dependencies of target hsi_inference_onnx
[ 14%] Building CXX object CMakeFiles/hsi_inference_onnx.dir/targets/HsiInferenceOnnx/main_onnx.cpp.o
[ 28%] Building CXX object CMakeFiles/hsi_inference_onnx.dir/targets/HsiInferenceOnnx/HsiInferenceOnnx.cpp.o
[ 42%] Building CXX object CMakeFiles/hsi_inference_onnx.dir/src/ftpnano.cpp.o
[ 57%] Building CXX object CMakeFiles/hsi_inference_onnx.dir/src/getOptions.cpp.o
[ 71%] Building CXX object CMakeFiles/hsi_inference_onnx.dir/src/logger.cpp.o
[ 85%] Building CXX object CMakeFiles/hsi_inference_onnx.dir/src/utils.cpp.o
[100%] Linking CXX executable hsi_inference_onnx_debug
CMakeFiles/hsi_inference_onnx.dir/build.make:245: recipe for target 'hsi_inference_onnx_debug' failed
CMakeFiles/Makefile2:123: recipe for target 'CMakeFiles/hsi_inference_onnx.dir/all' failed
**/usr/bin/ld: cannot find -lHSI.cpython-36m-aarch64-linux-gnu**
**collect2: error: ld returned 1 exit status**
make[3]: *** [hsi_inference_onnx_debug] Error 1
make[2]: *** [CMakeFiles/hsi_inference_onnx.dir/all] Error 2
CMakeFiles/Makefile2:130: recipe for target 'CMakeFiles/hsi_inference_onnx.dir/rule' failed
make[1]: *** [CMakeFiles/hsi_inference_onnx.dir/rule] Error 2
Makefile:196: recipe for target 'hsi_inference_onnx' failed
make: *** [hsi_inference_onnx] Error 2
Build failed.
The important part:重要的部分:
/usr/bin/ld: cannot find -lHSI.cpython-36m-aarch64-linux-gnu
collect2: error: ld returned 1 exit status
From what I have gathered, target_link_libraries
simply adds -l<library_name>
to the link command, and -l<library_name>
assumes that there is a file called lib<library_name>.so
to link, which is not the case here.从我收集到的信息来看,
target_link_libraries
只是将-l<library_name>
添加到链接命令中,而-l<library_name>
假设有一个名为lib<library_name>.so
的文件进行链接,但此处并非如此。 How can I get CMake to link the library properly despite the weird filename?尽管文件名很奇怪,我怎样才能让 CMake 正确链接库?
NOTE: I am able to get the project to build by doing the following:注意:我可以通过执行以下操作来构建项目:
build
directory to clear CMake cachesbuild
目录以清除 CMake 缓存libhsi.so
libhsi.so
的符号链接find_library(HSI_LIB hsi PATHS ${HSI_DIR})
find_library(HSI_LIB hsi PATHS ${HSI_DIR})
This changes the link command to -lhsi
instead, which is able to find the renamed/soft-linked library file.这将链接命令改为
-lhsi
,它能够找到重命名/软链接的库文件。 HOWEVER, this is not ideal for me and the original question remains unanswered :)但是,这对我来说并不理想,原始问题仍未得到解答:)
For libraries with weird filename you should add :
before the filename.对于具有奇怪文件名的库,您应该在文件名之前添加
:
。 Be careful, like mentioned in https://linux.die.net/man/1/ld : If namespec is of the form :filename, ld will search the library path for a file called filename, otherwise it will search the library path for a file called libnamespec.a (namespec is what comes after -l).小心,就像在https://linux.die.net/man/1/ld 中提到的:如果 namespec 的形式是 :filename,ld 将在库路径中搜索名为 filename 的文件,否则它将搜索库路径对于名为 libnamespec.a 的文件(namespec 是 -l 之后的内容)。
For your example you should replace ${HSI_LIB}
in target_link_libraries by :${HSI_LIB}.so
.对于您的示例,您应该将
${HSI_LIB}
中的${HSI_LIB}
替换${HSI_LIB}
:${HSI_LIB}.so
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.