简体   繁体   English

CMAKE 如何告诉 makefile 链接库?

[英]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 .我有一个简单的可执行文件,它使用~/mylib/lib/libmylib.somylib中的函数。

On CMakeLists.txt , I tell CMAKE where to find the library and link it:CMakeLists.txt上,我告诉 CMAKE 在哪里可以找到库并链接它:

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

after doing cd build; cmake..在进行cd build; cmake.. cd build; cmake.. , the Makefile is generated, and calling make compiles it successfully. cd build; cmake.. ,生成Makefile ,调用make编译成功。


But let's say I comment the second line on CMakeLists.txt , as但是假设我在CMakeLists.txt上评论第二行,如

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

And perform the same cd build; cmake..并执行相同的cd build; cmake.. cd build; cmake.. . cd build; cmake.. I get the exact same Makefile , however make fails with these kinds of errors:我得到完全相同的 Makefile ,但是make失败并出现以下错误:

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 Makefile s are the same, why would one make fail and not the other?但是,如果两个Makefile相同,为什么一个make失败而不是另一个?

For every executable and library CMake creates link.txt script which performs the linking step.对于每个可执行文件和库 CMake 创建执行链接步骤的link.txt脚本。

This file is used in per-target build.make script via此文件用于 per-target build.make脚本通过

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM