Bersan
Bersan

Reputation: 3463

How does CMAKE tell the makefile to link a library?

I have a simple executable that uses functions from a library mylib at ~/mylib/lib/libmylib.so.

On CMakeLists.txt, I tell CMAKE where to find the library and link it:

find_library(MYLIB_PATH mylib HINT $ENV{HOME}/mylib/lib)
target_link_libraries (output "${MYLIB_PATH}")

after doing cd build; cmake .., the Makefile is generated, and calling make compiles it successfully.


But let's say I comment the second line on CMakeLists.txt, as

find_library(MYLIB_PATH mylib HINT $ENV{HOME}/mylib/lib)
# target_link_libraries (output "${MYLIB_PATH}")

And perform the same cd build; cmake ... I get the exact same Makefile, however make fails with these kinds of errors:

In function `Model::Model(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
Model.cpp:(.text+0x21): undefined reference to `TF_NewStatus'

It makes sense that it fails because the library is not linked. But if both Makefiles are the same, why would one make fail and not the other?

Upvotes: 5

Views: 2156

Answers (1)

Tsyvarev
Tsyvarev

Reputation: 65928

For every executable and library CMake creates link.txt script which performs the linking step.

This file is used in per-target build.make script via

$(CMAKE_COMMAND) -E cmake_link_script CMakeFiles/<target-name>.dir/link.txt

Upvotes: 7

Related Questions