Search code examples
c++cmakesymlink.so

CMake cannot find .so which is symbolic link file


Platform: ubuntu22.04 I'm using cmake to link to a 3rd party library. It looks like:

// Those two files are identical. 
// abc.so is the original file and libabc.so is a copy

// ${shared} is an absolute path
${shared}/libabc.so
${shared}/abc.so

My CMakeLists.txt:

LINK_DIRECTORIES(${shared})

ADD_EXECUTABLE(main ...)

TARGET_LINK_LIBRARIES(main libabc.so)

So far it works fine. You may have noticed that I have ${shared}/libabc.so and ${shared}/abc.so at the same time. They are identical. So I delete libabc.so (abc.so is the original file) and create a link file with ln -s relative_path/shared/abc.so relative_path/shared/libabc.so. But I got:

/usr/bin/ld: cannot find -labc: No such file or directory
collect2: error: ld returned 1 exit status

Why do I get this error when libabc.so did exists?


Solution

  • Unless you ran ln -s shared/abc.so shared/libabc.so from the root directory (with current working directory of shell in root directory), it should have instead been ln -s /shared/abc.so /shared/libabc.so. You want absolute paths and not relative paths.

    Not sure if this applies or not according to your ${shared} variable, note from the docs for link_directories:

    Adds the paths in which the linker should search for libraries. Relative paths given to this command are interpreted as relative to the current source directory

    Also, I believe your target_link_libraries(main libabc.so) can be target_link_libraries(main abc), as documented in the target_link_library docs in the "plain library name" form of <item> parameters.