3

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?

Celeritas
  • 14,489
  • 36
  • 113
  • 194
  • 4
    What command line do you use to build your code? You'll need to link to the library that implements pthreads so will need to include `-pthread` in the command line. – simonc Oct 09 '13 at 08:32
  • Those are linker errors, not compiler errors. – Kerrek SB Oct 09 '13 at 08:34
  • I updated the description. I'm using the default code blocks to compile. – Celeritas Oct 09 '13 at 08:38
  • I've never used code blocks but it looks like you're missing a reference to libpthread. Try adding `-pthread` to the other linker options. – simonc Oct 09 '13 at 08:41
  • I too am using CodeBlocks. With the solution that you accepted makes me think that it worked out for you and not it works fine. Old problem I know. Could you add the solution in the bottom of your Question? As the solution seems to be in a Linux or Mac solution environment. Would be great as I am having trouble with threads in CodeBlock and getting the same errors. I installed the pthread.h manuall, but not sure where to put the *.dll files either. Thanks! – ejbytes Dec 11 '16 at 05:31
  • 1
    Does this answer your question? [How to set up pthreads on windows?](https://stackoverflow.com/questions/19467455/how-to-set-up-pthreads-on-windows) – Henke Feb 06 '22 at 13:24

4 Answers4

7

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.

Mario
  • 71
  • 1
  • 1
  • I did everything like you described for my console application, but for me I had to set the "Build target" to "release" instead of "debug" in Codeblocks, so it worked then. Thank you very much, you made my day :)! – dima Oct 13 '16 at 14:02
  • This worked for me. Asked for Windows and got Windows answer. – ejbytes Dec 11 '16 at 06:36
  • This works very well but I got a redefinition error(https://stackoverflow.com/questions/33557506/timespec-redefinition-error). The solution is to include the following macro before including the pthread header file in your program: #define HAVE_STRUCT_TIMESPEC – Vishal Sharma Feb 08 '18 at 16:14
4

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.

cdarke
  • 42,728
  • 8
  • 80
  • 84
  • 1
    How did you know that? – Celeritas Oct 09 '13 at 09:27
  • -l is the linker option to add a library. The compiler and linker documentation lists the available options. – Klas Lindbäck Oct 09 '13 at 09:33
  • @Celeritas: 'cause I've done it before. I agree that this is not particularly well documented - it is easy to find only if you already know! I will add some explanation. – cdarke Oct 09 '13 at 10:41
  • Would love some documentation for Windows. This seems to be using directories in Linux or Mac (e.g. /usr/lib/...) Whereas in Windows is somethign like C:/.../MinGW/lib/ – ejbytes Dec 11 '16 at 05:27
2
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.

Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82
  • This fixed some problems which is good. How come I need to add this option to the linker and I've never had to supply the linker with options before? – Celeritas Oct 09 '13 at 08:51
  • 1
    @Celeritas Many functions, e.g. those part of standard C, and often quite a few platform specific function is part of a library that's always linked in (usually called the "C library", libc or C runtime, and similar for the standard C++ library). In the general case you need to explicitly tell the linker which libraries you need to link in though, and pthreads lives in its own library. – nos Oct 09 '13 at 09:08
  • You mention, "Add -lpthread to the linker command line". How does one do this? How does one Add -lpthread to the linker command line? – ejbytes Dec 11 '16 at 05:36
  • @ejbytes If you don't run the linker separately, just add `-lpthread` to the compiler command line - the compiler will forward it to the linker. – Klas Lindbäck Dec 12 '16 at 08:20
  • @KlasLindbäck Thank you. I don't know the difference between -lpthread and -lpthreadGC2, but for some reason Mario's solution using -lpthreadGC2 works and -lpthread does not. Puzzling. – ejbytes Dec 15 '16 at 00:03
2

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.

Gizmo0001
  • 31
  • 1