2012-04-18 66 views
5

pthread_create()を示す非常に単純なコードを持つ単一のC++ソースファイル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ライブラリが付属していません。あなたは実際に持っていますか?(1つは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 ++コマンドラインでライブラリを指定する必要があります。だから試してみてください:

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

私は答えを見つけたときに私は自分自身を蹴った(これはライブラリとgccのFAQのようなものです)。ほとんどのgccオプションの順序は関係ありませんが、ライブラリにとっては重要です。

pthreadライブラリにMinGWディストリビューションが付属している場合は、ライブラリパスを指定する必要はありません。上記のコマンドラインはa.exe実行ファイルを生成することに注意してください。それを避けるために-o test.exeを渡してください。

+0

ああ、それは甘いです。 -lpthreadの順序変更が機能しました。あなたの役に立つポインタと答えをありがとう。私はStackoverflowが大好き!それはフォーラムです。私のコードではない! – goldenmean

+0

他のユーザーから約1か月後にありがとうございました! – The111

+0

2年以上後に他のユーザーからありがとう! –

関連する問題