本文介绍了哪个变量用于CMake的ADD_LIBRARY函数的编译器标志?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否存在一个变量,该变量包含对CMake的ADD_LIBRARY函数的某些调用中使用的编译器标志,例如,当我们添加模块时使用的那些标志:

Does exist a variable which contains the compiler flags used in some call to CMake's ADD_LIBRARY function, for example the ones used when we add a module:

ADD_LIBRARY(mylib MODULE mysrc.cpp)

或者,有没有办法

推荐答案

将我的评论变成答案

没有一个CMake变量来获取所有编译器标志。问题在于,CMake生成器最终将把编译器标志放在一起(来自各种CMake变量和属性,包括来自依赖目标的属性)。因此,在配置步骤中您无需掌握所有标志。

There is not a single CMake variable to get the all compiler flags. The problem is that the CMake generator will finally put together the compiler flags (from various CMake variables and properties incl. from depending targets). So you don't have all the flags during configuration step.

我看到以下可能的问题/解决方案对:

I see the following possible problem/solution pairs:


  • CMake是一个跨编译器的跨包装器(实际上就是 C 的意思),因此无需将编译器标志提取到外部脚本中

  • 如果您只想为CMake调用添加某种过滤器,则可以相应地设置启动器变量/属性,例如或

  • 如果您只想查看编译器调用含税您可以设置的所有标志

  • 如果您真的只需要输出中的编译器标志,并且您不希望CMake实际编译任何内容,则可以-至少对于CMake而言Makefile生成器-修改和像这样:

  • CMake is a cross-platform wrapper around your compiler (that's actually what the C stands for), so no need to extract the compiler flags into an external script
  • If you just want to add sort of a filter to what is called by CMake you can user set "launcher" variables/properties accordingly e.g. CMAKE_CXX_COMPILER_LAUNCHER or RULE_LAUNCH_LINK
  • If you want the compiler calls in a machine readable JSON format you could export those by setting CMAKE_EXPORT_COMPILE_COMMANDS
  • If you just want to see the compiler calls incl. all the flags you could set CMAKE_VERBOSE_MAKEFILE
  • If you really just need the compiler flags on the output and you don't want CMake to actually compile anything, you could - at least for CMake's Makefile generators - modify CMAKE_CXX_COMPILE_OBJECT and CMAKE_CXX_CREATE_SHARED_MODULE like this:

set(CMAKE_DEPFILE_FLAGS_CXX "")
set(
    CMAKE_CXX_COMPILE_OBJECT
    "<CMAKE_COMMAND> -E echo <FLAGS>"
)
set(
    CMAKE_CXX_CREATE_SHARED_MODULE
    "<CMAKE_COMMAND> -E echo <CMAKE_SHARED_MODULE_CXX_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_MODULE_CREATE_CXX_FLAGS>"
)

file(WRITE mysrc.cpp "")
add_library(mylib MODULE mysrc.cpp)


参考文献







  • Is Cmake set variable recursive?
  • What does the "c" in cmake stand for?
  • How to use CMAKE_EXPORT_COMPILE_COMMANDS?
  • Using CMake with GNU Make: How can I see the exact commands?
  • Retrieve all link flags in CMake

这篇关于哪个变量用于CMake的ADD_LIBRARY函数的编译器标志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 18:48