2014-11-23 63 views
0

此代碼:爲什麼這段代碼不能用clang構建,使用gcc崩潰,但在VC++下運行良好?

#include <iostream> 
#include <future> 

int main() 
{ 
    std::async([]{std::cout << "hello\n";}).get(); 
} 

運行正常,並打印hello用Visual C++ 2013,但將引發異常用gcc生產:

terminate called after throwing an instance of 'std::system_error' 
what(): Unknown error -1 

和甚至不帶鐺構建,生產這種錯誤消息:

/usr/bin/ld: /tmp/source-83f28e.o: undefined reference to symbol '[email protected]@GLIBC_2.2.5' 
//lib/x86_64-linux-gnu/libpthread.so.0: error adding symbols: DSO missing from command line 
clang: error: linker command failed with exit code 1 (use -v to see invocation) 

我用rexter來運行測試。你能解釋這種行爲嗎?

編輯

隨着-pthread編譯選項gcc版本運行良好,現在鐺版本構建和生產:

hello 
exception_ptr not yet implemented 
+5

你使用過'-pthread'標誌嗎? – juanchopanza 2014-11-23 23:48:57

+0

這不是一個真正的「崩潰」。 – 2014-11-23 23:58:08

+0

@ juanchopanza _marked as duplicate_這是正確的,特別是在事後的幫助下。鐺錯誤消息導致鏈接器選項,但gcc消息 - 根本不。 – 2014-11-24 00:33:52

回答

2

爲了您添加支持多線程,則需要對並行線程庫鏈接。這是GCC和Clang上的-pthread(作爲內置標誌提供)。或者,-lpthread可能會伎倆。

相關問題