2014-01-19 50 views
12

我想跨爲Windows編譯一個簡單的應用程序中的一員:MinGW的錯誤:「線」是不是「性病」

#include <thread> 

void Func(){ 
    return; 
} 

int main(){ 
    std::thread thr1(Func); 
    thr1.detach(); 
    return 0; 
} 

而這就是我得到:

$ i686-w64-mingw32-g++ -static-libstdc++ -static-libgcc -pipe -g -std=c++0x ./threadstutor.cpp 
./threadstutor.cpp: In function ‘int main()’: 
./threadstutor.cpp:8:3: error: ‘thread’ is not a member of ‘std’ 
./threadstutor.cpp:8:15: error: expected ‘;’ before ‘thr1’ 
./threadstutor.cpp:9:3: error: ‘thr1’ was not declared in this scope 

其實,如果用g ++編譯Ubuntu,這段代碼就沒有這個問題;但是我需要爲Windows進行交叉編譯,並且在這裏我被卡住了。

+4

如果你在windows上編譯,你需要使用帶有posix-threads的Mingw-Builds v4.8.1:http://sourceforge.net/projects/mingwbuilds/files/host-windows/releases/4.8.1/64 -bit/threads-posix/sjlj /你可以選擇sjlj和seh。 Seh只有x64,sjlj同時是x32和x64。 – Brandon

+0

@CantChooseUsernames我在Ubuntu上編譯它。我從庫中找到最新的mingw; _「i686-w64-mingw32-g ++ --version」_說_ _ i686-w64-mingw32-g ++(GCC)4.6.3「_ –

+0

@CantChooseUsernames雖然如果我沒有找到解決方案,我可以使用它作爲某種解決方法:通過Wine運行MinGW來編譯源代碼。雖然它很奇怪:D 順便說一句,我的版本的編譯器不支持_std :: thread_'s? –

回答

15

已經有一個更好的選擇: https://github.com/meganz/mingw-std-threads 這是最常用的線程的一個輕量級Win32本地實施和同步C++ 11類的MinGW。這些都是在一個只能使用標題的庫中實現的,它可以與系統庫共存。它也支持Windows XP,它沒有直接模擬條件變量。