MinGW+Posix

MinGW不支持POSIX,需要下载第三方Posix库

 

下载地址:

http://sourceware.org/pthreads-win32/

 

注意:对于上述下载中,已编译好的"libpthreadGC2.a"和"libpthreadGCE2.a"并非真正的静态库,而是从动态库中提取出来的,编译时可以通过,但是运行时还是要加载动态库

 

解决办法:

      step 1: 重新编译libpthread

                 make CROSS=/home/duanbei/mingw32/bin/i386-mingw32- clean GC-static

      seep 2: 调整自己的代码

                 (1) 编译时加上 "-DPTW32_STATIC_LIB" 宏

                 (2) 在代码文件的构造和析构函数中加入如下调用                     

                          BOOL pthread_win32_process_attach_np (void);

                          BOOL pthread_win32_process_detach_np (void);

                          BOOL pthread_win32_thread_attach_np (void); // Currently a no-op

                          BOOL pthread_win32_thread_detach_np (void);

                       eg:

                       static void __attribute__ ((constructor)) _test_constructor() { #ifdef _WIN32 #ifdef PTW32_STATIC_LIB pthread_win32_process_attach_np(); pthread_win32_thread_attach_np(); #endif #endif } static void __attribute__ ((destructor)) _test_destructor() { #ifdef _WIN32 #ifdef PTW32_STATIC_LIB pthread_win32_process_detach_np(); pthread_win32_thread_detach_np(); #endif #endif }

 

感觉阐述的不是太准确,还是把官方文档贴出来

MinGW32 (creates libpthreadGCn.a as a static link lib): make clean GC-static Define PTW32_STATIC_LIB when building your application. Also, your application must call a two non-portable routines to initialise the some state on startup and cleanup before exit. One other routine needs to be called to cleanup after any Win32 threads have called POSIX API routines. See README.NONPORTABLE or the html reference manual pages for details on these routines: BOOL pthread_win32_process_attach_np (void); BOOL pthread_win32_process_detach_np (void); BOOL pthread_win32_thread_attach_np (void); // Currently a no-op BOOL pthread_win32_thread_detach_np (void);

 

你可能感兴趣的:(thread,html,文档,reference,Constructor,destructor)