Maybe this questions is easy but I can't figure it out how to resolve it: I try to compile an example which has pthreas on windows 7 (64bit), with Code Blocks I downloaded the prebuild library and set up building_options: the path for the compiler pthreadLib\include and the linker to pthreadLib\lib\x64 The program is:

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;
}

In case I comment pthread_create(); function, than it builds. So pthread_t is recognized as a type. When I try to compile with pthread_create I get an error:

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))

Do I have to make additional setups in C::B? I tired to add the linker command -lpthread but is not recognized.

有帮助吗?

解决方案

After 2 days I figured it out.

First: I installed the minGW 64 for Windows OS. Next: I set up C::B to use the minGW_64 after this post. Moreover: I added to the linker libs ..\libs\Pre-built.2\lib\x64\libpthreadGC2.a and ..\libs\Pre-built.2\lib\x64\pthreadVC2.lib. Finally, I added to my project the pthreadGC2.dll (64bit version!).

Lesson learned, don't mix lib. and compiler with 86 and 64.

其他提示

For Linux Use sleep(1) in stead of Sleep(1)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top