简体   繁体   English

使用 MSYS2 编译 C++ Windows exe 独立文件

[英]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:以下是所有细节,以更好地了解我的情况:

  1. g++ HelloWord.cpp -o Helloword is the line I use to compile g++ HelloWord.cpp -o Helloword是我用来编译的那行
  2. C:\msys64\mingw64\bin here's the path where g++ is stored C:\msys64\mingw64\bin这里是存放g++的路径
  3. All the exact error messages I receive from windows after double clicking on the exe file that has been generated.双击已生成的 exe 文件后,我从 windows 收到的所有确切错误消息。 Note that these messages do not appear on the CMD, but in a classic Windows error pop-up:请注意,这些消息不会出现在 CMD 上,而是出现在经典的 Windows 错误弹出窗口中:
  • The program can't start because msys-2.0.dll is missing from your computer.程序无法启动,因为您的计算机中缺少 msys-2.0.dll。 Try reinstalling the program to fix this problem.尝试重新安装程序以解决此问题。
  • The program can't start because libstdc++-6.dll is missing from your computer.程序无法启动,因为您的计算机缺少 libstdc++-6.dll。 Try reinstalling the program to fix this problem.尝试重新安装程序以解决此问题。
  • The program can't start because libgcc_s_seh-1.dll is missing from your computer.该程序无法启动,因为您的计算机中缺少 libgcc_s_seh-1.dll。 Try reinstalling the program to fix this problem.尝试重新安装程序以解决此问题。

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 已经解决了他们的问题,但对于其他发现此问题的人来说:

  1. Make sure you launch MSYS2 by clicking on mingw64.exe or the equivalent shortcut.确保通过单击mingw64.exe或等效快捷方式启动 MSYS2。
  2. Run 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安装工具链。
  3. Compile your code with g++ and use the -static option.使用g++编译您的代码并使用-static选项。
  4. Run 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM