[問題] Semaphore 和pthread的問題

作者: ck80865 (bear)   2014-10-31 21:57:31
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
Linux
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
<pthread.h>
<semaphore.h>
問題(Question):
pthread 和 semaphore 一起使用的問題,
在網路上找到的許多範例 compile 都會出現一樣的問題
會顯示`sem_init'、`pthread_create'、`sem_wait'這些都未定義
不知道問題出在哪邊
程式碼(Code):(請善用置底文網頁, 記得排版)
http://codepad.org/9Lt6HuBF
補充說明(Supplement):
作者: x000032001 (版廢了該走了)   2014-10-31 22:10:00
gcc -o test test.c -lpthread -lrt
作者: lantw44 (#######################)   2014-10-31 22:23:00
cc -pthread
作者: x000032001 (版廢了該走了)   2014-10-31 22:36:00
include給cpp看 link是ld的事可以說include只是定義 實際要呼叫誰是ld去找來link的所以你要告訴他function在pthread(libpthread.so)-l找不到就要再加-Lpath設定dir 詳細的gcc -v可以看
作者: LPH66 (-6.2598534e+18f)   2014-10-31 22:58:00
header 只是元件說明書, 實際元件要 -lxxx 從程式庫拿出來
作者: Killercat (殺人貓™)   2014-11-01 12:56:00
header是讓你找到.h 我舉個例子,你可以試試看寫一個標準的class, 只寫header不寫cpp,你會發現若是其他檔案include header並且用它, compile是會過的,死在ld(linking)而已普通我們說的三種phase就是compile, linking, run這個觀念一定要弄清楚 不然保證你未來2年很難過
作者: ah7675 (阿毛)   2014-11-01 13:51:00
又要推"程式設計師的自我修養"了XD
作者: jammy50605 (小刀)   2014-11-02 12:35:00
樓上那本都缺貨 買不到= =
作者: Killercat (殺人貓™)   2014-11-02 17:05:00
還沒搞懂linking time/runtime看那本書是找死而已XD還是循序漸進慢慢先學下去吧~

Links booklink

Contact Us: admin [ a t ] ucptt.com