C++ 无论代码如何,MinGW .exe 都需要一些 gcc dll?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18138635/
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
MinGW .exe requires a few gcc dll's regardless of the code?
提问by Wolfgang Skyler
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:
使用 MinGW 编译时,我必须在 exe 运行之前从 MinGW bin 目录复制某些 dll 文件(即使使用“-static”和/或“-static-libstdc++”。)我该如何更改?是否有我必须使用的特殊版本的 MinGW?最终,我希望能够仅使用目录中的 exe(并且没有设置 Windows 环境变量)来运行程序。这些文件是:
- libstdc++-6.dll
- libgcc_s_seh-1.dll
- libwinpthread-1.dll
- libstdc++-6.dll
- libgcc_s_seh-1.dll
- libwinpthread-1.dll
And here is the complete list of step's I fallow:
这是我休耕的步骤的完整列表:
- Open Up Code::Blocks
- Select "File->New->Project->Console"
- Fill out the project settings for project "Hello World"
- Right click Project->Build Options...->Hello World (Root target)->Other Options
- Enter "-static" (or "-static-libstdc++") under the already set "-fexceptions"
- CTRL-F9 : Build Project (Without executing)
- Navigate to, in Windows Explorer, and run the built "Hello World.exe" file.
- Click "OK" when a message pop's up saying "Error: libstdc++-6.dll is missing from your computer."
- Copy "libstdc++-6.dll" from the /MinGW/bin/ directory, into the "Hello World.exe" directory.
- Run "Hello World.exe"
- Click "OK" for the message saying "Error: libgcc_s_seh-1.dll is missing from your computer."
- Copy "libgcc_s_seh-1.dll" into the "Hello World.exe" directory.
- Repeat and end up copying "libwinpthread-1.dll" over aswell.
View the message
Hello World!
- 打开代码::块
- 选择“文件->新建->项目->控制台”
- 填写项目“Hello World”的项目设置
- 右键单击 Project->Build Options...->Hello World (Root target)->Other Options
- 在已经设置的“-fexceptions”下输入“-static”(或“-static-libstdc++”)
- CTRL-F9 : 构建项目(不执行)
- 在 Windows 资源管理器中导航到并运行构建的“Hello World.exe”文件。
- 当弹出消息“错误:您的计算机缺少 libstdc++-6.dll”时,单击“确定”。
- 将“libstdc++-6.dll”从/MinGW/bin/目录复制到“Hello World.exe”目录中。
- 运行“Hello World.exe”
- 对于显示“错误:您的计算机中缺少 libgcc_s_seh-1.dll”的消息,单击“确定”。
- 将“libgcc_s_seh-1.dll”复制到“Hello World.exe”目录中。
- 重复并最终复制“libwinpthread-1.dll”。
查看消息
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:
需要上面提到的所有dll文件。而且,为了安全起见,代码是:
// main.cpp
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!" << endl;
return 0;
}
采纳答案by moskito-x
Your commands are wrong !
你的命令是错误的!
Go to the directory where your main.cppfile is, and try the following.
转到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
then you'll no longer need to copy the DLLs (for your Hello World program).
那么您将不再需要复制 DLL(用于您的 Hello World 程序)。
Other notes:
其他注意事项:
The MinGW installation instructions recommends setting
MinGW安装说明推荐设置
c:\minGW;c:\MinGW\bin;
to the PATH environment variable.
到 PATH 环境变量。
Normally the
通常情况下
-static -static-libgcc -static-libstdc++
linker options should work (try all 3 of them at once). But not for libwinpthread-1.dll
.
链接器选项应该有效(一次尝试所有 3 个)。但不是为了libwinpthread-1.dll
.
Also, try to clean
before recompiling.
另外,clean
在重新编译之前尝试。
There's no "-static-something" command.
没有“-static-something”命令。
Only standard libraries libgccand libstdc++can be set to static linking.
只有标准库libgcc和libstdc++可以设置为静态链接。
For other libraries, you first switch to static linking with "-static" and then list the libraries to include with separate commands, i.e. "-lpthread".
对于其他库,您首先使用“-static”切换到静态链接,然后列出要包含在单独命令中的库,即“-lpthread”。
Cmake users should try adding:
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")
回答by J.J. Hakala
-static-libgcc
may be a bad idea if exceptions are used. link options documentationstates that
-static-libgcc
如果使用异常可能是个坏主意。链接选项文档指出
There are several situations in which an application should use the shared libgcc instead of the static version. The most common of these is when the application wishes to throw and catch exceptions across different shared libraries. In that case, each of the libraries as well as the application itself should use the shared libgcc.
在几种情况下,应用程序应该使用共享的 libgcc 而不是静态版本。其中最常见的是当应用程序希望跨不同共享库抛出和捕获异常时。在这种情况下,每个库以及应用程序本身都应该使用共享的 libgcc。