phemmer
phemmer

Reputation: 8846

CMake target_link_libraries converting path which results in linker "not found" error

I'm using CMake to build a module for an external application. As part of the build, I'm also generating an executable which links against the module to run tests. However as part of module convention for the external application, the module name should not have the lib prefix. This results in the following CMakeLists.txt:

add_library(mymodule SHARED mymodule.c)
set_target_properties(mymodule PROPERTIES PREFIX "")

add_executable(mytest mytest.c)
target_link_libraries(mytest ${CMAKE_BINARY_DIR}/mymodule.so)

When building, CMake is converting the path to a search name, which results in the error:

/usr/bin/ld: cannot find -lmymodule

If I run cmake . again (without changing anything) after mymodule.so is built, it switches from using -lmymodule to the actual path of mymodule.so, and it works. If I remove the PROPERTIES PREFIX "", it also works.

Since I'm specifying the mymodule.so path within target_link_libraries(), how can I get CMake to stop converting it into search name (-lmymodule)?

Upvotes: 0

Views: 854

Answers (1)

ixSci
ixSci

Reputation: 13718

You should link against targets, not files if those targets are part of your project. In your case it would be target_link_libraries(mytest mymodule).

But if for some reason the need arises to link against a full path, CMake has some peculiarities which are described in the docs. So in your case it converts the full path to -lmymodule because it is a behavior CMake exhibits if the full path provided to the library w/o proper soname.

Upvotes: 4

Related Questions