【问题标题】:CMAKE, Clang and C++v11 on OS X 10.8OS X 10.8 上的 CMAKE、Clang 和 C++v11
【发布时间】:2012-08-11 22:38:03
【问题描述】:

OS X 1.8

CMAKE 2.8.9

叮当$ clang -v Apple clang version 4.0 (tags/Apple/clang-421.10.60) (based on LLVM 3.1svn) Target: x86_64-apple-darwin12.0.0 Thread model: posix

CMAKELists.txt:

cmake_minimum_required (VERSION 2.8.9)
project (Test)
add_executable(Test main.cpp)

main.cpp

//Create a C++11 thread from the main program
#include <iostream>
#include <thread>

//This function will be called from a thread
void call_from_thread() {
    std::cout << "Hello, World!" << std::endl;
}

int main() {
    //Launch a thread
    std::thread t1(call_from_thread);

    //Join the thread with the main thread
    t1.join();

    return 0;
}

我的错误:

$ make
Scanning dependencies of target Test
[100%] Building CXX object CMakeFiles/Test.dir/main.cpp.o
test/main.cpp:4:10: fatal error: 'thread' file not found
#include <thread>
     ^
1 error generated.
make[2]: *** [CMakeFiles/Test.dir/main.cpp.o] Error 1
make[1]: *** [CMakeFiles/Test.dir/all] Error 2
make: *** [all] Error 2

那么 Clang 的版本不支持 C++v11 的特性吗?同样的程序在 OSX 10.8 上的 gcc-4.7.1 下编译

这个参考说它应该可以工作http://www.cpprocks.com/a-comparison-of-c11-language-support-in-vs2012-g-4-7-and-clang-3-1/

我做错了什么?

【问题讨论】:

  • 您是否将-std=c++11 -stdlib=libc++ 添加到编译器标志中?

标签: macos compiler-construction c++11 compiler-errors clang


【解决方案1】:

您需要向编译器提供-std=c++11-stdlib=libc++ 标志才能完全激活其C++11 支持。这可以通过 ccmake(打开高级模式(使用 t),并将 CMAKE_CXX_FLAGS 设置为 -std=c++11 -stdlib=libc++)或通过 CMakeLists.txt 中的等效指令来完成:

cmake_minimum_required(VERSION 2.8.9)
set(CMAKE_CXX_FLAGS "-std=c++11 -stdlib=libc++")
project(Test)
add_executable(Test main.cpp)

【讨论】:

  • 谢谢:set(CMAKE_CXX_FLAGS "-std=c++11 -stdlib=libc++")
  • 那还是不行。选项被传递给编译器,“make VERBOSE=1”验证了这一点,但是 C++11 类,例如 std::thread 或 std::function 是未定义的,尽管包含了 。跨度>
  • @Michael 你找到解决方案了吗?
  • @aledalgrande:也许你还需要set(CMAKE_EXE_LINKER_FLAGS "-stdlib=libc++")
  • set(CMAKE_CXX_FLAGS "-stdlib=libc++") 对我有用
猜你喜欢
  • 2013-02-15
  • 1970-01-01
  • 2013-05-03
  • 2013-03-03
  • 1970-01-01
  • 2012-11-08
  • 1970-01-01
  • 1970-01-01
  • 2013-01-02
相关资源
最近更新 更多