2013-01-01 8 views
22

मैं निम्नलिखित C++ प्रोग्राम को संकलित करने का प्रयास कर रहा हूं जो C++ 11 <thread> शीर्षलेख पर निर्भर करता है। मैं ओएसएक्स शेर पर ऐसा करने की कोशिश कर रहा हूं।ओएसएक्स शेर पर क्लैंग 3.2 के साथ सी ++ 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; 
} 

उपर्युक्त प्रोग्राम g ++ 4.7 के साथ ठीक से संकलित करता है जिसे मैंने होमब्री पैकेज मैनेजर का उपयोग करके स्थापित किया है। लेकिन जब बजना 3.2 के साथ उपरोक्त कार्यक्रम संकलन करने की कोशिश कर रहा निम्न त्रुटि संदेश मिलता है (यह भी homebrew पैकेज प्रबंधक का उपयोग कर स्थापित):

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> हैडर नहीं मिल सकता है, लेकिन मुझे यकीन है कि क्यों नहीं कर रहा हूँ।

संपादित करें: मैं नीचे sharth का जवाब करने की कोशिश की और अब मैं त्रुटि है कि <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

आप सिस्टम क्लैंग का उपयोग नहीं कर रहे हैं, आप होमब्रू क्लैंग का उपयोग कर रहे हैं। किसी भी कारण से, यह libC++ पर/usr/include/C++/v1 पर देख रहा है, जो वह स्थान है जिसे वे लिनक्स पर स्थापित करने के लिए अनुशंसा करते हैं। ओएस एक्स पर, यह/usr/lib/C++/v1 में संग्रहीत है, जहां सिस्टम क्लैंग दिखता है। –

+0

हां, मैंने उल्लेख किया कि मेरी टिप्पणी में। क्या मैं libC++ और होमब्रू क्लैंग का उपयोग नहीं कर सकता? –

संबंधित मुद्दे