也许这个问题很容易,但我无法弄清楚如何解决它:
我尝试编译一个在 Windows 7 (64位)上有pthreas
的示例,代码块我下载了预构建库并设置了 building_options < / strong>:编译器pthreadLib\include
的路径和pthreadLib\lib\x64
的链接器
该计划是:
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..\libs\Pre-built.2\lib\x64 -LD:\DropBox\WorkUT\Programs\MyODP\libs -o bin\Release\RIP.exe obj\Release\main.o -s ..\libs\Pre-built.2\lib\x64\libpthreadGC2.a ..\libs\Pre-built.2\lib\x64\pthreadVC2.lib
obj\Release\main.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
,但无法识别。