Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows pthreads与MinGW的静态链接_Windows_Dll_Static Linking_Mingw32_Pthreads Win32 - Fatal编程技术网

Windows pthreads与MinGW的静态链接

Windows pthreads与MinGW的静态链接,windows,dll,static-linking,mingw32,pthreads-win32,Windows,Dll,Static Linking,Mingw32,Pthreads Win32,我想将“pthreads for Win32”与我的应用程序静态链接,并使用MinGW32编译,这样应用程序就不需要pthreadGC2.dll来运行 我使用的是最新版本的pthreads-2.9.1,从和lib下载,并将include文件复制到MinGW lib和include目录 在搜索web时,我偶然发现了使用-static libgcc-static libstdc++标志的指令。这不起作用,这意味着应用程序可以编译,但如果没有pthreadGC2.dll,则无法运行 他们还建议使用-s

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

我使用的是最新版本的pthreads-2.9.1,从和lib下载,并将include文件复制到MinGW lib和include目录

在搜索web时,我偶然发现了使用-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-dll.tar.lzma内部)下载到应用程序文件夹的pthreadGC2.dll是不够的。要运行该应用程序,我还应该复制另一个dll:libgcc_s_dw2-1.dll。这对我来说真的很糟糕-我不想让我的用户每次运行我的应用程序时都有这两个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");
}

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

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

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

这样,您还可以释放对libgcc_s_dw2-1.dll的依赖关系。考虑使用mingw-w64,它附带内置pthread。这对我来说是个问题,因为我的应用程序在mingw-64上还有其他编译问题。所以我真的需要mingw-32也许可以解决这些问题-mingw已经不存在了你是什么意思mingw已经不存在了?你是说mingw-32还是一般的mingw?mingw没有得到很好的维护。有一个保养良好的叉子,mingw-w64。“mingw-32”不是什么东西。术语“mingw32”是一个用于32位和64位的ABI名称。感谢Emilio对我的帮助!
mingw32-gcc.exe -o sample.exe sample.c -Wl,-Bstatic -lpthread -Wl,-Bdynamic -lws2_32