[英]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.