block pthread示例无法编译
Code::Blocks pthread example can not compile
也许这个问题很容易,但我不知道如何解决它:我尝试编译一个在windows 7(64位)上具有pthreas
的示例,使用代码块我下载了预构建库并设置了building_options:编译器pthreadLibinclude
和链接器pthreadLiblibx64
的路径程序是:
extern "C"
{
#include <pthread.h>
#include <unistd.h>
}
#include <iostream>
#include <windows.h>
using namespace std ;
void * function1(void * argument);
void * function2(void * argument);
int main( void )
{
pthread_t t1, t2 ; // declare 2 threads.
pthread_create( &t1, NULL, function1,NULL); // create a thread running function1
pthread_create( &t2, NULL, function2,NULL); // create a thread running function2
Sleep(1);
return 0;
}
void * function1(void * argument)
{
cout << " hello " << endl ;
Sleep(2); // fall alseep here for 2 seconds...
return 0;
}
void * function2(void * argument)
{
cout << " world " << endl ;
return 0;
}
如果我注释了pthread_create();
函数,那么它将构建。因此,pthread_t
被识别为一种类型。当我尝试用pthread_create
编译时,我得到一个错误:
mingw32-g++.exe -L..libsPre-built.2libx64 -LD:DropBoxWorkUTProgramsMyODPlibs -o binReleaseRIP.exe objReleasemain.o -s ..libsPre-built.2libx64libpthreadGC2.a ..libsPre-built.2libx64pthreadVC2.lib
objReleasemain.o:main.cpp:(.text.startup+0x36): undefined reference to `_imp__pthread_create'
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 0 second(s))
1 error(s), 1 warning(s) (0 minute(s), 0 second(s))
我必须在C::B
中进行额外设置吗?我尝试添加链接器命令-lpthread
,但无法识别。
两天后我就明白了。
首先:我安装了Windows操作系统的minGW 64。接下来:在这篇文章之后,我设置C::B
使用minGW_64
。此外:我添加了链接库..libsPre-built.2libx64libpthreadGC2.a
和..libsPre-built.2libx64pthreadVC2.lib
。最后,我添加到我的项目pthreadGC2.dll
(64位版本!)。
吸取教训,不要混用lib。
Linux用sleep(1)代替sleep(1)