2013-01-01 72 views
22

我想编译下面的C++程序,它依赖于C++ 11 <thread>头文件。我正在尝试在OSX Lion上执行此操作。如何在OSX狮子上使用clang 3.2编译C++ 11?

#include <iostream> 
#include <thread> 
#include <vector> 

void hello() 
{ 
    std::cout << "Hello from thread " << std::this_thread::get_id() << std::endl; 
} 

int main() 
{ 
    std::vector<std::thread> threads; 

    for(int i = 0; i < 5; i++) 
    { 
     threads.push_back(std::thread(hello)); 
    } 

    for(auto& thread: threads) 
    { 
     thread.join(); 
    } 

    return 0; 
} 

上述程序编译罚款与我使用自制包管理器安装克++ 4.7。然而试图编译铿锵3.2以上程序时,我收到以下错误信息(也使用自制包管理器安装):

Zameers-MacBook-Air:tmp zmanji$ clang++ -v -std=c++11 test.cpp 
clang version 3.2 (tags/RELEASE_32/final) 
Target: x86_64-apple-darwin11.3.0 
Thread model: posix 
"/usr/local/Cellar/llvm/3.2/bin/clang" -cc1 -triple x86_64-apple-macosx10.7.0 -emit-obj -mrelax-all -disable-free -main-file-name test.cpp -mrelocation-model pic -pic-level 2 -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 128.2 -v -resource-dir /usr/local/Cellar/llvm/3.2/bin/../lib/clang/3.2 -fmodule-cache-path /var/folders/qf/j_7_sw0n093gn1y0mtsyztxh0000gn/T/clang-module-cache -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /Users/zmanji/tmp -ferror-limit 19 -fmessage-length 101 -stack-protector 1 -mstackrealign -fblocks -fobjc-runtime=macosx-10.7.0 -fobjc-dispatch-method=mixed -fobjc-default-synthesize-properties -fencode-extended-block-signature -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -o /var/folders/qf/j_7_sw0n093gn1y0mtsyztxh0000gn/T/test-Zkjucl.o -x c++ test.cpp 
clang -cc1 version 3.2 based upon LLVM 3.2svn default target x86_64-apple-darwin11.3.0 
ignoring nonexistent directory "/usr/include/c++/4.2.1/i686-apple-darwin10/x86_64" 
ignoring nonexistent directory "/usr/include/c++/4.0.0" 
ignoring nonexistent directory "/usr/include/c++/4.0.0/i686-apple-darwin8/" 
ignoring nonexistent directory "/usr/include/c++/4.0.0/backward" 
#include "..." search starts here: 
#include <...> search starts here: 
/usr/include/c++/4.2.1 
/usr/include/c++/4.2.1/backward 
/usr/local/include 
/usr/local/Cellar/llvm/3.2/bin/../lib/clang/3.2/include 
/usr/include 
/System/Library/Frameworks (framework directory) 
/Library/Frameworks (framework directory) 
End of search list. 
test.cpp:2:10: fatal error: 'thread' file not found 
#include <thread> 
     ^
1 error generated. 

似乎铛找不到<thread>头,但我不知道为什么。

编辑: 我试过下面沙尔斯的答案,现在我得到了<iostream>标头不存在错误。

Zameers-MacBook-Air:tmp zmanji$ clang++ -v -std=c++11 -stdlib=libc++ test.cpp 
clang version 3.2 (tags/RELEASE_32/final) 
Target: x86_64-apple-darwin11.3.0 
Thread model: posix 
"/usr/local/Cellar/llvm/3.2/bin/clang" -cc1 -triple x86_64-apple-macosx10.7.0 -emit-obj -mrelax-all -disable-free -main-file-name test.cpp -mrelocation-model pic -pic-level 2 -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 128.2 -v -resource-dir /usr/local/Cellar/llvm/3.2/bin/../lib/clang/3.2 -fmodule-cache-path /var/folders/qf/j_7_sw0n093gn1y0mtsyztxh0000gn/T/clang-module-cache -stdlib=libc++ -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /Users/zmanji/tmp -ferror-limit 19 -fmessage-length 203 -stack-protector 1 -mstackrealign -fblocks -fobjc-runtime=macosx-10.7.0 -fobjc-dispatch-method=mixed -fobjc-default-synthesize-properties -fencode-extended-block-signature -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -o /var/folders/qf/j_7_sw0n093gn1y0mtsyztxh0000gn/T/test-k2Alf4.o -x c++ test.cpp 
clang -cc1 version 3.2 based upon LLVM 3.2svn default target x86_64-apple-darwin11.3.0 
ignoring nonexistent directory "/usr/local/Cellar/llvm/3.2/bin/../lib/c++/v1" 
ignoring nonexistent directory "/usr/include/c++/v1" 
#include "..." search starts here: 
#include <...> search starts here: 
/usr/local/include 
/usr/local/Cellar/llvm/3.2/bin/../lib/clang/3.2/include 
/usr/include 
/System/Library/Frameworks (framework directory) 
/Library/Frameworks (framework directory) 
End of search list. 
test.cpp:1:10: fatal error: 'iostream' file not found 
#include <iostream> 
     ^
1 error generated. 
+0

我相信这是'-pthread'和'-std = C++ 11' – Rapptz

回答

43

您需要使用libc++,而不是libstdc++

clang++ -std=c++11 -stdlib=libc++ foo.cc 
+0

这并不为我工作。查看我编辑的错误消息,我现在正在获取。 –

+3

你没有使用系统铿锵声,你正在使用自制铛声。无论出于何种原因,它都在/ usr/include/C++/v1上寻找libC++,这是他们推荐在Linux上安装它的位置。在OS X上,它存储在/ usr/lib/C++/v1中,这是系统铿锵声的外观。 –

+0

是的,我在我的评论中提到过。我不能使用libC++和homebrew clang吗? –

相关问题