本文介绍了cmake:获取add_library()名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以获取所有 add_library()调用的名称?例如,

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

有没有做过的cmake调用或变量? (如果感兴趣,我对cmake 2.8版感兴趣)

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

谢谢!

推荐答案

在较新版本的CMake(> =版本3.7)中,该版本为目录属性:

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

get_directory_property(MY_LIBRARIES_NAMES BUILDSYSTEM_TARGETS)

对于较旧版本的CMake,您可以覆盖 add_library()调用以收集目标列表:

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}")

参考


  • 使CMake Visual Studio中的所有项目都依赖于一个项目


  • Making all projects in CMake Visual Studio depend on one project
  • CMake: How do I change properties on subdirectory project targets?

这篇关于cmake:获取add_library()名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 23:45