C++ 如何在 mingw 中静态链接 libwinpthread-1.dll?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13768515/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:41:51  来源:igfitidea点击:

how to do static linking of libwinpthread-1.dll in mingw?

c++mingwstatic-linking

提问by rsk82

I use mingw from here: http://sourceforge.net/projects/mingwbuilds/files/host-windows/releases/4.7.2/32-bit/threads-posix/sjlj/x32-4.7.2-release-posix-sjlj-rev2.7z/download

我从这里使用 mingw: http://sourceforge.net/projects/mingwbuilds/files/host-windows/releases/4.7.2/32-bit/threads-posix/sjlj/x32-4.7.2-release-posix-sjlj-rev2.7z/download

And I've sucessfully managed to link statically libstdc++-6.dlland libgcc_s_sjlj-1.dllby using -static-libgcc -static-libstdc++parameters, but I cannot find a command for doing the same with libwinpthread-1.dll.

而且我已经成功地设法静态链接libstdc++-6.dlllibgcc_s_sjlj-1.dll使用-static-libgcc -static-libstdc++的参数,但我无法找到,做具有相同的命令libwinpthread-1.dll

采纳答案by Tomasz Lis

You should probably check command line options documentation for GCC.

您可能应该检查 GCC 的命令行选项文档。

These's no '-static-something' command, only standard libraries (libgcc and libstdc++) can be set to static linking with one command. For other libraries, you first switch to static linking with "-static" and then list the libraries to include with separate commands, ie "-lpthread".

这些不是“-static-something”命令,只有标准库(libgcc 和 libstdc++)可以通过一个命令设置为静态链接。对于其他库,您首先使用“-static”切换到静态链接,然后列出要包含在单独命令中的库,即“-lpthread”。

回答by rubenvb

If your toolchain includes the static winpthreads, adding the option

如果您的工具链包含静态 winpthreads,则添加选项

-static

Will pull in static versions of all libraries it can.

将拉入所有库的静态版本。

Alternatively, you can remove libwinpthread.dll.a and the DLL itself from the toolchain directories. This might mess up programs linking with libstdc++ and libgcc DLLs though, so be careful.

或者,您可以从工具链目录中删除 libwinpthread.dll.a 和 DLL 本身。不过,这可能会弄乱与 libstdc++ 和 libgcc DLL 链接的程序,所以要小心。

A third option is to use -Wl,-Bdynamicand -Wl,-Bstaticto select which version you want linked in (which is what -staticinternally does when ld is called). An example:

第三个选项是使用-Wl,-Bdynamic-Wl,-Bstatic选择要链接的版本(这-static是调用 ld 时在内部执行的操作)。一个例子:

gcc -o someexec someobject.o -Wl,-Bdynamic -lsomelibIwantshared -Wl,-Bstatic -lsomelibIwantstatic

If you run your link command with -vadded, you should see these options appearing in the ld/collect2 invocation when you use -static-libgccand -static-libstdc++.

如果您使用-vadded运行您的链接命令,您应该会在使用-static-libgcc和时看到这些选项出现在 ld/collect2 调用中-static-libstdc++

回答by Star Brilliant

Try this:

尝试这个:

-static-libgcc -static-libstdc++ -Wl,-Bstatic -lstdc++ -lpthread -Wl,-Bdynamic

Notice the -lstdc++before -lpthread. It worked for me.

注意-lstdc++之前的-lpthread. 它对我有用。

Make sure to add this to the very end of your g++command line.

确保将此添加到g++命令行的最后。

回答by user7023624

To statically link winpthreadeven if threading isn't used in the program, pass the -Bstaticand --whole-archiveparameters to the linker:

即使程序中未使用线程,也要静态链接winpthread,请将-Bstatic--whole-archive参数传递给链接器:

g++ -o hello.exe hello.cpp -Wl,-Bstatic,--whole-archive -lwinpthread -Wl,--no-whole-archive

Note the following:

请注意以下事项:

  • The "whole archive" option should be disabled immediately afterwards.
  • You don't need to do this hack if your program actually uses symbols from the library (i.e. you use <thread>from C++11), in which case the library won't get dropped when you statically link it.
  • This hack is intended for MinGW-w64, to fix the libwinpthread-1.dlldependencies.
  • 之后应立即禁用“整个存档”选项。
  • 如果您的程序实际上使用了库中的符号(即您<thread>从 C++11 中使用),则不需要执行此 hack ,在这种情况下,静态链接库时不会删除该库。
  • 此 hack 适用于 MinGW-w64,以修复libwinpthread-1.dll依赖项。

回答by Syndog

For anyone working in CMake, this solution is readily implemented in your CMakeLists.txt file as follows...

对于在 CMake 中工作的任何人,此解决方案很容易在您的 CMakeLists.txt 文件中实现,如下所示...

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static")

回答by S. Exchange Considered Harmful

Apparently, CMake does some weird stuff with how the -Wlcompiler flags are handled, making the -Wl,-Bstatic -lstdc++ -lwinpthread -Wl,-Bdynamicsolution not work, with only two other options seemingly left: the bad compiler flag -staticand the ugly compiler flag -Wl,--whole-archive.

显然,CMake 对-Wl编译器标志的处理方式做了一些奇怪的事情,使-Wl,-Bstatic -lstdc++ -lwinpthread -Wl,-Bdynamic解决方案不起作用,似乎只剩下另外两个选项:坏编译器标志-static和丑陋的编译器标志-Wl,--whole-archive

Meanwhile, the good option that actually works in CMake, yet seems rather undocumented, is to directly use the linker flags. Hence, in CMake, this seems to be the best way to statically link to all the mingw-w64 C++ dependencies:

同时,在 CMake 中实际工作但似乎相当无证的好选择是直接使用链接器标志。因此,在 CMake 中,这似乎是静态链接到所有 mingw-w64 C++ 依赖项的最佳方式:

target_link_libraries (MyVeryAwesomeApp -static gcc stdc++ winpthread -dynamic)

It should be noted that even if there isn't a library explicitly following -dynamic, it should still be applied in order to ensure that the standard, implicitly linked libraries get linked correctly.

应该注意的是,即使没有明确跟随的库-dynamic,它仍然应该被应用,以确保标准的、隐式链接的库得到正确链接。

回答by Neobry

Just add -staticto your CFLAGS.

只需添加-static到您的CFLAGS.

For example: ./configure CFLAGS="-static".

例如:./configure CFLAGS="-static"

This will link all static libraries to your executable file.

这会将所有静态库链接到您的可执行文件。