在 CMake 中,有没有办法指定我的所有可执行文件链接到某个库?基本上我希望所有可执行文件都链接到
tcmalloc
和 profiler
。简单地指定 -ltcmalloc
和 -lprofiler
并不是一个好的解决方案,因为我想让 CMake 以可移植的方式找到库的路径。
您可以使用自己的函数覆盖内置
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()
您可以在 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)
查看文档以获取更多信息。