剛接觸 CODEBLOCKS 在fedora9 下配置好環境後,複製了書上的一段線程應用代碼。遭遇兩個迷惑問題。未解決。
1.開始編譯,提示pthread_create 未定義(用到:res = pthread_create(&thread[no], NULL, thrd_func, (void*)no);)。
上網原因:問題原因: pthread 庫不是 Linux 系統預設的庫,串連時需要使用靜態庫 libpthread.a,所以在使用pthread_create()建立線程,以及調用 pthread_atfork()函數建立fork處理常式時,需要連結該庫。
於是,在CODEBLOCKS 連結器設定項中添加libpthread.a,尋找到該檔案在/usr/bin/lib 中(注不同環境路徑可能有異)。
2.再次編譯。未定義的問題解決了,但緊接著更大的問題 來了。
debug Information:
/usr/lib/libpthread.a(pthread_create.o)||In function `pthread_create':|
(.text+0xa2f)||undefined reference to `_dl_stack_flags'|
/usr/lib/libpthread.a(pthread_create.o)||In function `pthread_create':|
(.text+0xbe5)||undefined reference to `_dl_stack_flags'|
/usr/lib/libpthread.a(ptw-pause.o)||In function `__pause_nocancel':|
(.text+0x18)||undefined reference to `__syscall_error'|
/usr/lib/libpthread.a(ptw-pause.o)||In function `__pause_nocancel':|
(.text+0x3b)||undefined reference to `__syscall_error'|
/usr/lib/libpthread.a(init.o)||In function `__pthread_initialize_minimal':|
(.text+0x1c)||undefined reference to `__libc_setup_tls'|
/usr/lib/libpthread.a(init.o)||In function `__pthread_initialize_minimal':|
(.text+0x236)||undefined reference to `_dl_init_static_tls'|
/usr/lib/libpthread.a(init.o)||In function `__pthread_initialize_minimal':|
(.text+0x240)||undefined reference to `_dl_wait_lookup_done'|
/usr/lib/libpthread.a(ptw-read.o)||In function `__read_nocancel':|
(.text+0x26)||undefined reference to `__syscall_error'|
/usr/lib/libpthread.a(ptw-read.o)||In function `__read_nocancel':|
(.text+0x56)||undefined reference to `__syscall_error'|
/usr/lib/libpthread.a(ptw-open.o)||In function `__open_nocancel':|
(.text+0x26)||undefined reference to `__syscall_error'|
/usr/lib/libpthread.a(ptw-open.o)||In function `__open_nocancel':|
(.text+0x56)||undefined reference to `__syscall_error'|
/usr/lib/libpthread.a(sigaction.o)||In function `__libc_sigaction':|
(.text+0x46)||undefined reference to `_dl_sysinfo_dso'|
||=== 已完成構建: 12 個錯誤, 0 個警告 ===|
看到這些就暈菜了。那位前輩能指點迷津。感謝。
檔案有預定#include <pthread.h>