C++ 如何使用 gcc/g++ 在 Linux 上为 Windows 编译?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2033997/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:53:59  来源:igfitidea点击:

How to compile for Windows on Linux with gcc/g++?

c++openglgccglutfreeglut

提问by Louise

I have written some effects in C++ (g++) using freeglut on Linux, and I compile them with

我在 Linux 上使用 freeglut 在 C++ (g++) 中编写了一些效果,并使用

g++ -Wall -lglut part8.cpp -o part8

So I was wondering if it is possible to have g++ make static compiled Windows executables that contains everything needed?

所以我想知道是否有可能让 g++ 生成包含所需一切的静态编译 Windows 可执行文件?

I don't have Windows, so it would be really cool, if I could do that on Linux :)

我没有 Windows,所以如果我能在 Linux 上做到这一点,那就太酷了 :)

采纳答案by AndiDog

mingw32 exists as a package for Linux. You can cross-compile and -link Windows applications with it. There's a tutorial here at the Code::Blocks forum. Mind that the command changes to x86_64-w64-mingw32-gcc-win32, for example.

mingw32 作为 Linux 的一个包存在。您可以使用它交叉编译和链接 Windows 应用程序。Code::Blocks 论坛上有一个教程。例如,请注意命令更改x86_64-w64-mingw32-gcc-win32为 。

Ubuntu, for example, has MinGW in its repositories:

例如,Ubuntu 的存储库中有 MinGW:

$ apt-cache search mingw
[...]
g++-mingw-w64 - GNU C++ compiler for MinGW-w64
gcc-mingw-w64 - GNU C compiler for MinGW-w64
mingw-w64 - Development environment targeting 32- and 64-bit Windows
[...]

回答by aggregate1166877

One option of compiling for Windows in Linux is via mingw. I found a very helpful tutorial here.

在 Linux 中为 Windows 编译的一种选择是通过 mingw。我在这里找到了一个非常有用的教程。

To install mingw32 on Debian based systems, run the following command:
sudo apt-get install mingw32 mingw32-binutils mingw32-runtime

要在基于 Debian 的系统上安装 mingw32,请运行以下命令:
sudo apt-get install mingw32 mingw32-binutils mingw32-runtime

To compile your code, you can use something like:
i586-mingw32msvc-g++ -o myApp.exe myApp.cpp

要编译您的代码,您可以使用以下内容:
i586-mingw32msvc-g++ -o myApp.exe myApp.cpp

You'll sometimes want to test the new Windows application directly in Linux. You can use wine for that, although you should always keep in mind that wine could have bugs. This means that you might not be sure that a bug is in wine, your program, or both, so only use wine for general testing.

有时您会想直接在 Linux 中测试新的 Windows 应用程序。您可以为此使用葡萄酒,但您应该始终牢记葡萄酒可能存在缺陷。这意味着您可能不确定 wine、您的程序或两者中是否存在错误,因此仅将 wine 用于一般测试。

To install wine, run:
sudo apt-get install wine

要安装 wine,请运行:
sudo apt-get install wine

回答by 0x8BADF00D

Suggested method gave me error on Ubuntu 16.04: E: Unable to locate package mingw32

建议的方法在 Ubuntu 16.04 上给了我错误: E:无法找到包 mingw32

===========================================================================

================================================== ==========================

To install this package on Ubuntu please use following:

要在 Ubuntu 上安装此软件包,请使用以下命令:

sudo apt-get install mingw-w64

After install you can use it:

安装后就可以使用了:

x86_64-w64-mingw32-g++

Please note!

请注意!

For 64-bit use: x86_64-w64-mingw32-gc++

对于 64 位使用: x86_64-w64-mingw32-gc++

For 32-bit use: i686-w64-mingw32-g++

对于 32 位使用: i686-w64-mingw32-g++

回答by Pedro Miranda

Install a cross compiler, like mingw64 from your package manager. Then compile in the following way: instead of simply calling gcccall i686-w64-mingw32-gccfor 32-bit Windows or x86_64-w64-mingw32-gcc"for 64-bit Windows. I would also use the --staticoption, as the target system may not have all the libraries.

从你的包管理器安装一个交叉编译器,比如 mingw64。然后按以下方式编译:而不是简单地为 32 位 Windows 或64 位 Windows调用gcccall 。我也会使用这个选项,因为目标系统可能没有所有的库。i686-w64-mingw32-gccx86_64-w64-mingw32-gcc"--static

If you want to compile other language, like Fortran, replace -gccwith -gfortranin the previous commands.

如果要编译其他语言,如 Fortran,请将前面的命令中的-gccwith替换-gfortran

回答by Richard Pennington

I've used mingw on Linux to make Windows executables in C, I suspect C++ would work as well.

我在 Linux 上使用 mingw 用 C 制作 Windows 可执行文件,我怀疑 C++ 也能工作。

I have a project, ELLCC, that packages clang and other things as a cross compiler tool chain. I use it to compile clang (C++), binutils, and GDB for Windows. Follow the download link at ellcc.orgfor pre-compiled binaries for several Linux hosts.

我有一个项目 ELLCC,它将 clang 和其他东西打包为交叉编译器工具链。我用它来为 Windows 编译 clang (C++)、binutils 和 GDB。按照ellcc.org 上的下载链接获取适用于多个 Linux 主机的预编译二进制文件。

回答by iljau

From: https://fedoraproject.org/wiki/MinGW/Tutorial

来自:https: //fedoraproject.org/wiki/MinGW/Tutorial

As of Fedora 17 it is possible to easily build (cross-compile) binaries for the win32 and win64 targets. This is realized using the mingw-w64 toolchain: http://mingw-w64.sf.net/. Using this toolchain allows you to build binaries for the following programming languages: C, C++, Objective-C, Objective-C++ and Fortran.

从 Fedora 17 开始,可以轻松地为 win32 和 win64 目标构建(交叉编译)二进制文件。这是使用 mingw-w64 工具链实现的:http://mingw-w64.sf.net/ 。使用此工具链,您可以为以下编程语言构建二进制文件:C、C++、Objective-C、Objective-C++ 和 Fortran。

"Tips and tricks for using the Windows cross-compiler": https://fedoraproject.org/wiki/MinGW/Tips

“使用 Windows 交叉编译器的提示和技巧”:https: //fedoraproject.org/wiki/MinGW/Tips

回答by scavenger

for Fedora it's

对于 Fedora,它是

sudo yum groupinstall -y "MinGW cross-compiler"