Celeritas
Celeritas

Reputation: 15043

How do I get pthreads to work in Windows?

I was running into errors such as those mentioned bellow when trying to compile code containing pthreads

warning: return type defaults to 'int' [-Wreturn-type]|
|In function 'print_message_function':|
warning: control reaches end of non-void function [-Wreturn-type]|
| undefined reference to `_imp__pthread_create'|
| undefined reference to `_imp__pthread_create'|
| undefined reference to `_imp__pthread_join'|
| undefined reference to `_imp__pthread_join'|

I'm running GCC on Windows 7 but I have mingw installed. I'm using the IDE Code::Blocks and select "compile current file". Here is a screen shot of the linker settings, I'm at a loss here

codeblocks linker

UPDATE: I added -pthread to the "Other linker options" and it works better. There still are problems. When I compile it says

|In function 'print_message_function':|
warning: control reaches end of non-void function [-Wreturn-type]|

and when I go to run it CodeBlocks says "it appears the program has not been built yet" and when I click on "build" I am shown this error

mingw32-g++.exe  -o "SimpleExample.exe" "SimpleExample.o"  -static-libgcc -static-libstdc++ -pthread  
mingw32-g++.exe: error: unrecognized option '-pthread'
Process terminated with status 1 (0 minutes, 0 seconds)
0 errors, 1 warnings (0 minutes, 0 seconds)

How do I fix this? I want to build/test on Windows but have the program run on a Unix environment. What is the difference between compile and build in an IDE?

Upvotes: 3

Views: 26013

Answers (4)

Gizmo0001
Gizmo0001

Reputation: 31

Here's what happens currently as of now when using MinGW Installation Manager (the mingw32 package manager for windows) under Windows with the following packages installed:

  • mingw32-libpthreadgc-dll
  • mingw32-libpthreadgce-dll

ERROR: gcc 5.3.0 fails linking pthread e.g.

c:/mingw/bin/../lib/gcc/mingw32/5.3.0/../../../../mingw32/bin/ld.exe: cannot find -lpthread
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 0 second

SOLUTION: include sources from MinGW Package Manager, too, i.e. also select

  • mingw32-libpthreadgc-dev
  • mingw32-libpthreadgce-dev

MinGW 4.9.2 does not show this effect. GCC 5.4 on Ubuntu also does not require the pthread sources to compile any code. This one helped me out whilst other tries (using mingw32-libpthread-old or configuring linker settings) failed.

Upvotes: 2

Mario
Mario

Reputation: 81

this answer come late but ... it worked for me, so I decided to share it.

  1. I downloaded the pthreads library from
    ftp://sourceware.org/pub/pthreads-win32/pthreads-w32-2-9-1-release.zip
  2. I unzipped it, then I copyed header files (.h) in
    C:\ProgramFiles\CodeBlocks\MinGW\include and library files in C:\ProgramFiles\CodeBlocks\MinGW\lib
  3. I copyed the dll files in the executable folder of my project (myprojects/bin/debug in my case)
  4. I added the -lpthreadGC2 option in the
    Settings -> Compiler -> Linker Settings -> Other Linker Options of my Code::Blocks IDE

Hope this can help.

Upvotes: 8

cdarke
cdarke

Reputation: 44354

It is -lpthread, not -pthread.

Edit: Libraries can be added to the compile line in a couple of ways. If we have a file called (for example) /usr/lib/libpthread.so we could include the file like this:

cc -o myprog /usr/lib/libpthread.so myprog.c

or, alternatively:

cc -o myprog -lpthread -L /usr/lib myprog.c

Since /usr/lib is a standard directory, we don't normally require the -L option. At runtime we might have to set an environment variable:

export LD_LIBRARY_PATH=/usr/lib

but again, the standard libraries are defaulted, so you don't have to use this unless you are building your own or using 3rd-party libraries.

Upvotes: 4

Klas Lindbäck
Klas Lindbäck

Reputation: 33273

warning: control reaches end of non-void function [-Wreturn-type]|

Your main does not return a value. Add return 0; at the end of main.

| undefined reference to `_imp__pthread_create'|

You need to link with the thread library. Add -lpthread to the linker command line.

Upvotes: 2

Related Questions