【问题标题】:Passing `-std=c++11` to CMakeLists?将 `-std=c++11` 传递给 CMakeLists?
【发布时间】:2014-01-16 13:08:16
【问题描述】:

我刚刚安装了 Qt Creator,并且正在使用 C++11 语法。

不幸的是,当我尝试构建我的项目时,我得到了:

/usr/include/c++/4.8/bits/c++0x_warning.h:32: error:
      #error This file requires compiler and library support for the ISO C++ 2011
             standard. This support is currently experimental, and must be
             enabled with the -std=c++11 or -std=gnu++11 compiler options.
      #error This file requires compiler and library support for the \
       ^

然后是一堆错误,例如“tuple不是std的成员”。

我的 CMakeLists.txt 包含:

project(routing_tests)
set(QMAKE_CXXFLAGS "-std=c++11")
cmake_minimum_required(VERSION 2.8)
aux_source_directory(. SRC_LIST)
add_executable(${PROJECT_NAME} ${SRC_LIST})

编辑:显示问题的小测试用例 https://gist.github.com/anonymous/8171073

【问题讨论】:

  • 显示 cmake 输出,请连同完整的 CMakeLists.txt 文件。
  • 等一下 QMake 还是 CMake?
  • @drescherjm:正是我回答的重点。
  • 如果不设置 CMake(CMAKE_CXXFLAGS "-std=c++11")
  • 啊。我的屏幕直到现在才反映你的回答..

标签: c++ qt c++11 cmake compiler-flags


【解决方案1】:

main.cpp

#include <iostream>
#include <tuple>

int main() {
    auto foo = std::make_tuple("bar", "foo", "can");
    std::cout << std::get<0>(foo) << std::get<1>(foo) << std::get<2>(foo);
} 

CMakeLists.txt

project(tuple_tests)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
# C++14: set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++1y")
cmake_minimum_required(VERSION 2.8)
aux_source_directory(. SRC_LIST)
add_executable(${PROJECT_NAME} ${SRC_LIST})

【讨论】:

  • 啊,所以是 CMAKE 而不是 GMAKE。
  • @AT:你的意思是QMAKE,而不是GMAKE。你是今天第二个犯这个错字的人。 :)
  • @AT:你需要重新运行cmake。
  • 请尝试add_definitions("-std=c++11")
  • @drescherjm:我认为 AT 只是想编辑我的答案,但不幸的是被拒绝了。没有问题,正如要点指出的那样,现在问题已经解决了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-17
  • 2018-07-14
  • 1970-01-01
  • 2015-09-26
相关资源
最近更新 更多