2012-08-15 78 views
8

OS X 1.8CMake的,鏘和C++在OS V11 X 10.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功能嗎?此相同的程序不能下的gcc-4.7.1編譯在OSX 10.8

此引用說,它應該工作http://www.cpprocks.com/a-comparison-of-c11-language-support-in-vs2012-g-4-7-and-clang-3-1/

我在做什麼錯?

+1

你有沒有加入'-std = C++ 11 -stdlib = libC++'給編譯器標誌? – 2012-08-15 15:56:27

回答

23

您需要將-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) 
+0

謝謝:'set(CMAKE_CXX_FLAGS「-std = C++ 11 -stdlib = libC++」) ' – Jason 2012-08-15 16:01:46

+1

這仍然不起作用。這些選項被傳遞給編譯器,「make VERBOSE = 1」證實,儘管包含,但是諸如std :: thread或std :: function之類的C++ 11類未定義。 – Michael 2014-03-20 12:03:13

+0

@Michael你找到解決方案嗎? – aledalgrande 2014-05-22 20:41:31