Linking with CMakeLists: ld cannot find library

前端 未结 2 1871
孤街浪徒
孤街浪徒 2021-01-19 12:25

I have a CMakeLists.txt file, with the following:

target_link_libraries(${PROJECT_NAME} OpenNI2)

When I run cmake

相关标签:
2条回答
  • 2021-01-19 12:42

    it works if adding like:

    target_link_libraries(${PROJECT_NAME} /path_to_library_build/libOpenNI2.a)
    

    details:

    ld is looking for the libraries in a very short list of folders defined in

    /etc/ld.so.conf
    

    and it usually looks like following:

    include /etc/ld.so.conf.d/*.conf
    

    and actual paths list from those *.conf files usually is like:

    # Legacy biarch compatibility support
    /lib32
    /usr/lib32
    # Multiarch support
    /usr/local/lib/x86_64-linux-gnu
    /lib/x86_64-linux-gnu
    /usr/lib/x86_64-linux-gnu
    

    if your project linking library is not in the folder of this list, ld won't find it unless either a special linking variable set LD_LIBRARY_PATH with the path to your library or a complete path/library name provided in cmake target_link_libraries directive.

    details on how to proper setup LD_LIBRARY_PATH variable discussed here

    0 讨论(0)
  • 2021-01-19 12:55

    That's because when linking, the linker doesn't look in the current directory but only in a set of predefined directories.

    You need to tell CMake where the library is, for example by giving the full path to the library in the target_link_library command, or adding it as an imported library.

    0 讨论(0)
提交回复
热议问题