[英]Compile C++ Windows exe standalone files with MSYS2
I've recently started using Msys2 to install gcc compiler to make some exe for Windows.我最近开始使用 Msys2 安装 gcc 编译器来为 Windows 制作一些 exe。 It works very well, but there's a problem when passing my exe to my brother.
它工作得很好,但是将我的 exe 传递给我的兄弟时出现了问题。 His laptop has not msys2 installed and when he tries to run my exe some errors occur.
他的笔记本电脑没有安装 msys2,当他尝试运行我的 exe 时会出现一些错误。 Seems like few dll files are necessary to use my exe (like msys-2.0.dll).
似乎很少需要 dll 文件才能使用我的 exe(如 msys-2.0.dll)。
I've found out that those files are used by msys2 to "fake" the OS on the machine pretending it's a POSIX one.我发现 msys2 使用这些文件来“伪造”机器上的操作系统,假装它是 POSIX 操作系统。 Is there a way to compile standalone exe for windows with msys2?
有没有办法用 msys2 为 windows 编译独立的 exe? I would like my brother to be able to use my exe without installing msys or else.
我希望我的兄弟能够在不安装 msys 或其他的情况下使用我的 exe。
Here are all the details to understand better my situation:以下是所有细节,以更好地了解我的情况:
g++ HelloWord.cpp -o Helloword
is the line I use to compile g++ HelloWord.cpp -o Helloword
是我用来编译的那行C:\msys64\mingw64\bin
here's the path where g++ is stored C:\msys64\mingw64\bin
这里是存放g++的路径 Fixed: I've resolved the issue just using the g++ parameter -static
.已修复:我仅使用 g++ 参数
-static
解决了该问题。 Is it an overkill?是不是矫枉过正?
My version of MinGW is a bit old...我的 MinGW 版本有点老了……
C:\example>where g++
C:\misc\mingw810_64\bin\g++.exe
C:\example>g++ --version
g++ (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 8.1.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
But same idea:但同样的想法:
C:\example>cat > compile_me.cpp
#include <iostream>
int main () { std::cout << "hi" << std::endl; }
^Z
C:\example>g++ compile_me.cpp -o compiled.exe
C:\example>compiled.exe
hi
C:\example>dumpbin /dependents compiled.exe
...
Image has the following dependencies:
KERNEL32.dll
msvcrt.dll
libstdc++-6.dll
...
In that case (dynamically linked stdlib) you'd deploy libstdc++6.dll with the executable, installing it to the same path as the exe (the other two are generally present in the windows system path).在这种情况下(动态链接的标准库),您将使用可执行文件部署 libstdc++6.dll,将其安装到与 exe 相同的路径(其他两个通常存在于 windows 系统路径中)。
If you want to drop that dependency, use -static
:如果要删除该依赖项,请使用
-static
:
C:\example>g++ compile_me.cpp -o compiled.exe -static
C:\example>compiled.exe
hi
C:\example>dumpbin /dependents compiled.exe
...
Image has the following dependencies:
KERNEL32.dll
msvcrt.dll
...
Deploying that.exe alone should be fine.单独部署 that.exe 应该没问题。
The file size will be larger but that's not a huge deal these days.文件大小会更大,但如今这并不是什么大不了的事。 Also your MinGW / MSYS install might come with strip:
此外,您的 MinGW / MSYS 安装可能带有条带:
C:\example>dir compiled.exe
Volume in drive C is Windows
Volume Serial Number is D2BA-C6F0
Directory of C:\example
09/24/2022 06:49 PM 2,389,120 compiled.exe
1 File(s) 2,389,120 bytes
0 Dir(s) 135,945,314,304 bytes free
C:\example>strip compiled.exe
C:\example>dir compiled.exe
Volume in drive C is Windows
Volume Serial Number is D2BA-C6F0
Directory of C:\example
09/24/2022 07:03 PM 838,656 compiled.exe
1 File(s) 838,656 bytes
0 Dir(s) 135,944,765,440 bytes free
C:\example>compiled.exe
hi
If there are other dynamic libraries that your particular executable ends up depending on, and the vendor has chosen not to provide statically linked alternatives, then you'll have to just deploy them with the exe.如果您的特定可执行文件最终依赖于其他动态库,并且供应商选择不提供静态链接的替代方案,那么您只需将它们与 exe 一起部署即可。 It's generally easy enough to just throw everything in a zip file or use your favorite scriptable installer.
将所有内容放入 zip 文件或使用您最喜欢的可脚本化安装程序通常很容易。
(Note: dumpbin ships with Visual Studio; and can be found in some appropriate subdirectory in VC\Tools in the vs install path). (注意:dumpbin 随 Visual Studio 提供;并且可以在 VC\Tools 的 vs 安装路径中的某个适当子目录中找到)。
MSYS2 compiles native PE32 executables. MSYS2 编译本机 PE32 可执行文件。 It does not rely on any magic msys environment or static linking.
它不依赖于任何神奇的 msys 环境或 static 链接。
Get yourself a dependency walker and look to see what DLLs your app needs to run.让自己成为一个依赖漫游器,看看你的应用程序需要运行哪些 DLL。 Anything not in a Windows subdirectory should be where you focus your attention.
不在 Windows 子目录中的任何内容都应该是您关注的地方。 Also make sure your app does not require any special Microsoft redistributable dependencies.
还要确保您的应用不需要任何特殊的 Microsoft 可再发行依赖项。
Ultimately, you should be creating an installer for your application to handle dependencies.最终,您应该为您的应用程序创建一个安装程序来处理依赖项。 I personally like Inno setup, but plenty of others exist that are well liked also.
我个人喜欢 Inno 设置,但还有很多其他的设置也很受欢迎。
The OP has solved their problem but for anyone else who finds this: OP 已经解决了他们的问题,但对于其他发现此问题的人来说:
mingw64.exe
or the equivalent shortcut.mingw64.exe
或等效快捷方式启动 MSYS2。which g++
to make sure you are using /mingw64/bin/g++
.which g++
以确保您使用的是/mingw64/bin/g++
。 If it shows some other g++
then run pacman -S mingw-w64-x86_64-toolchain
to install the toolchain.g++
然后运行pacman -S mingw-w64-x86_64-toolchain
安装工具链。g++
and use the -static
option.g++
编译您的代码并使用-static
选项。ntldd yourprogram.exe
to check which DLLs your program is using and make sure they are part of Windows, not part of MSYS2.ntldd yourprogram.exe
以检查您的程序正在使用哪些 DLL,并确保它们是 Windows 的一部分,而不是 MSYS2 的一部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.