2012-04-18 13 views
5

Estoy tratando de compilar un solo archivo fuente C++ test.cpp, que tiene un código muy simple que muestra pthread_create(); funcionalidad pthread_cond_signal/pthread_cond_wait().Errores del enlazador en la compilación de un código pthread simple en MingW

He instalado Mingw/Ansys en Windows XP donde estoy trabajando. En el símbolo del MingW hago:

g++ -IC:/MinGW/include/ -lpthread test.cpp 
//-IC:/MinGW/include to get pthread.h 
//-LC:/MinGW/bin to get pthreadGC2.dll 

el CPP incluye pthread.h como:

#include <pthread.h> 

Pero esto me dio varios errores de referencia de engarce a todas las funciones de la biblioteca pthread.

¿Qué estoy haciendo mal aquí? ¿Es posible construir un código pthread en el entorno MingW en Windows o no?

¿Cómo resolver este error?

C:\DOCUME~1\ADESHP~1\LOCALS~1\Temp\ccUQhu7D.o:pthread_cond.c:(.text+0x23): undefined reference to `_imp__pthread_mutex_lock' 
C:\DOCUME~1\ADESHP~1\LOCALS~1\Temp\ccUQhu7D.o:pthread_cond.c:(.text+0x67): undefined reference to `_imp__pthread_cond_signal' 
C:\DOCUME~1\ADESHP~1\LOCALS~1\Temp\ccUQhu7D.o:pthread_cond.c:(.text+0x75): undefined reference to `_imp__pthread_mutex_unlock' 
C:\DOCUME~1\ADESHP~1\LOCALS~1\Temp\ccUQhu7D.o:pthread_cond.c:(.text+0x98): undefined reference to `_imp__pthread_exit' 
C:\DOCUME~1\ADESHP~1\LOCALS~1\Temp\ccUQhu7D.o:pthread_cond.c:(.text+0xbc): undefined reference to `_imp__pthread_mutex_lock' 
C:\DOCUME~1\ADESHP~1\LOCALS~1\Temp\ccUQhu7D.o:pthread_cond.c:(.text+0xe8): undefined reference to `_imp__pthread_cond_wait' 
C:\DOCUME~1\ADESHP~1\LOCALS~1\Temp\ccUQhu7D.o:pthread_cond.c:(.text+0x10f): undefined reference to `_imp__pthread_mutex_unlock' 
C:\DOCUME~1\ADESHP~1\LOCALS~1\Temp\ccUQhu7D.o:pthread_cond.c:(.text+0x135): undefined reference to `_imp__pthread_exit' 
C:\DOCUME~1\ADESHP~1\LOCALS~1\Temp\ccUQhu7D.o:pthread_cond.c:(.text+0x153): undefined reference to `_imp__pthread_attr_init' 
C:\DOCUME~1\ADESHP~1\LOCALS~1\Temp\ccUQhu7D.o:pthread_cond.c:(.text+0x169): undefined reference to `_imp__pthread_mutex_init' 
C:\DOCUME~1\ADESHP~1\LOCALS~1\Temp\ccUQhu7D.o:pthread_cond.c:(.text+0x17f): undefined reference to `_imp__pthread_attr_setdetachstate' 
C:\DOCUME~1\ADESHP~1\LOCALS~1\Temp\ccUQhu7D.o:pthread_cond.c:(.text+0x195): undefined reference to `_imp__pthread_cond_init' 
C:\DOCUME~1\ADESHP~1\LOCALS~1\Temp\ccUQhu7D.o:pthread_cond.c:(.text+0x1bc): undefined reference to `_imp__pthread_create' 
C:\DOCUME~1\ADESHP~1\LOCALS~1\Temp\ccUQhu7D.o:pthread_cond.c:(.text+0x1e3): undefined reference to `_imp__pthread_create' 
C:\DOCUME~1\ADESHP~1\LOCALS~1\Temp\ccUQhu7D.o:pthread_cond.c:(.text+0x201): undefined reference to `_imp__pthread_join' 
C:\DOCUME~1\ADESHP~1\LOCALS~1\Temp\ccUQhu7D.o:pthread_cond.c:(.text+0x21f): undefined reference to `_imp__pthread_join' 
C:\DOCUME~1\ADESHP~1\LOCALS~1\Temp\ccUQhu7D.o:pthread_cond.c:(.text+0x239): undefined reference to `_imp__pthread_mutex_destroy' 
C:\DOCUME~1\ADESHP~1\LOCALS~1\Temp\ccUQhu7D.o:pthread_cond.c:(.text+0x247): undefined reference to `_imp__pthread_cond_destroy' 
C:\DOCUME~1\ADESHP~1\LOCALS~1\Temp\ccUQhu7D.o:pthread_cond.c:(.text+0x255): undefined reference to `_imp__pthread_attr_destroy' 
C:\DOCUME~1\ADESHP~1\LOCALS~1\Temp\ccUQhu7D.o:pthread_cond.c:(.text+0x263): undefined reference to `_imp__pthread_exit' 
collect2: ld returned 1 exit status 
+0

Las distribuciones MinGW que he usado no vienen con una biblioteca pthread. ¿De verdad tiene uno (uno está disponible en http://sourceware.org/pthreads-win32/)? –

+0

@Michael Burr: No instalé pthread en MingW explícitamente. Pero como vi pthreadGC2.dll en la carpeta binaria de MingW, supuse que admitía llamadas pthread. ¿No? – goldenmean

+0

Gracias por el enlace a pthread-Win32. Solo una consulta, entonces, ¿cuál es entonces el encabezado pthread.h y pthreadgc2.dll en MinGW? – goldenmean

Respuesta

14

Debe especificar la biblioteca en la línea de comando gcc/g ++ después de los archivos que dependen de la biblioteca. Así que trate de:

g++ -IC:/MinGW/include/ test.cpp -lpthread 

GOLPEÉ mí mismo cuando me encontré con la respuesta (que es una especie de FAQ para bibliotecas y gcc). Para la mayoría de las opciones de gcc, el orden no importa, pero para las bibliotecas es fundamental.

No debería tener que especificar la ruta de la biblioteca si la biblioteca pthread vino con su distribución MinGW (como parece ser el caso para usted). Además, recuerde que la línea de comando anterior producirá un ejecutable a.exe; pase -o test.exe para evitar eso.

+0

Oh, eso es dulce. El cambio de orden de -lpthread funcionó. Gracias por sus útiles consejos y respuestas. ¡Amo Stackoverflow! El foro que es; no en mi código! – goldenmean

+0

¡Gracias de otro usuario casi un mes después! – The111

+0

¡Gracias de otro usuario más de dos años después! –

Cuestiones relacionadas