[英]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中看到它们,但是:
make VERBOSE=1
instead of standard make
make VERBOSE=1
而不是标准make
来查看命令行 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中选中“高级”来找到) CACHE FORCE
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.