Kan Li
Kan Li

Reputation: 8797

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? 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.

Upvotes: 8

Views: 4426

Answers (2)

sakra
sakra

Reputation: 65751

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

Upvotes: 11

Stephen Newell
Stephen Newell

Reputation: 7828

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.

Upvotes: 1

Related Questions