2012-04-18 6 views
5

pthread_create()를 보여주는 매우 간단한 코드를 가진 test.cpp 파일 하나를 컴파일하려고합니다. pthread_cond_signal/pthread_cond_wait() 기능.MingW에서 간단한 pthread 코드를 컴파일 할 때 링커 오류가 발생했습니다.

내가 일하는 Windows XP에 Mingw/Ansys를 설치했습니다. 는 MinGW 프롬프트에서 나는 수행

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

CPP를 같이 pthread.h를 포함

#include <pthread.h> 

하지만이 모두는 pthread 라이브러리 함수 나에게 여러 링커 정의되지 않은 참조 오류를했다.

내가 여기서 잘못하고있다. Windows에서 MingW 환경에서 pthread 코드를 생성 할 수 있습니까?

이 오류를 해결하는 방법은 무엇입니까?

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

내가 사용한 MinGW 배포판에는 pthread 라이브러리가 포함되어 있지 않습니다. 실제로 하나 있습니까 (하나는 http://sourceware.org/pthreads-win32/에서 구할 수 있습니다)? –

+0

@Michael Burr : MingW에 pthread를 명시 적으로 설치하지 않았습니다. 하지만 trhe MingW 바이너리 폴더에서 pthreadGC2.dll을 보았을 때, 나는 pthread 호출을 지원한다고 추정했다. 아니? – goldenmean

+0

pthread-Win32 링크를 가져 주셔서 감사합니다. 그냥 쿼리 다음 MinGW pthread.h 헤더 및 pthreadgc2.dll 무엇입니까? – goldenmean

답변

14

라이브러리에 의존하는 파일 다음에 gcc/g ++ 명령 줄에 라이브러리를 지정해야합니다. 그래서 시도 : 나는 대답 (라이브러리와 GCC에 대한 자주 묻는 질문의 그것의 종류)에 발견하면

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

내가 나 자신을 걷어 찼다. 대부분의 gcc 옵션의 경우 순서는 중요하지 않지만 라이브러리의 경우 중요합니다.

pthread 라이브러리가 MinGW 배포판과 함께 제공되면 라이브러리 경로를 지정할 필요가 없습니다. 또한 위의 명령 줄은 a.exe 실행 파일을 생성합니다. 그것을 피하려면 -o test.exe을 전달하십시오.

+0

아, 달콤합니다. -lpthread의 순서가 변경되었습니다. 도움이되는 지침과 답변에 감사드립니다. 내가 Stackoverflow 사랑해! 포럼입니다. 내 코드가 아냐! – goldenmean

+0

거의 한 달 후 다른 사용자에게 감사합니다. – The111

+0

2 년 후 다른 사용자에게 감사드립니다. –

관련 문제