Gcc 如何修复对`_imp_pthread_create';
我在windows7 32位上使用MinGW。 我无法编译使用pthread的源代码 我的代码如下Gcc 如何修复对`_imp_pthread_create';,gcc,linker,pthreads,mingw,Gcc,Linker,Pthreads,Mingw,我在windows7 32位上使用MinGW。 我无法编译使用pthread的源代码 我的代码如下 #include <stdio.h> #include <pthread.h> int main(int argc, char** argv) { (void)argv; printf("######## start \n"); #ifndef pthread_create return ((int*)(&pthread_create))[argc];
#include <stdio.h>
#include <pthread.h>
int main(int argc, char** argv)
{
(void)argv;
printf("######## start \n");
#ifndef pthread_create
return ((int*)(&pthread_create))[argc];
#else
(void)argc;
return 0;
#endif
}
我使用以下pthread库
pthreads-w32-2.8.0-3-mingw32-dev
下面是/usr/local/lib中的libpthread.dll.a
有人知道如何解决这个问题吗?命令行:
gcc -I /usr/local/include -L /usr/local/lib/libpthread.dll.a trylpthread.c
没有道理
-L
是一个链接器选项,用于指示链接器搜索所需的库
在目录
中。因此,您告诉链接器在中搜索所需的库
路径/usr/local/lib/libpthread.dll.a
,它不是目录,而另一方面
您根本没有告诉链接器链接任何库。这就是为什么它找不到任何证据
\u imp\u pthread\u create
的定义
你发布的程序也没有意义。台词:
#ifndef pthread_create
return ((int*)(&pthread_create))[argc];
#else
(void)argc;
return 0;
#endif
说:-
如果我没有定义预处理器宏pthread\u create
,则编译:
return ((int*)(&pthread_create))[argc];
否则,请编译:
(void)argc;
return 0;
如果您定义了一个预处理器宏pthread\u create
,例如
#define pthread_create whatever
那么您要编译的代码将是:
(void)argc;
return 0;
return ((int*)(&whatever))[argc];
由于您确实没有定义任何此类宏,因此您编译的代码是:
return ((int*)(&pthread_create))[argc];
正如您所看到的,在连接时失败。如果该代码是用这样定义的pthread\u create
编译的,
它将是:
(void)argc;
return 0;
return ((int*)(&whatever))[argc];
将程序改写为:
#include <stdio.h>
#include <pthread.h>
int main(int argc, char** argv)
{
(void)argv;
printf("######## start \n");
return ((int*)(&pthread_create))[argc];
}
链接:
gcc -o trylpthread.exe trylpthread.o /usr/local/lib/libpthread.dll.a
gcc -o trylpthread.exe trylpthread.o -pthread
请记住,当您编译并链接程序时,相应的pthreadGC???.dll
必须在运行时在程序加载程序搜索DLL的位置之一找到
更好的方法是卸载MinGW和pthreads-w32-2.8.0-3-mingw32-dev
和
安装GCC的最新Windows端口,例如(最简单)或。选择32位版本,如果您的Windows系统
是32位的。这些工具链具有内置的pthread
支持,正如GCC标准所做的那样
编译时使用:
gcc -Wall -I /usr/local/include -o trylpthread.o -c trylpthread.c
gcc -Wall -o trylpthread.o -c trylpthread.c
链接:
gcc -o trylpthread.exe trylpthread.o /usr/local/lib/libpthread.dll.a
gcc -o trylpthread.exe trylpthread.o -pthread
(不是
-lpthread
)我可以兼容gcc-I/usr/local/include trylpthread.c/usr/local/lib/libpthread.dll.a
。在mingw-get重新安装pthread之后,我还可以编译gcc-I/usr/local/include trylpthread.c-pthread。谢谢