条件CXX_FLAGS使用cmake基于编译器? [英] Conditional CXX_FLAGS using cmake based on compiler?
问题描述
我刚刚开始在一些个人和学校的项目中使用CMake,我已经被一个小问题所困扰。
I've just started using CMake for some personal and school projects, and I've been stumped by a minor issue.
我想试试获得在多个编译器(在这种情况下为g ++,cl和bcc32)下的C ++程序编译。我有不同的命令行开关为每个编译器,我正在尝试做的是基本上做一个gnu / ms / borland目录,并在那里创建CMake的东西(通过输入目录并做一个 cmake - DCMAKE_CXX_COMPILER = g ++
..在gnu目录中)。
Let's say I'm trying to get a C++ program compiling under multiple compilers (g++, cl, and bcc32 in this case). I have different command line switches for each compiler, and what I was attempting to do was to basically make a gnu/ms/borland directory and create CMake stuff in there (by entering the directories and doing a cmake -DCMAKE_CXX_COMPILER=g++
.. in the gnu, directory, for instance).
在顶层的CMakeLists.txt目录,我尝试按照以下行做一些事情:
In the CMakeLists.txt on the top level directory, I tried doing something along the lines of:
if(CMAKE_CXX_COMPILER STREQUAL g++)
set(CMAKE_CXX_FLAGS "-Wextra -Wall -ansi -pedantic")
等都与elsifs为其他编译器,但是这似乎不能正确工作 - 它完全删除 CXXFLAGS
。如果我使文件完全无条件(即,只是假设g ++和使用g ++标志。)行。工作。
And so on with elsifs for the other compilers, but this doesn't seem to work correctly -- it drops the CXXFLAGS
entirely. The line works if I make the file completely unconditional (ie, just assume g++ and use g++ flags.).
我在这里做错了,如何处理这种问题?
What am I doing wrong here, or is there a better way to handle this sort of a problem?
推荐答案
有一堆预定义的CMake变量,取决于您使用的编译器:
There are a bunch of pre-defined CMake variables depending on the compiler you're using:
if (MSVC)
set ( CMAKE_CXX_FLAGS "/GLOBAL_FLAGS_GO_HERE")
set ( CMAKE_CXX_FLAGS_DEBUG "/DEBUG_FLAGSS_GO_HERE")
set ( CMAKE_CXX_FLAGS_RELEASE "/RELEASE_FLAGS_GO_HERE" )
endif ()
if (BORLAND)
set ( CMAKE_CXX_FLAGS "/GLOBAL_FLAGS_GO_HERE")
set ( CMAKE_CXX_FLAGS_DEBUG "/DEBUG_FLAGS_GO_HERE")
set ( CMAKE_CXX_FLAGS_RELEASE "/RELEASE_FLAGS_GO_HERE" )
endif ()
if (CMAKE_COMPILER_IS_GNUCXX)
set ( CMAKE_CXX_FLAGS "/GLOBAL_FLAGS_GO_HERE")
set ( CMAKE_CXX_FLAGS_DEBUG "/DEBUG_FLAGS_GO_HERE")
set ( CMAKE_CXX_FLAGS_RELEASE "/RELEASE_FLAGS_GO_HERE" )
endif ()
如果你希望你的编译器选项覆盖在生成的CMakeCache坚持:
If you want your compiler options to override and persist in the generated CMakeCache:
if (CMAKE_COMPILER_IS_GNUCXX)
set ( CMAKE_CXX_FLAGS "/GLOBAL_FLAGS_GO_HERE"
CACHE STRING "g++ Compiler Flags for All Builds" FORCE)
set ( CMAKE_CXX_FLAGS_DEBUG "/DEBUG_FLAGS_GO_HERE"
CACHE STRING "g++ Compiler Flags for Debug Builds" FORCE)
set ( CMAKE_CXX_FLAGS_RELEASE "/RELEASE_FLAGS_GO_HERE"
CACHE STRING "g++ Compiler Flags for Release Builds" FORCE)
endif ()
这篇关于条件CXX_FLAGS使用cmake基于编译器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!