1

I use MinGW on windows7 32bit. And I can’t compile my source which uses pthread.

My code is below.

#include <stdio.h>
#include <pthread.h>

int main(int argc, char** argv)
{
  (void)argv;
  printf("######## start \n");
#ifndef pthread_create
  return ((int*)(&pthread_create))[argc];
#else
  (void)argc;
  return 0;
#endif
}

Error happens as I compile it.

gcc -I /usr/local/include -L /usr/local/lib/libpthread.dll.a trylpthread.c
C:\Users\xxx\AppData\Local\Temp\cc9OVt5b.o:trylpthread.c:(.text+0x25): undefined reference to `_imp__pthread_create'
collect2.exe: error: ld returned 1 exit status

I use following pthread library.

pthreads-w32-2.8.0-3-mingw32-dev

And here is libpthread.dll.a in /usr/local/lib

Does anyone know how to fix this problem?

user1345414
  • 3,745
  • 9
  • 36
  • 56
  • 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:23

1 Answers1

1

The commandline:

gcc -I /usr/local/include -L /usr/local/lib/libpthread.dll.a trylpthread.c

does not make sense.

-L <dir> is a linker option that directs the linker to search for required libraries in directory <dir>. Thus you are telling the linker to search for required libraries in path /usr/local/lib/libpthread.dll.a, which is not a directory, while on the other hand you are not telling the linker to link any libraries at all. That is why it fails to find any definition for _imp__pthread_create.

Neither does the program you have posted make sense. The lines:

#ifndef pthread_create
  return ((int*)(&pthread_create))[argc];
#else
  (void)argc;
  return 0;
#endif

say:-

If I have not defined a preprocessor macro pthread_create then compile:

  return ((int*)(&pthread_create))[argc];

else compile:

  (void)argc;
  return 0;

Well if you had defined a preprocessor macro pthread_create, e.g.

#define pthread_create whatever

then the code you would compile would be:

  (void)argc;
  return 0;

And since you have indeed not defined any such macro, the code you compile is:

  return ((int*)(&pthread_create))[argc];

which fails at linkage, as you see. And if that code was compiled with pthread_create so defined, it would be:

  return ((int*)(&whatever))[argc];

Rewrite your program as:

#include <stdio.h>
#include <pthread.h>

int main(int argc, char** argv)
{
  (void)argv;
  printf("######## start \n");
  return ((int*)(&pthread_create))[argc];
}

Compile with:

gcc -Wall -I /usr/local/include -o trylpthread.o -c trylpthread.c

Link with:

gcc  -o trylpthread.exe trylpthread.o  /usr/local/lib/libpthread.dll.a

Remember that when you get the program compiled and linked, the appropriate pthreadGC??.dll must be found at runtime in one of the places where the program loader searches for dlls.

Better still, uninstall your MinGW and your pthreads-w32-2.8.0-3-mingw32-dev and install a more up-to-date Windows port of GCC, e.g. TDM-GCC (simplest) or mingw-w64. Pick the 32-bit version, if your Windows system is 32-bit. These toolchains come with built in pthread support, as GCC standardly does.

Compile with:

gcc -Wall -o trylpthread.o -c trylpthread.c

Link with:

gcc  -o trylpthread.exe trylpthread.o  -pthread

(not -lpthread)

Mike Kinghan
  • 55,740
  • 12
  • 153
  • 182
  • I can complie with `gcc -I /usr/local/include trylpthread.c /usr/local/lib/libpthread.dll.a`. And I also can compile wihth `gcc -I /usr/local/include trylpthread.c -pthread` after reinstalled pthread by mingw-get. Thanks – user1345414 Nov 29 '15 at 05:35