I am using CMake to compile an application which uses the HSImage library on github. 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
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. Here is the relevant part of my CMakeLists.txt file:
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. How can I get CMake to link the library properly despite the weird filename?
NOTE: I am able to get the project to build by doing the following:
- Delete the project's
builddirectory to clear CMake caches - Rename the file or create a symbolic link to
libhsi.so - Change CMakeLists.txt to
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. HOWEVER, this is not ideal for me and the original question remains unanswered :)
target_link_libraryand CMake will not add the-loption to it.target_link_libraryand it produces the same error, even after deleting thebuilddirectory :/target_link_directories? This command is needed only when you have only a name of the library. Butfind_librarygives you an absolute path.