In CMake, is there a way to specify that all my executables links to some library? Basically I want all my executables link to tcmalloc
and 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.
In CMake, specify all executables target_link_libraries certain libraries
You can override the built-in add_executable
function with your own which always adds the required link dependencies:
macro (add_executable _name)
# invoke built-in add_executable
_add_executable(${ARGV})
if (TARGET ${_name})
target_link_libraries(${_name} tcmalloc profiler)
endif()
endmacro()
I wasn't aware you could override the built-in commands. Good to know. –
Nussbaum
I just read in the
tcmalloc
documentation that it must come at the end of the linker list, and just specifying the dependency with target_link_libraries
does not guarantee that condition. If it is not at the end of the link list, then there will be false leak reports. –
Alloy You can write a function/macro in CMake that does the work for you.
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.
It works, but kind of hacky. I need to replace every
add_executable
with setup
. Is there some variable that I can set globally to achieve this? –
Churchill You can modify the CMAKE_EXE_LINKER_FLAGS variable (check CMakeCache.txt in your build directory). Personally I'd say that's more of a hack than switching to a function, but if you have a significant number of executables in your project that's the faster solution. –
Nussbaum
© 2022 - 2024 — McMap. All rights reserved.