问题
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