2013-01-09 13 views
5

के साथ मैं अपनी भाषा के संकलन को तेज करने के लिए सी ++ 11 धागे का उपयोग करना चाहता था (हाँ मैं एक कंपाइलर बना रहा हूं: x)। मैंने पहले नमूने को क्लैंग (3.3 एसवीएन) के साथ कई त्रुटियों को फेंकने की कोशिश की। यह जीसीसी (4.6.3) के तहत ठीक संकलित।सी ++ 11 धागे क्लैंग

मैंने llvm.org के एसवीएन से क्लैंग और libC++ डाउनलोड किया। क्लैंग जीसीसी (4.6.3) के साथ संकलित किया गया था और libC++ क्लैंग के साथ संकलित किया गया था। दोनों मेकफ़ाइल सीएमके के साथ उत्पन्न हुए थे। http://libcxx.llvm.org/

कोड का टुकड़ा मैं संकलित करने के लिए चाहते हैं (foobar.cpp):

#include <iostream> 
#include <thread> 

using namespace std; 

int main(void) { 
    thread t1([](void)->void { 
     cout << "foobar" << endl; 
    }); 
} 

यह ठीक संकलित http://llvm.org/docs/GettingStarted.html#checkout

libc लिए ++ मैं इस गाइड का पालन किया:

बजना के लिए मैं इस गाइड का पालन किया clang --std=c++0x -stdlib=libc++ -lpthread foobar.cpp के साथ, लेकिन मुझे बहुत सारे लिंकर त्रुटियां मिलती हैं:

/tmp/foobar-59W5DR.o: In function `main': 
foobar.cpp:(.text+0x21): undefined reference to `std::__1::thread::~thread()' 
/tmp/foobar-59W5DR.o: In function `_ZNSt3__16threadC2IZ4mainE3$_0JEvEEOT_DpOT0_': 
foobar.cpp:(.text+0x5c): undefined reference to `operator new(unsigned long)' 
foobar.cpp:(.text+0x186): undefined reference to `operator delete(void*)' 
foobar.cpp:(.text+0x19b): undefined reference to `std::__1::__throw_system_error(int, char const*)' 
/tmp/foobar-59W5DR.o: In function `_ZNSt3__114__thread_proxyINS_5tupleIJZ4mainE3$_0EEEEEPvS4_': 
foobar.cpp:(.text+0x200): undefined reference to `std::__1::__thread_local_data()' 
foobar.cpp:(.text+0x211): undefined reference to `operator new(unsigned long)' 
foobar.cpp:(.text+0x23b): undefined reference to `std::__1::__thread_struct::__thread_struct()' 
foobar.cpp:(.text+0x31b): undefined reference to `operator delete(void*)' 
/tmp/foobar-59W5DR.o: In function `_ZNSt3__110unique_ptrINS_5tupleIJZ4mainE3$_0EEENS_14default_deleteIS3_EEED2Ev': 
foobar.cpp:(.text+0x3dd): undefined reference to `operator delete(void*)' 
/tmp/foobar-59W5DR.o: In function `main::$_0::operator()() const': 
foobar.cpp:(.text+0x3fc): undefined reference to `std::__1::cout' 
/tmp/foobar-59W5DR.o: In function `_ZNSt3__110unique_ptrINS_5tupleIJZ4mainE3$_0EEENS_14default_deleteIS3_EEEC2EPS3_': 
foobar.cpp:(.text+0x4e9): undefined reference to `std::terminate()' 
/tmp/foobar-59W5DR.o: In function `std::__1::__thread_specific_ptr<std::__1::__thread_struct>::reset(std::__1::__thread_struct*)': 
foobar.cpp:(.text._ZNSt3__121__thread_specific_ptrINS_15__thread_structEE5resetEPS1_[_ZNSt3__121__thread_specific_ptrINS_15__thread_structEE5resetEPS1_]+0x57): undefined reference to `std::__1::__thread_struct::~__thread_struct()' 
foobar.cpp:(.text._ZNSt3__121__thread_specific_ptrINS_15__thread_structEE5resetEPS1_[_ZNSt3__121__thread_specific_ptrINS_15__thread_structEE5resetEPS1_]+0x60): undefined reference to `operator delete(void*)' 
/tmp/foobar-59W5DR.o: In function `std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::operator<< <std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*)': 
foobar.cpp:(.text._ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc[_ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc]+0x28): undefined reference to `std::__1::basic_ostream<char, std::__1::char_traits<char> >::sentry::sentry(std::__1::basic_ostream<char, std::__1::char_traits<char> >&)' 
foobar.cpp:(.text._ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc[_ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc]+0x1ec): undefined reference to `std::__1::ios_base::getloc() const' 
foobar.cpp:(.text._ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc[_ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc]+0x1fe): undefined reference to `std::__1::ctype<char>::id' 
foobar.cpp:(.text._ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc[_ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc]+0x206): undefined reference to `std::__1::locale::use_facet(std::__1::locale::id&) const' 
foobar.cpp:(.text._ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc[_ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc]+0x272): undefined reference to `std::__1::locale::~locale()' 
foobar.cpp:(.text._ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc[_ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc]+0x294): undefined reference to `std::__1::locale::~locale()' 
foobar.cpp:(.text._ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc[_ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc]+0x378): undefined reference to `std::__1::ios_base::clear(unsigned int)' 
foobar.cpp:(.text._ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc[_ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc]+0x3d0): undefined reference to `std::__1::basic_ostream<char, std::__1::char_traits<char> >::sentry::~sentry()' 
foobar.cpp:(.text._ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc[_ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc]+0x3dc): undefined reference to `__cxa_begin_catch' 
foobar.cpp:(.text._ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc[_ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc]+0x3f9): undefined reference to `std::__1::ios_base::__set_badbit_and_consider_rethrow()' 
foobar.cpp:(.text._ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc[_ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc]+0x403): undefined reference to `__cxa_end_catch' 
foobar.cpp:(.text._ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc[_ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc]+0x424): undefined reference to `std::__1::basic_ostream<char, std::__1::char_traits<char> >::sentry::~sentry()' 
foobar.cpp:(.text._ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc[_ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc]+0x43d): undefined reference to `__cxa_end_catch' 
foobar.cpp:(.text._ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc[_ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc]+0x466): undefined reference to `std::terminate()' 
/tmp/foobar-59W5DR.o: In function `std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::endl<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&)': 
foobar.cpp:(.text._ZNSt3__14endlIcNS_11char_traitsIcEEEERNS_13basic_ostreamIT_T0_EES7_[_ZNSt3__14endlIcNS_11char_traitsIcEEEERNS_13basic_ostreamIT_T0_EES7_]+0x38): undefined reference to `std::__1::ios_base::getloc() const' 
foobar.cpp:(.text._ZNSt3__14endlIcNS_11char_traitsIcEEEERNS_13basic_ostreamIT_T0_EES7_[_ZNSt3__14endlIcNS_11char_traitsIcEEEERNS_13basic_ostreamIT_T0_EES7_]+0x49): undefined reference to `std::__1::ctype<char>::id' 
foobar.cpp:(.text._ZNSt3__14endlIcNS_11char_traitsIcEEEERNS_13basic_ostreamIT_T0_EES7_[_ZNSt3__14endlIcNS_11char_traitsIcEEEERNS_13basic_ostreamIT_T0_EES7_]+0x55): undefined reference to `std::__1::locale::use_facet(std::__1::locale::id&) const' 
foobar.cpp:(.text._ZNSt3__14endlIcNS_11char_traitsIcEEEERNS_13basic_ostreamIT_T0_EES7_[_ZNSt3__14endlIcNS_11char_traitsIcEEEERNS_13basic_ostreamIT_T0_EES7_]+0xac): undefined reference to `std::__1::locale::~locale()' 
foobar.cpp:(.text._ZNSt3__14endlIcNS_11char_traitsIcEEEERNS_13basic_ostreamIT_T0_EES7_[_ZNSt3__14endlIcNS_11char_traitsIcEEEERNS_13basic_ostreamIT_T0_EES7_]+0xbe): undefined reference to `std::__1::locale::~locale()' 
foobar.cpp:(.text._ZNSt3__14endlIcNS_11char_traitsIcEEEERNS_13basic_ostreamIT_T0_EES7_[_ZNSt3__14endlIcNS_11char_traitsIcEEEERNS_13basic_ostreamIT_T0_EES7_]+0xcd): undefined reference to `std::__1::basic_ostream<char, std::__1::char_traits<char> >::put(char)' 
foobar.cpp:(.text._ZNSt3__14endlIcNS_11char_traitsIcEEEERNS_13basic_ostreamIT_T0_EES7_[_ZNSt3__14endlIcNS_11char_traitsIcEEEERNS_13basic_ostreamIT_T0_EES7_]+0xdd): undefined reference to `std::__1::basic_ostream<char, std::__1::char_traits<char> >::flush()' 
/tmp/foobar-59W5DR.o: In function `std::__1::ostreambuf_iterator<char, std::__1::char_traits<char> > std::__1::__pad_and_output<char, std::__1::char_traits<char> >(std::__1::ostreambuf_iterator<char, std::__1::char_traits<char> >, char const*, char const*, char const*, std::__1::ios_base&, char)': 
foobar.cpp:(.text._ZNSt3__116__pad_and_outputIcNS_11char_traitsIcEEEENS_19ostreambuf_iteratorIT_T0_EES6_PKS4_S8_S8_RNS_8ios_baseES4_[_ZNSt3__116__pad_and_outputIcNS_11char_traitsIcEEEENS_19ostreambuf_iteratorIT_T0_EES6_PKS4_S8_S8_RNS_8ios_baseES4_]+0x215): undefined reference to `std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::__init(unsigned long, char)' 
foobar.cpp:(.text._ZNSt3__116__pad_and_outputIcNS_11char_traitsIcEEEENS_19ostreambuf_iteratorIT_T0_EES6_PKS4_S8_S8_RNS_8ios_baseES4_[_ZNSt3__116__pad_and_outputIcNS_11char_traitsIcEEEENS_19ostreambuf_iteratorIT_T0_EES6_PKS4_S8_S8_RNS_8ios_baseES4_]+0x389): undefined reference to `std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::~basic_string()' 
foobar.cpp:(.text._ZNSt3__116__pad_and_outputIcNS_11char_traitsIcEEEENS_19ostreambuf_iteratorIT_T0_EES6_PKS4_S8_S8_RNS_8ios_baseES4_[_ZNSt3__116__pad_and_outputIcNS_11char_traitsIcEEEENS_19ostreambuf_iteratorIT_T0_EES6_PKS4_S8_S8_RNS_8ios_baseES4_]+0x3a4): undefined reference to `std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::~basic_string()' 
/tmp/foobar-59W5DR.o:(.eh_frame+0x47): undefined reference to `__gxx_personality_v0' 
clang-3: error: linker command failed with exit code 1 (use -v to see invocation) 

यहां दो मानक पुस्तकालयों के तहत मानक खोज निर्देशिकाएं हैं। के परिणाम के रूप में 'बजना -Wp, -v -x C++ - -fsyntax-केवल' मैं यह मिल गया:

मैं clang --std=c++11 -stdlib=libc++ -llibc++ -lpthread -o foobar foobar.cpp कोशिश की, लेकिन विफल रही लिंकर फिर, यह libc नहीं मिल रहा है ++। क्या मैं libC++ से जुड़ने में असफल रहता हूं या यह कुछ और मुश्किल है?

+1

'-lpreadread' – Zaffy

+2

के बजाय '-pthread' के साथ लिंक करने पर विचार करें Zaffy ने उत्तर दिया है कि आपकी समस्या को ठीक करना चाहिए। लेकिन जब आप प्रोग्राम चलाते हैं, तो आपको कोर-डंप मिल जाएगा। आपको धागे में शामिल होना है, अन्यथा यह दुर्घटनाग्रस्त हो जाएगा। कृपया जोड़ें: 't1.join();' –

+0

@ फिलिप कैलासेन आह धन्यवाद, मैंने ज़ैफी के उत्तर पर पोस्ट की गई टिप्पणी को ठीक किया ... ^।^ – bash0r

उत्तर

12

आप सी ++ के लिए सी नहीं के लिए क्लैंग के साथ संकलित कर रहे हैं।

  • सी उपयोग बजना
  • सी के लिए बजना ++

कारण का उपयोग ++ क्यों बजना फ्लॉप चेतावनी दी है आप है, क्योंकि आप स्पष्ट रूप से मानक को परिभाषित किया।

+0

मैं इसकी पुष्टि कर सकता हूं। 'clang ++' काम करना चाहिए। –

+1

वह अपरिभाषित संदर्भ त्रुटियों को नहीं देगा, यह संकलक त्रुटियों को देगा। यह सही भाषा का सही ढंग से निर्णय ले रहा है। – Puppy

+0

@Zaffy पहली बार मैंने कभी भी 'clang ++' के बजाय 'clang' का उपयोग किया ... मैं किस तरह का हूँ? जब मैं आउटपुट चलाता हूं तो अब मुझे 'सक्रिय अपवाद के बिना बुलाया जाता है \ n निरस्त' मिलता है। कोई उपाय? – bash0r