简体   繁体   English

CMakeLists.txt中的编译器标志不会出现在CMake-Gui或CMakeCache.txt中

[英]Compiler Flags from CMakeLists.txt don't appear in CMake-Gui or CMakeCache.txt

I just started to learn CMake and thought I would have understood the basic process of first writing the CMakeLists.txt , then configuring to generate the CMakeCache.txt and at the end generating the Makefiles . 我刚开始学习CMake并认为我会理解首先编写CMakeLists.txt的基本过程,然后配置生成CMakeCache.txt并在最后生成Makefiles

However, when I try to apply it to the following CMakeLists.txt, I'm not getting the expected results and I'm not sure what is going wrong. 但是,当我尝试将它应用于以下CMakeLists.txt时,我没有得到预期的结果,我不确定出了什么问题。 Part of the CMakeLists.txt looks like this: CMakeLists.txt的一部分如下所示:

# compiler flags
if (CMAKE_COMPILER_IS_GNUCXX)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -fpermissive -Wall -Wformat-security")
    if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 4.8)
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-local-typedefs")
    endif()
endif()

Since I'm using gcc/g++ 4.7.3, the compiler flags from the first if-statement should be set. 由于我使用的是gcc / g ++ 4.7.3,因此应该设置第一个if语句的编译器标志。 But if I configure this with CMake-Gui, there are no compiler flags pre-defined whatsoever. 但是如果我使用CMake-Gui配置它,则没有任何预定义的编译器标志。 The same happens when I out-comment the if-statements and just keep the set(CMAKE_CXX_FLAGS ...) . 当我对if语句进行out-comment并保留set(CMAKE_CXX_FLAGS ...)发生同样的情况。 When searching the CMakeCache.txt for any -std=c++11 flags, I don't get any results, too. CMakeCache.txt搜索任何-std=c++11标志时,我也没有得到任何结果。

Why does this happen? 为什么会这样? What's the point of specifying compiler flags inside the CMakeLists.txt when they aren't used? 在不使用CMakeLists.txt时,在CMakeLists.txt中指定编译器标志有什么意义? Or am I getting something completely wrong and they are used, but then I don't know why and how I could check. 或者我得到了一些完全错误的东西而且他们被使用了,但后来我不知道为什么以及如何检查。

When generating the actual (Eclipse CDT) project with make and importing it to Eclipse, I'm getting error messages that C++11 features can't be resolved, the __cplusplus macro contains the value 199711 so the -std=c++11 flag is obviously not used. 使用make生成实际(Eclipse CDT)项目并将其导入Eclipse时,我收到无法解析C ++ 11功能的错误消息, __cplusplus宏包含值199711因此-std=c++11显然没有使用-std=c++11旗。

The flags you specified in the CMakeLists.txt file are probably correctly used by the compiler. 您在CMakeLists.txt文件中指定的标志可能已被编译器正确使用。 You can't see them directly in CMakeCache.txt but: 你不能直接在CMakeCache.txt中看到它们,但是:

  1. You can see command lines by running make VERBOSE=1 instead of standard make 您可以通过运行make VERBOSE=1而不是标准make来查看命令行
  2. Also, you can set CMAKE_VERBOSE_MAKEFILE to 1 to enable printing of commands (this can be found by checking "Advanced" in CMake GUI) 此外,您可以将CMAKE_VERBOSE_MAKEFILE设置为1以启用命令打印(可以通过在CMake GUI中选中“高级”来找到)
  3. As @Angew said, if you really want to see the updated flags in the CMake GUI, set your variables with CACHE FORCE 正如@Angew所说,如果您真的想在CMake GUI中看到更新的标志, 请使用CACHE FORCE设置变量

As an example, i use this kind of configuration in a project for some month, and never had problem: 作为一个例子,我在一个项目中使用这种配置一个月,从来没有问题:

if(MSVC) # MSVC compiler (Win32 only)
    # Display more warnings
    set(CMAKE_CXX_FLAGS "/W3")
elseif(UNIX OR CMAKE_COMPILER_IS_GNUCXX) # Clang OR Gcc (Linux, Mac OS or Win32 with MingW)
    # Enable C++11 and displays all warnings
    set(CMAKE_CXX_FLAGS "-Wall -std=c++11")
    if(APPLE) # Clang / Mac OS only
        # Required on OSX to compile c++11
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++ -mmacosx-version-min=10.7")
    endif(APPLE)
endif()

Update : 更新

Starting with CMake 3.0, you can replace set(CMAKE_CXX_FLAGS "...") by add_compile_options(-std=c++11) 从CMake 3.0开始,您可以用add_compile_options(-std=c++11)替换set(CMAKE_CXX_FLAGS "...") add_compile_options(-std=c++11)

CMake 3.1 introduced a new syntax to configure the compiler with specific C++ version: CMake 3.1引入了一种新的语法来配置具有特定C ++版本的编译器:

set(CMAKE_CXX_STANDARD 11)

You can first, set the variable to a value only if it is not in cache already. 您可以先将变量设置为仅在缓存不在缓存中的值。 The last parameter is the description which we don't need since we'll override it anyway. 最后一个参数是我们不需要的描述,因为无论如何我们都会覆盖它。

set(VARIABLE "Hello World!" CACHE STRING "")

Then force the value into cache using its existing value from the line above. 然后使用上面一行中的现有值将值强制进入缓存。 Since that is cached, users can still change the variable and it won't be set back every time. 由于这是缓存的,用户仍然可以更改变量,并且每次都不会被设置。

set(VARIABLE ${VARIABLE} CACHE STRING "Description." FORCE)

This is a bit hacky in CMake as you can see, but it works reliably. 你可以看到,这在CMake中有点笨拙,但它可靠地工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM