Which variable for compiler flags of CMake's ADD_LIBRARY function?

丶灬走出姿态 提交于 2019-12-25 09:00:21

问题


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)

Or, is there a way of getting such flags?


回答1:


Turning my comments into an answer

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)
    

References

  • 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


来源:https://stackoverflow.com/questions/42696024/which-variable-for-compiler-flags-of-cmakes-add-library-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!