cmake: get add_library() names

2019-06-14 11:26发布

Is there a way to get the names of all add_library() calls? E.g.

add_library(lib1
  some.cpp
)

add_library(lib2
  some.cpp
)

add_library(lib3
  some.cpp
)

# And then somehow get in a variable MY_LIBRARIES_NAMES = lib1 lib2 lib3 

Is there any cmake call or variable that does that? (I am interested in cmake version 2.8 if it matters)

Thanks!

标签: cmake
1条回答
放荡不羁爱自由
2楼-- · 2019-06-14 11:55

In newer versions of CMake (>= version 3.7) that would be the BUILDSYSTEM_TARGETS directory property:

get_directory_property(MY_LIBRARIES_NAMES BUILDSYSTEM_TARGETS)

For older versions of CMake you could overwrite the add_library() call to collect a list of targets:

macro(add_library _target)
    _add_library(${_target} ${ARGN})
    set_property(GLOBAL APPEND PROPERTY GlobalTargetList ${_target})
endmacro()

...

get_property(_allTargets GLOBAL PROPERTY GlobalTargetList)
message(STATUS "GlobalTargetList: ${_allTargets}")

References

查看更多
登录 后发表回答