1

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 build directory 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 :)

6
  • You can provide a full path in the target_link_library and CMake will not add the -l option to it. Commented Nov 2, 2020 at 17:53
  • @Someprogrammerdude I tried hardcoding the full library path into the arguments of target_link_library and it produces the same error, even after deleting the build directory :/ Commented Nov 2, 2020 at 18:20
  • 1
    Why do you use target_link_directories? This command is needed only when you have only a name of the library. But find_library gives you an absolute path. Commented Nov 2, 2020 at 18:33
  • @Tsyvarev You are correct, it is not necessary in this case. I think it's leftover from when I had a different command that linked the library by filename only. Either way the absolute path doesn't work. Commented Nov 2, 2020 at 19:08
  • 1
    I suspect you link both by full path and by name somehow (or have a broken CMake version, that happens too). Commented Nov 2, 2020 at 20:11

1 Answer 1

3

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).

For your example you should replace ${HSI_LIB} in target_link_libraries by :${HSI_LIB}.so.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.