使用 Eclipse+CDT+MinGW 編寫 Linux 程式,如果要用到 thread 的話,預設沒有(我的版本沒有),必須額外載入套件「pthreads-w 32-2-8 -0-release.exe」。解壓出來後有三個套件目錄:Pre-built.2、pthreads.2、QueueUserAPCEx。
1、Download pthreads-w32-2-8-0-release.exe 。 http://www.mirrorservice.org/sites/sourceware.org/pub/pthreads-win32/
聽說:因為pthread 不是預設的 Linux 函式庫,連接時需要使用靜態庫 libthread.a 才可使用 pthread_create() 建立執行緒,等其它執行緒處理的方式;因此需要在載入相關的套件。
◎步驟一、在先前建置Eclipse+CDT+MinGW 環境所設的三個環境變數中,都加入 Pre-built.2、pthreads.2 相關的鏈接位置,這是我使用的方式;有看到人直接「把Pre-built.2中的libpthreadGC2.a改名為libpthread.a複製到c:\mingw\lib目錄,pthread.h複製到c:\mingw\include目錄即可解决,我當時試是不行,可能我有漏東西吧!」。
程式碼中可載入套件,但在eclipse中編譯時會出現錯誤:
undefined reference to 'pthread_create'
undefined reference to 'pthread_join'
◎步驟二:在編譯要多設參數,最快最方便的方式,在 Project 下的 Properties 下的 C/C++ Build 中的 Setting 下的 Tool Settings 中的 GCC C++ Complier 進行設定,如圖2,在 Command 中多加入 「- l pthread 」參數,
◎步驟三:同時也在另一個參數設定Expert Settings 中加入 「- l pthread」。在 MinGW C++ Linker 中也是進行相同的設定動作,如圖3 所示。之後即可編譯成功。
圖 2 GCC C++ Compiler
圖 3 MinGW C++ Linker
2、使用Pthread參考網址
C. http://blog.tianya.cn/blogger/post_show.asp?BlogID=797111&PostID=23966420&idWriter=0&Key=0
D. http://www.360doc.com/content/10/0407/15/1066294_21950629.shtml
E. http://tw.myblog.yahoo.com/yh-chiang/article?mid=707&prev=719&next=701
