OS X 10.8上的CMAKE,Clang和C ++ v11

时间:2012-08-15 15:47:07

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

OS X 1.8

CMAKE 2.8.9

Clang $ 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/

我做错了什么?

1 个答案:

答案 0 :(得分:25)

您需要向编译器提供-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)