In CMake, specify all target_link_libraries executable files for specific libraries

In CMake, is there a way to indicate that all of my executables are associated with a library? Basically, I want all of my executables to reference tcmalloc and the profiler. Just specifying -ltcmalloc and -lprofiler is not the best solution, because I want CMake to find the library paths in a portable way.

+5
source share
2 answers

You can override the built-in function add_executablewith your own, which always adds the necessary 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()
+10
source

You can write a function / macro in CMake that does your work.

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

For more information, contact

+1

All Articles