简体   繁体   English

在CMake中,指定所有可执行文件target_link_libraries某些库

[英]In CMake, specify all executables target_link_libraries certain libraries

In CMake, is there a way to specify that all my executables links to some library? 在CMake中,有没有办法指定我的所有可执行文件都链接到某个库? Basically I want all my executables link to tcmalloc and profiler. 基本上我希望我的所有可执行文件都链接到tcmalloc和profiler。 Simply specify -ltcmalloc and -lprofiler is not a good solution because I want to let CMake find the paths to the library in a portable way. 简单地指定-ltcmalloc和-lprofiler不是一个好的解决方案,因为我想让CMake以可移植的方式找到库的路径。

You can override the built-in add_executable function with your own which always adds the required link dependencies: 您可以使用自己的内置add_executable函数覆盖它,它总是添加所需的链接依赖项:

macro (add_executable _name)
    # invoke built-in add_executable
    _add_executable(${ARGV})
    if (TARGET ${_name})
        target_link_libraries(${_name} tcmalloc profiler)
    endif()
endmacro()

You can write a function/macro in CMake that does the work for you. 你可以在CMake中编写一个函数/宏来为你工作。

function(setup name sources
add_executable(name sources)
target_link_library(name tcmalloc profiler)
endfunction(setup)
setup(foo foo.c)
setup(bar bar.c)

Check out the documentation for more information. 查看文档以获取更多信息。

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

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