fx2*_*x23 4 windows dll mingw32 static-linking pthreads-win32
我想将“pthreads for Win32”与我的应用程序静态链接,用 MinGW32 编译,这样应用程序就不需要 pthreadGC2.dll 来运行。
我正在使用最新版本的 pthreads - 2.9.1,从这里下载,lib 和包含文件被复制到 MinGW lib 和包含目录。
在网上搜索如何做到这一点时,我偶然发现了这个线程,该线程指示使用 -static-libgcc -static-libstdc++ 标志。这不起作用,这意味着应用程序可以编译,但在没有 pthreadGC2.dll 的情况下无法运行。
他们还建议使用 -static -static-libgcc -static-libstdc++。这不会编译并出现以下错误:
main.o:main.cpp:(.text+0x461): undefined reference to `_imp__pthread_create'
main.o:main.cpp:(.text+0x4be): undefined reference to `_imp__pthread_join'
Run Code Online (Sandbox Code Playgroud)
有谁知道怎么做?
顺便说一句 - 将 pthreadGC2.dll 从 2.9.1 版本(在 pthreads-w32-2.9.1-1-mingw32-dll.tar.lzma 内)下载到应用程序的文件夹是不够的。要运行该应用程序,我还应该复制另一个 dll:libgcc_s_dw2-1.dll。这对我来说真的很糟糕 - 我不想让我的用户每次想要运行我的应用程序时都拥有这 2 个 DLL
这是我的代码:
#include <stdio.h>
#include <pthread.h>
static void* threadMain(void* param)
{
printf("thread start\n");
for (int i = 0; i < 2; i++)
Sleep(1);
printf("thread end\n");
return NULL;
}
int main(int argc, char* argv[])
{
pthread_t thread;
int err = pthread_create(&thread, NULL, threadMain, NULL);
if (err != 0)
{
printf("Cannot create thread: %s", strerror(err));
return;
}
printf("thread created\n");
Sleep(1);
pthread_join(thread, NULL);
printf("thread joined\n");
}
Run Code Online (Sandbox Code Playgroud)
和我的makefile:
all:
g++.exe -DHAVE_STRUCT_TIMESPEC -c -o main.o main.cpp
g++.exe -static-libgcc -static-libstdc++ -o link_pthreads.exe main.o -lpthread
clean:
del main.o
del link_pthreads.exe
Run Code Online (Sandbox Code Playgroud)
小智 6
使用 MinGW 的新更新版本,您可以获得 pthread-win32 库的 2.10 版。在几个更改和错误修复中,您可以找到静态链接它的可能性。您只需要在静态库列表中包含 -lpthread。
以下是混合静态链接与 pthread 库和动态链接与 Windows 库的示例:
mingw32-gcc.exe -o sample.exe sample.c -Wl,-Bstatic -lpthread -Wl,-Bdynamic -lws2_32
Run Code Online (Sandbox Code Playgroud)
这样,您还可以释放对 libgcc_s_dw2-1.dll 的依赖
| 归档时间: |
|
| 查看次数: |
3548 次 |
| 最近记录: |