0

I'm working on a CMake project which builds a shared object, let's call it libfoo.so which has to be dynamically linked to another shared object libbar.so. This later one is not built from files contained in the project itself but rather just copied into the build directory from an external source during the invocation of cmake. To make CMake aware of this file I've tried doing the following:

add_library(bar SHARED IMPORTED GLOBAL)

set_target_properties(bar PROPERTIES
  IMPORTED_LOCATION some/path/to/libbar.so
)

And then to build libfoo.so I do (in another CMakeLists.txt in an unrelated directory):

add_library(foo SHARED ...)
target_link_libraries(foo bar) 

However, this does not work as desired, when running make VERBOSE=1 after cmake the output shows that libfoo.so is linked with -lbar instead of libbar.so.

If necessary I can try creating a minimal working example but maybe someone has encountered the same problem and can tell me what's going wrong.

Peter
  • 2,919
  • 1
  • 16
  • 35
  • Seems to me that everything is working fine. If the full name is `libbar.so` and you see that `libfoo.so` links to `bar`, then it is correct. Try `readelf -d libfoo.so` to see if `libbar.so` is in the `DT_NEEDED` section. – thomas_f Aug 12 '20 at 11:24
  • I don't think it's working as intended, I have already inspected `libfoo.so` with `objdump -p` and `libbar.so` is not listed as adynamic depedency. Also if I instead call `target_link_libraries(foo some_non_existent_target)` CMake will still try to link with `-lsome_non_existent_target`. – Peter Aug 12 '20 at 11:28
  • Any error messages we should be aware of then? Are you actually getting link errors or runtime errors? – thomas_f Aug 12 '20 at 11:35
  • Ah yes, linking fails because `-lbar` can't be found, I should probably add that. I think the problem is that `bar` is not a recognized target at the point where I call `target_link_libraries`. But I don't understand why this is the case, I assumed that the `GLOBAL` flag would make the target `bar` available throughout the whole project. – Peter Aug 12 '20 at 11:39
  • `set_target_properties` seems to be wrong. You are supposed to set a full path to target but your example shows a relative path. Can you post the actual command? – fdk1342 Aug 12 '20 at 12:11
  • Does this answer your question? [cmake doesn't support imported libraries?](https://stackoverflow.com/questions/15813746/cmake-doesnt-support-imported-libraries) – fdk1342 Aug 12 '20 at 12:15
  • Does this answer your question? [cmake:missing and no known rule to make it when I import a prebuilt library](https://stackoverflow.com/questions/41478323/cmakemissing-and-no-known-rule-to-make-it-when-i-import-a-prebuilt-library) This duplicate question may address the issue more directly. – Kevin Aug 12 '20 at 13:16

0 Answers0