条件CXX_FLAGS使用cmake基于编译器? [英] Conditional CXX_FLAGS using cmake based on compiler?

查看:817
本文介绍了条件CXX_FLAGS使用cmake基于编译器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用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")

编译器,但是这似乎不能正确工作 - 它完全删除 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变量

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屋!

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