无论代码如何,MinGW .exe 都需要一些 gcc dll?

2021-12-25 00:00:00 dll mingw c++

使用 MinGW 编译时,我必须在 exe 运行之前从 MinGW bin 目录中复制某些 dll 文件(即使使用-static"和/或-static-libstdc++"也是如此.)我该如何改变?是否有我必须使用的特殊版本的 MinGW?最终,我希望能够只使用目录中的 exe(并且没有设置 Windows 环境变量)来运行程序.这些文件是:

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. 打开代码::块
  2. 选择文件->新建->项目->控制台"
  3. 填写项目Hello World"的项目设置
  4. 右键单击项目->构建选项...->Hello World(根目标)->其他选项
  5. 在已设置的-fexceptions"下输入-static"(或-static-libstdc++")
  6. CTRL-F9 : 构建项目(不执行)
  7. 在 Windows 资源管理器中导航到并运行构建的Hello World.exe"文件.
  8. 当弹出消息错误:您的计算机缺少 libstdc++-6.dll"时,单击确定".
  9. 将libstdc++-6.dll"从/MinGW/bin/目录复制到Hello World.exe"目录中.
  10. 运行Hello World.exe"
  11. 对于显示错误:您的计算机中缺少 libgcc_s_seh-1.dll"的消息,单击确定".
  12. 将libgcc_s_seh-1.dll"复制到Hello World.exe"目录中.
  13. 重复并最终复制libwinpthread-1.dll".
  14. 查看消息

  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!

我的命令行是:

g++.exe -Wall -fexceptions -static -static-libgcc -static-libstdc++ -g -static-libgcc -static-libstdc++ -L. -c "C:Users\______DesktopHello Worldmain.cpp" -o objDebugmain.o
g++.exe -o "binDebugHello World.exe" objDebugmain.o

需要上面提到的所有dll文件.而且,为了安全起见,代码是:

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;
}

推荐答案

你的命令错了!

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

Go to the directory where your main.cpp file is, and try the following.

g++.exe -Wall -c -g main.cpp -o objDebugmain.o
g++.exe -static -static-libgcc -static-libstdc++ -o "binDebugHello World.exe" objDebugmain.o

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

then you'll no longer need to copy the DLLs (for your Hello World program).

其他注意事项:

MinGW安装说明推荐设置

The MinGW installation instructions recommends setting

c:minGW;c:MinGWin;

到 PATH 环境变量.

to the PATH environment variable.

通常是

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

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

linker options should work (try all 3 of them at once). But not for libwinpthread-1.dll.

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

Also, try to clean before recompiling.

没有-static-something"命令.

There's no "-static-something" command.

只有标准库 libgcc 和 libstdc++ 可以设置为静态链接.

Only standard libraries libgcc and libstdc++ can be set to static linking.

对于其他库,您首先使用-static"切换到静态链接,然后列出要包含在单独命令中的库,即-lpthread".

For other libraries, you first switch to static linking with "-static" and then list the libraries to include with separate commands, i.e. "-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")

相关文章