[英]How do I get pthreads to work in Windows?
在嘗試編譯包含pthreads的代碼時,我遇到了諸如下面提到的錯誤
warning: return type defaults to 'int' [-Wreturn-type]|
|In function 'print_message_function':|
warning: control reaches end of non-void function [-Wreturn-type]|
| undefined reference to `_imp__pthread_create'|
| undefined reference to `_imp__pthread_create'|
| undefined reference to `_imp__pthread_join'|
| undefined reference to `_imp__pthread_join'|
我在Windows 7上運行GCC,但我安裝了mingw。 我正在使用IDE Code :: Blocks並選擇“編譯當前文件”。 這是鏈接器設置的屏幕截圖,我在這里不知所措
更新 :我添加了-pthread
到“其他鏈接器選項”,它更好地工作。 還有問題。 當我編譯它說
|In function 'print_message_function':|
warning: control reaches end of non-void function [-Wreturn-type]|
當我去運行CodeBlocks說“看起來程序尚未構建”,當我點擊“build”時,我顯示此錯誤
mingw32-g++.exe -o "SimpleExample.exe" "SimpleExample.o" -static-libgcc -static-libstdc++ -pthread
mingw32-g++.exe: error: unrecognized option '-pthread'
Process terminated with status 1 (0 minutes, 0 seconds)
0 errors, 1 warnings (0 minutes, 0 seconds)
我該如何解決? 我想在Windows上構建/測試,但程序在Unix環境下運行。 在IDE中編譯和構建有什么區別?
這個答案來得晚,但......它對我有用,所以我決定分享它。
希望這可以提供幫助。
它是-lpthread
,而不是-pthread。
編輯:可以通過幾種方式將庫添加到編譯行。 如果我們有一個名為(例如) /usr/lib/libpthread.so
的文件,我們可以包含這樣的文件:
cc -o myprog /usr/lib/libpthread.so myprog.c
或者,或者:
cc -o myprog -lpthread -L /usr/lib myprog.c
由於/usr/lib
是標准目錄,因此我們通常不需要-L
選項。 在運行時,我們可能必須設置一個環境變量:
export LD_LIBRARY_PATH=/usr/lib
但同樣,標准庫是默認的,因此除非您構建自己的或使用第三方庫,否則不必使用它。
warning: control reaches end of non-void function [-Wreturn-type]|
您的main
不會返回值。 添加return 0;
在main
結束時。
| undefined reference to `_imp__pthread_create'|
您需要鏈接線程庫。 將-lpthread
添加到鏈接器命令行。
以下是目前在Windows下使用MinGW Installation Manager(Windows的mingw32軟件包管理器)並安裝了以下軟件包時發生的情況:
錯誤:gcc 5.3.0無法鏈接pthread例如
c:/mingw/bin/../lib/gcc/mingw32/5.3.0/../../../../mingw32/bin/ld.exe: cannot find -lpthread
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 0 second
解決方案:也包括MinGW Package Manager的來源,即也選擇
MinGW 4.9.2沒有顯示這種效果。 Ubuntu上的GCC 5.4也不要求pthread源編譯任何代碼。 這個幫助我,而其他嘗試(使用mingw32-libpthread-old或配置鏈接器設置)失敗。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.