【问题标题】:pthreads static linking with MinGWpthreads 与 MinGW 的静态链接
【发布时间】:2017-01-06 01:17:47
【问题描述】:

我想将“pthreads for Win32”与我的应用程序静态链接,使用 MinGW32 编译,这样应用程序就不需要 pthreadGC2.dll 来运行。

我正在使用最新版本的 pthreads - 2.9.1,从 here 下载,并将 lib 和包含文件复制到 MinGW 的 lib 和包含目录中。

在网上搜索如何做到这一点时,我偶然发现了this thread,它指示使用 -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'

有人知道怎么做吗?

顺便说一句 - 将从 2.9.1 版本(在 pthreads-w32-2.9.1-1-mingw32-dl​​l.tar.lzma 中)下载的 pthreadGC2.dll 复制到应用程序的文件夹是不够的。要运行该应用程序,我还应该复制另一个 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");
}

还有我的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

【问题讨论】:

  • 考虑使用mingw-w64,它自带pthreads
  • 这对我来说是个问题,因为我的应用程序在使用 mingw-64 时存在其他编译问题。所以我真的需要mingw-32
  • 也许可以解决这些问题 - mingw 已失效
  • 你的意思是 mingw 已经失效了?你的意思是 mingw-32 还是一般的 mingw?
  • mingw 没有得到很好的维护。有一个维护良好的前叉,mingw-w64。 “mingw-32”不是一个东西。术语“mingw32”是用于 32 位和 64 位的 ABI 名称。

标签: windows dll static-linking mingw32 pthreads-win32


【解决方案1】:

使用 MinGW 的新更新版本,您可以获得 2.10 版本的 pthread-win32 库。在几个更改和错误修复中,您可以找到静态链接它的可能性。 您只需要在静态库列表中包含 -lpthread。

以下是将静态链接与 pthread 库和动态链接与 Windows 库混合的示例:

mingw32-gcc.exe -o sample.exe sample.c -Wl,-Bstatic -lpthread -Wl,-Bdynamic -lws2_32

这样,你也释放了对 libgcc_s_dw2-1.dll 的依赖

【讨论】:

  • 感谢 Emilio 对我的帮助很大!
猜你喜欢
  • 2012-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-04
  • 2011-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多