Reputation: 15043
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
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
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:
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
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
Reputation: 81
this answer come late but ... it worked for me, so I decided to share it.
Hope this can help.
Upvotes: 8
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
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