簡體   English   中英

如何讓pthreads在Windows中工作?

[英]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並選擇“編譯當前文件”。 這是鏈接器設置的屏幕截圖,我在這里不知所措

codeblocks鏈接器

更新 :我添加了-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中編譯和構建有什么區別?

這個答案來得晚,但......它對我有用,所以我決定分享它。

  1. 我從中下載了pthreads庫
    ftp://sourceware.org/pub/pthreads-win32/pthreads-w32-2-9-1-release.zip
  2. 我解壓縮它,然后我復制了頭文件(.h)
    C:\\ ProgramFiles \\ CodeBlocks \\ MinGW \\ include和庫文件在C:\\ ProgramFiles \\ CodeBlocks \\ MinGW \\ lib中
  3. 我將dll文件復制到我項目的可執行文件夾中(myprojects / bin / debug)
  4. 我在中添加了-lpthreadGC2選項
    設置 - >編譯器 - >鏈接器設置 - >我的代碼的其他鏈接器選項::阻止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軟件包管理器)並安裝了以下軟件包時發生的情況:

  • mingw32的-libpthreadgc的DLL
  • mingw32的-libpthreadgce的DLL

錯誤: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的來源,即也選擇

  • mingw32的-libpthreadgc-dev的
  • mingw32的-libpthreadgce-dev的

MinGW 4.9.2沒有顯示這種效果。 Ubuntu上的GCC 5.4也不要求pthread源編譯任何代碼。 這個幫助我,而其他嘗試(使用mingw32-libpthread-old或配置鏈接器設置)失敗。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM