0

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?

6
  • @user same error: /usr/bin/ld: cannot find -labc: No such file or directory. I tried abc.so ,abc, libabc.so, libabc.
    – user5819540
    Commented Mar 25, 2023 at 2:09
  • @user Problem solved! THANKS! The ln -s path should be absolute. My ${shared} is absolute path. I didn't make that clear.
    – user5819540
    Commented Mar 25, 2023 at 2:15
  • @user sorry. I have updated the question to show where ${share} is. The issue was about ln not link_diretories. It works before I use ln.
    – user5819540
    Commented Mar 25, 2023 at 2:24
  • uhh am I blind or have you not shown what the value of ${shared} is? Now I'm just very confused.
    – starball
    Commented Mar 25, 2023 at 2:27
  • 1
    You can also use a relative path but it must be relative to where the symbolic link is Commented Mar 25, 2023 at 2:48

1 Answer 1

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.