哪个变量用于CMake的ADD_LIBRARY函数的编译器标志? [英] Which variable for compiler flags of CMake's ADD_LIBRARY function?

查看:134
本文介绍了哪个变量用于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 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函数的编译器标志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆