18

I'm trying to cross-compile for Windows a simple application:

#include <thread>

void Func(){
  return;
}

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

And that's what I get:

$ 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

Actually, this code have no such problem if compile with g++ for Ubuntu; but I need to cross-compile for Windows, and here I'm stuck.

Hi-Angel
  • 4,407
  • 8
  • 59
  • 81
  • 8
    If you are compiling this on windows, you will need Mingw-Builds v4.8.1 with posix-threads: http://sourceforge.net/projects/mingwbuilds/files/host-windows/releases/4.8.1/64-bit/threads-posix/sjlj/ You can choose between sjlj and seh. Seh is only x64 and sjlj is both x32 and x64. – Brandon Jan 19 '14 at 01:08
  • @CantChooseUsernames i'm compiling it on Ubuntu. I have latest mingw from repository; _"i686-w64-mingw32-g++ --version"_ says _"i686-w64-mingw32-g++ (GCC) 4.6.3"_ – Hi-Angel Jan 19 '14 at 01:12
  • @CantChooseUsernames though if I didn't find the solution, I could use it as some kind of workaround: run MinGW through wine to compile the source. Although it's weird :D By the way, is my version of compiler doesn't support _std::thread_'s? – Hi-Angel Jan 19 '14 at 01:23
  • I don't think 4.6.3 every support threads for Windows executables.. I think `std::thread` came in at 4.7+. Maybe an update might fix it. Always worth a shot. – Brandon Jan 19 '14 at 01:45
  • @CantChooseUsernames alas, there is still no success. I found on a site of MinGW link to latest build, then tried to compile with it. But the error rest the same. Version of this g++ is _4.9.0 20131227_ Seems, it's time to give up with STL, and use multi-threading support of MFC :( – Hi-Angel Jan 19 '14 at 02:15
  • try printing `g++ --version` and make sure it is actually using the right one. Other than that, I'm not sure why you can't use `std::thread`. Weird. – Brandon Jan 19 '14 at 02:25
  • @CantChooseUsernames Yes, i'm sure. `$ ./i686-w64-mingw32-g++ --version` says `i686-w64-mingw32-g++ (GCC) 4.9.0 20131227 (experimental).....`, and `$ ./i686-w64-mingw32-g++ ~/Projects/WinCrossCmplng/StilsoftTest/threadstutor.cpp -static-libstdc++ -static-libgcc -pipe -g -std=c++11` still talking the same error `...error: ‘thread’ is not a member of ‘std’...` Sorry for awful formatting. – Hi-Angel Jan 19 '14 at 02:31
  • 1
    @CantChooseUsernames I found a strange thing! I just tried to download Windows version of MinGW and run it with wine. And... it works! The version number _4.8.1_, it's surely lower than previous build which I tried. Anyway, it's works! – Hi-Angel Jan 19 '14 at 02:49
  • @Brandon I am very thankful to you. Right now I couldn't find a MinGW link to download at all! The site gave me either linux version of MinGW(I am for some reason need a GNU/linux one), either installer which doesn't work because I am behind a proxy. Then I am recalled you old comment. Thank you very much! – Hi-Angel Jul 10 '14 at 10:05
  • There is no linux version of MinGW. The W means Windows – M.M Jun 08 '16 at 09:53
  • @M.M [There is](http://packages.ubuntu.com/search?keywords=mingw&searchon=names&suite=xenial&section=all). How else am I supposed to compile Windows app from GNU/Linux? ☺ – Hi-Angel Jun 08 '16 at 11:13
  • Possible duplicate of [mingw-w64 threads: posix vs win32](https://stackoverflow.com/questions/17242516/mingw-w64-threads-posix-vs-win32) – rogerdpack Oct 25 '18 at 06:10

3 Answers3

17

This error means that the STL you are using does not contain all the features of C++11.

To access C++11 threads in Windows, you will need a build of Mingw with posix-threads. Here you can find Mingw-Builds v4.8.1: http://sourceforge.net/projects/mingwbuilds/files/host-windows/releases/4.8.1/64-bit/threads-posix/sjlj/

Sergey K.
  • 24,096
  • 13
  • 100
  • 173
17

There is already a better option: https://github.com/meganz/mingw-std-threads This is a lightweight win32 native implementation of the most used threading and synchronization C++11 classes for MinGW. These are implemented in a header-only library that can co-exist with the system libs. It supports Windows XP as well, which does not have a direct analog of conditional variables.

Alexander Vassilev
  • 1,350
  • 13
  • 21
0

On Window, you can choice to use POSIX thread lib to construct your app. You can find it via https://sourceforge.net/projects/pthreads4w/files/

Dharman
  • 26,923
  • 21
  • 73
  • 125
  • 1
    There are easier solutions than manually setting up this library. You can just install MinGW-w64 (one of its numerous distributions), which should have multithreading library features by default. – HolyBlackCat Jul 27 '20 at 18:38
  • 1
    This suggestion seems to have already been mentioned [in this answer](https://stackoverflow.com/a/21879380/2388257) here. – Hi-Angel Jul 27 '20 at 20:51