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

问题描述 投票:0回答:2

在 CMake 中,有没有办法指定我的所有可执行文件链接到某个库?基本上我希望所有可执行文件都链接到

tcmalloc
profiler
。简单地指定
-ltcmalloc
-lprofiler
并不是一个好的解决方案,因为我想让 CMake 以可移植的方式找到库的路径。

c++ c cmake linker tcmalloc
2个回答
11
投票

您可以使用自己的函数覆盖内置

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()

1
投票

您可以在 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)

查看文档以获取更多信息。

© www.soinside.com 2019 - 2024. All rights reserved.