如何让pthreads在Windows中工作?

时间:2023-01-25 07:26:59

I was running into errors such as those mentioned bellow when trying to compile code containing pthreads

当我试图编译包含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'|

I'm running GCC on Windows 7 but I have mingw installed. I'm using the IDE Code::Blocks and select "compile current file". Here is a screen shot of the linker settings, I'm at a loss here

我在Windows 7上运行GCC,但是我已经安装了。我正在使用IDE代码:::Blocks并选择“compile current file”。这是链接器设置的截图,我在这里损失了。

如何让pthreads在Windows中工作?

UPDATE: I added -pthread to the "Other linker options" and it works better. There still are problems. When I compile it says

更新:我在“其他链接器选项”中添加了-pthread,效果更好。仍然有问题。当我编译它的时候。

|In function 'print_message_function':|
warning: control reaches end of non-void function [-Wreturn-type]|

and when I go to run it CodeBlocks says "it appears the program has not been built yet" and when I click on "build" I am shown this error

当我去运行它的时候代码库锁说“看起来程序还没有被构建”当我点击“构建”时,我看到了这个错误

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)

How do I fix this? I want to build/test on Windows but have the program run on a Unix environment. What is the difference between compile and build in an IDE?

我怎么修复这个?我想在Windows上构建/测试,但是让程序在Unix环境中运行。在IDE中编译和构建的区别是什么?

4 个解决方案

#1


3  

It is -lpthread, not -pthread.

它是-lpthread,而不是-pthread。

Edit: Libraries can be added to the compile line in a couple of ways. If we have a file called (for example) /usr/lib/libpthread.so we could include the file like this:

编辑:库可以通过几种方式添加到编译行中。如果我们有一个名为(例如)/usr/lib/libpthread的文件。所以我们可以包括这样的文件:

cc -o myprog /usr/lib/libpthread.so myprog.c

or, alternatively:

或者,或者:

cc -o myprog -lpthread -L /usr/lib myprog.c

Since /usr/lib is a standard directory, we don't normally require the -L option. At runtime we might have to set an environment variable:

因为/usr/lib是一个标准目录,所以我们通常不需要-L选项。在运行时,我们可能需要设置一个环境变量:

export LD_LIBRARY_PATH=/usr/lib

but again, the standard libraries are defaulted, so you don't have to use this unless you are building your own or using 3rd-party libraries.

但同样的,标准库是默认的,所以您不必使用它,除非您正在构建自己的或使用第三方的库。

#2


6  

this answer come late but ... it worked for me, so I decided to share it.

这个答案来晚了,但是…它对我有用,所以我决定分享它。

  1. I downloaded the pthreads library from
    ftp://sourceware.org/pub/pthreads-win32/pthreads-w32-2-9-1-release.zip
  2. 我从ftp://sourceware.org/pub/pthreads-win32/pthread -w32-2-9-1-release.zip下载了pthreads库。
  3. I unzipped it, then I copyed header files (.h) in
    C:\ProgramFiles\CodeBlocks\MinGW\include and library files in C:\ProgramFiles\CodeBlocks\MinGW\lib
  4. 我解压缩了它,然后用C:\编程文件\编程文件\MinGW\包含和库文件C:\编程文件\编程文件\MinGW\lib
  5. I copyed the dll files in the executable folder of my project (myprojects/bin/debug in my case)
  6. 我在项目的可执行文件夹(我的项目是myprojects/bin/debug)中复制dll文件
  7. I added the -lpthreadGC2 option in the
    Settings -> Compiler -> Linker Settings -> Other Linker Options of my Code::Blocks IDE
  8. 我在设置中添加了- lpthreadgc2选项—>编译器—>链接器设置—>我的代码的其他链接器选项::Blocks IDE

Hope this can help.

希望这可以帮助。

#3


2  

warning: control reaches end of non-void function [-Wreturn-type]|

Your main does not return a value. Add return 0; at the end of main.

你的主不返回值。添加返回0;在主要的结尾。

| undefined reference to `_imp__pthread_create'|

You need to link with the thread library. Add -lpthread to the linker command line.

您需要链接到线程库。向链接器命令行添加-lpthread。

#4


2  

Here's what happens currently as of now when using MinGW Installation Manager (the mingw32 package manager for windows) under Windows with the following packages installed:

以下是当前在windows下使用MinGW Installation Manager (windows的mingw32包管理器)时所发生的情况:

  • mingw32-libpthreadgc-dll
  • mingw32-libpthreadgc-dll
  • mingw32-libpthreadgce-dll
  • mingw32-libpthreadgce-dll

ERROR: gcc 5.3.0 fails linking pthread e.g.

错误: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

SOLUTION: include sources from MinGW Package Manager, too, i.e. also select

解决方案:包括来自MinGW包管理器的源代码,即也选择

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

MinGW 4.9.2 does not show this effect. GCC 5.4 on Ubuntu also does not require the pthread sources to compile any code. This one helped me out whilst other tries (using mingw32-libpthread-old or configuring linker settings) failed.

MinGW 4.9.2没有显示这种效果。在Ubuntu上的GCC 5.4也不需要pthread源来编译任何代码。当其他尝试(使用mingw32-libpthread-old或配置链接器设置)失败时,这个方法帮助了我。

#1


3  

It is -lpthread, not -pthread.

它是-lpthread,而不是-pthread。

Edit: Libraries can be added to the compile line in a couple of ways. If we have a file called (for example) /usr/lib/libpthread.so we could include the file like this:

编辑:库可以通过几种方式添加到编译行中。如果我们有一个名为(例如)/usr/lib/libpthread的文件。所以我们可以包括这样的文件:

cc -o myprog /usr/lib/libpthread.so myprog.c

or, alternatively:

或者,或者:

cc -o myprog -lpthread -L /usr/lib myprog.c

Since /usr/lib is a standard directory, we don't normally require the -L option. At runtime we might have to set an environment variable:

因为/usr/lib是一个标准目录,所以我们通常不需要-L选项。在运行时,我们可能需要设置一个环境变量:

export LD_LIBRARY_PATH=/usr/lib

but again, the standard libraries are defaulted, so you don't have to use this unless you are building your own or using 3rd-party libraries.

但同样的,标准库是默认的,所以您不必使用它,除非您正在构建自己的或使用第三方的库。

#2


6  

this answer come late but ... it worked for me, so I decided to share it.

这个答案来晚了,但是…它对我有用,所以我决定分享它。

  1. I downloaded the pthreads library from
    ftp://sourceware.org/pub/pthreads-win32/pthreads-w32-2-9-1-release.zip
  2. 我从ftp://sourceware.org/pub/pthreads-win32/pthread -w32-2-9-1-release.zip下载了pthreads库。
  3. I unzipped it, then I copyed header files (.h) in
    C:\ProgramFiles\CodeBlocks\MinGW\include and library files in C:\ProgramFiles\CodeBlocks\MinGW\lib
  4. 我解压缩了它,然后用C:\编程文件\编程文件\MinGW\包含和库文件C:\编程文件\编程文件\MinGW\lib
  5. I copyed the dll files in the executable folder of my project (myprojects/bin/debug in my case)
  6. 我在项目的可执行文件夹(我的项目是myprojects/bin/debug)中复制dll文件
  7. I added the -lpthreadGC2 option in the
    Settings -> Compiler -> Linker Settings -> Other Linker Options of my Code::Blocks IDE
  8. 我在设置中添加了- lpthreadgc2选项—>编译器—>链接器设置—>我的代码的其他链接器选项::Blocks IDE

Hope this can help.

希望这可以帮助。

#3


2  

warning: control reaches end of non-void function [-Wreturn-type]|

Your main does not return a value. Add return 0; at the end of main.

你的主不返回值。添加返回0;在主要的结尾。

| undefined reference to `_imp__pthread_create'|

You need to link with the thread library. Add -lpthread to the linker command line.

您需要链接到线程库。向链接器命令行添加-lpthread。

#4


2  

Here's what happens currently as of now when using MinGW Installation Manager (the mingw32 package manager for windows) under Windows with the following packages installed:

以下是当前在windows下使用MinGW Installation Manager (windows的mingw32包管理器)时所发生的情况:

  • mingw32-libpthreadgc-dll
  • mingw32-libpthreadgc-dll
  • mingw32-libpthreadgce-dll
  • mingw32-libpthreadgce-dll

ERROR: gcc 5.3.0 fails linking pthread e.g.

错误: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

SOLUTION: include sources from MinGW Package Manager, too, i.e. also select

解决方案:包括来自MinGW包管理器的源代码,即也选择

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

MinGW 4.9.2 does not show this effect. GCC 5.4 on Ubuntu also does not require the pthread sources to compile any code. This one helped me out whilst other tries (using mingw32-libpthread-old or configuring linker settings) failed.

MinGW 4.9.2没有显示这种效果。在Ubuntu上的GCC 5.4也不需要pthread源来编译任何代码。当其他尝试(使用mingw32-libpthread-old或配置链接器设置)失败时,这个方法帮助了我。