67

When compiling with MinGW, I have to copy over certain dll files from the MinGW bin directory before the exe will run (Even when using "-static" and/or "-static-libstdc++".) How do I change that? Is there a special build of MinGW that I have to use? Ultimately I want to be able to run the program with nothing but the exe in the directory (and no windows environment variables set.) These File's are:

  • libstdc++-6.dll
  • libgcc_s_seh-1.dll
  • libwinpthread-1.dll

And here is the complete list of step's I fallow:

  1. Open Up Code::Blocks
  2. Select "File->New->Project->Console"
  3. Fill out the project settings for project "Hello World"
  4. Right click Project->Build Options...->Hello World (Root target)->Other Options
  5. Enter "-static" (or "-static-libstdc++") under the already set "-fexceptions"
  6. CTRL-F9 : Build Project (Without executing)
  7. Navigate to, in Windows Explorer, and run the built "Hello World.exe" file.
  8. Click "OK" when a message pop's up saying "Error: libstdc++-6.dll is missing from your computer."
  9. Copy "libstdc++-6.dll" from the /MinGW/bin/ directory, into the "Hello World.exe" directory.
  10. Run "Hello World.exe"
  11. Click "OK" for the message saying "Error: libgcc_s_seh-1.dll is missing from your computer."
  12. Copy "libgcc_s_seh-1.dll" into the "Hello World.exe" directory.
  13. Repeat and end up copying "libwinpthread-1.dll" over aswell.
  14. View the message

    Hello World!
    

Edit: My command line is:

g++.exe -Wall -fexceptions -static -static-libgcc -static-libstdc++ -g -static-libgcc -static-libstdc++ -L. -c "C:\Users\______\Desktop\Hello World\main.cpp" -o obj\Debug\main.o
g++.exe -o "bin\Debug\Hello World.exe" obj\Debug\main.o

With all the dll files mentioned above required. And, just to be safe, the code is:

// main.cpp
#include <iostream>

using namespace std;

int main()
{
    cout << "Hello world!" << endl;
    return 0;
}
4

3 回答 3

61

你的命令是错误的!

转到main.cpp文件所在的目录,然后尝试以下操作。

g++.exe -Wall -c -g main.cpp -o obj\Debug\main.o
g++.exe -static -static-libgcc -static-libstdc++ -o "bin\Debug\Hello World.exe" obj\Debug\main.o

那么您将不再需要复制 DLL(用于您的 Hello World 程序)。

其他注意事项:

MinGW 安装说明建议设置

c:\minGW;c:\MinGW\bin;

到 PATH 环境变量。

通常情况下

-static -static-libgcc -static-libstdc++

链接器选项应该可以工作(一次尝试所有 3 个)。但不是为了libwinpthread-1.dll

clean另外,请在重新编译之前尝试。

没有“-static-something”命令。

只有标准库libgcclibstdc++可以设置为静态链接。

对于其他库,您首先使用“-static”切换到静态链接,然后使用单独的命令(即“-lpthread”)列出要包含的库。

Cmake 用户应该尝试添加:

set(CMAKE_CXX_STANDARD_LIBRARIES "-static-libgcc -static-libstdc++ -lwsock32 -lws2_32 ${CMAKE_CXX_STANDARD_LIBRARIES}")

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-Bstatic,--whole-archive -lwinpthread -Wl,--no-whole-archive")
于 2013-08-09T01:39:05.017 回答
10

-static-libgcc如果使用异常可能是个坏主意。链接选项文档指出

在几种情况下,应用程序应该使用共享的 libgcc 而不是静态版本。其中最常见的是当应用程序希望跨不同的共享库抛出和捕获异常时。在这种情况下,每个库以及应用程序本身都应该使用共享的 libgcc。

于 2016-05-22T05:51:52.930 回答
0

上面答案的评论包含完整的解决方案,所以我只想添加 CodeBlocks 透视图。我在 Windows7 x64 上使用 CodeBlocks16 和 MinGW-W64 8.1.0 ''i686-posix-dwarf'' 验证了它。

这解决了 OPs 问题

  • 创建新项目并将其命名为“Hello World”

  • 接受向导中的所有默认值

  • 选择 Project/BuildOptions/ 并选择“Hello World”。Out 编辑对 Debug 和 Release 都有效

  • 在“链接器”选项卡的“其他链接器选项”中添加以下内容

    -static
    -static-libgcc
    -static-strc++
    -lwinpthread
    
  • 在工具栏上选择“调试”并按构建(黄色齿轮图标)

  • 按绿色运行图标并确认构建正常

测试

  • 打开终端窗口并转到 HelloWorld\bin\debug 文件夹
  • 在那里开始你好世界。
  • 确认它可以在不要求任何 DLL 的情况下工作。

您也可以从资源管理器窗口启动它并确认它也不需要 DLL。

注意:在我的 Win7x64 系统上,当从资源管理器启动 HelloWorld.exe 时,添加“-lwinpthread”行会导致 CodeBlocks 忽略“Projects/Properties/Tab_BuildTargets/“执行结束时暂停”中的设置。所以“Hello World”输出几乎不可见,因为窗口在执行后立即关闭(也许有人知道为什么)

请注意,如果您没有找到 OP的winpthread.dll问题,那么您可能不会使用带有“posix”线程模型的 MinGW-W64 编译器。两个代码块 MinGW-W64 捆绑安装包都使用此类版本。对于 CB20.03,MinGW-W64 下载页面的相关下载将是

  • 32 位:8.1.0 ''i686-posix-dwarf''
  • 64 位:8.1.0 ''x86_64-posix-seh''

例如,如果我直接使用 Codeblocks 设置设置编译器并选择 32 位编译器包“i686-win32-dwarf”,则只有前 2 个 DLL 会丢失。在这种情况下,解决方法是将链接器选项设置为

  -static-libgcc
  -static-strc++
于 2021-01-09T17:22:28.400 回答