从 Linux 到 Windows 交叉编译 C++ 应用程序的手册?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/182408/
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
Manual for cross-compiling a C++ application from Linux to Windows?
提问by Pablo Herrero
Is there a manual for cross-compiling a C++ application from Linux to Windows?
是否有从 Linux 到 Windows 交叉编译 C++ 应用程序的手册?
Just that. I would like some information (links, reference, examples...) to guide me to do that.
只是。我想要一些信息(链接、参考、示例...)来指导我这样做。
I don't even know if it's possible.
我什至不知道这是否可能。
My objective is to compile a program in Linux and get a .exe file that I can run under Windows.
我的目标是在 Linux 中编译一个程序并获得一个可以在 Windows 下运行的 .exe 文件。
回答by richq
The basics are not too difficult:
基础不是太难:
sudo apt-get install mingw32
cat > main.c <<EOF
int main()
{
printf("Hello, World!");
}
EOF
i586-mingw32msvc-cc main.c -o hello.exe
Replace apt-get
with yum
, or whatever your Linux distro uses. That will generate a hello.exe
for Windows.
替换apt-get
为yum
,或者您的 Linux 发行版使用的任何内容。这将为hello.exe
Windows生成一个。
Once you get your head around that, you could use autotools, and set CC=i586-mingw32msvc-cc
一旦你解决了这个问题,你就可以使用autotools,并设置CC=i586-mingw32msvc-cc
CC=i586-mingw32msvc-cc ./configure && make
Or use CMake and a toolchain fileto manage the build. More difficult still is adding native cross libraries. Usually they are stored in /usr/cross/i586-mingw32msvc/{include,lib}
and you would need to add those paths in separately in the configure step of the build process.
或者使用CMake 和工具链文件来管理构建。更困难的是添加本机交叉库。通常它们存储在/usr/cross/i586-mingw32msvc/{include,lib}
,您需要在构建过程的配置步骤中单独添加这些路径。
回答by Anders Hansson
It depends on what you mean (I couldn't really say).
这取决于你的意思(我真的不能说)。
If you mean that you want to use an existing Linux application on Windows, then you could try compiling it using Cygwinon Windows. This however does not give you a Windows executable free from all dependencies towards Cygwin (your executable still depends on the
cygwin.dll
file) - and it still may need some porting before it will work. See http://www.cygwin.com.If you mean that you want to be able to perform the actual compilation of a Windows application on Linux and produce a .exe file that is executable on Windows - thus using your Linux box for development and/or compilation then you should look into MinGWfor Linux which is a tool for crosscompiling for Windows on Linux. See http://www.mingw.org/wiki/LinuxCrossMinGW.
如果您的意思是要在 Windows 上使用现有的 Linux 应用程序,那么您可以尝试在 Windows 上使用Cygwin编译它。然而,这并没有为您提供一个没有对 Cygwin 的所有依赖关系的 Windows 可执行文件(您的可执行文件仍然依赖于该
cygwin.dll
文件) - 它仍然可能需要一些移植才能工作。请参阅http://www.cygwin.com。如果你的意思是你要能够执行Linux上的Windows应用程序的实际编译并生成.exe文件是可执行文件在Windows上-从而用于开发和/或编译你的Linux机器,那么你应该考虑的MinGW的Linux 这是一个用于在 Linux 上交叉编译 Windows 的工具。请参阅http://www.mingw.org/wiki/LinuxCrossMinGW。
Best regards!
此致!
回答by 1.01pm
I suggest you give the following, GUB(Grand Unified Builder) a try as it cross-compiles several packages with their dependencies and assembles them into a single installation package for currently 11 architectures. You can download a prebuilt iso for installation in a VM from hereand follow the source here. It can currently be used to cross-compile GNU LilyPond/ GNU Denemo / Inkscape and OpenOffice.org.
我建议您尝试以下GUB(Grand Unified Builder),因为它交叉编译了几个包及其依赖项,并将它们组装成当前 11 种架构的单个安装包。您可以从此处下载预构建的 iso 以安装在 VM 中,并按照此处的源进行操作。它目前可用于交叉编译 GNU LilyPond/GNU Denemo/Inkscape 和 OpenOffice.org。
The target architectures are:
目标架构是:
- darwin-ppc - tar.bz2 file for Darwin 7 (MacOS 10.3)/PowerPC
- darwin-x86 - tar.bz2 file for Darwin 8 (MacOS 10.4)/x86
- mingw - mingw executable for Windows32
- linux-x86 - shar archive for Linux/x86
- linux-64 - shar archive for Linux/x86_64
- linux-ppc - shar archive for Linux/PowerPC
- freebsd-x86 - shar archive for FreeBSD 4/x86
- freebsd-64 - shar archive for FreeBSD 6/x86_64
- cygwin - .tar.bz2 packages for Cygwin/Windows32
- arm - shar archive for Linux/ARM (largely untested)
- debian - shar archive for Debian (largely untested)
- darwin-ppc - 用于 Darwin 7 (MacOS 10.3)/PowerPC 的 tar.bz2 文件
- darwin-x86 - Darwin 8 (MacOS 10.4)/x86 的 tar.bz2 文件
- mingw - Windows32 的 mingw 可执行文件
- linux-x86 - Linux/x86 的 shar 存档
- linux-64 - 用于 Linux/x86_64 的 shar 存档
- linux-ppc - 用于 Linux/PowerPC 的 shar 存档
- freebsd-x86 - FreeBSD 4/x86 的 shar 存档
- freebsd-64 - FreeBSD 6/x86_64 的 shar 存档
- cygwin - Cygwin/Windows32 的 .tar.bz2 包
- arm - Linux/ARM 的 shar 存档(基本上未经测试)
- debian - Debian 的 shar 存档(大部分未经测试)