从 Windows 到 Linux 的 C++ 交叉编译器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4769968/
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
C++ cross-compiler from Windows to Linux
提问by neo_x3m
Are there any user friendly tools out there to cross-compile from Windows to Linux?
是否有任何用户友好的工具可以从 Windows 交叉编译到 Linux?
回答by Austin Lamb
Are you asking for something that can take code written to run on Windows and make it run on linux? There's not really a compiler that does that - but maybe you're looking for Wine ( http://www.winehq.org/) which lets you run Windows apps on linux (if they use APIs Wine has thunked out).
您是否要求可以将编写的代码在 Windows 上运行并使其在 linux 上运行的东西?没有真正做到这一点的编译器 - 但也许您正在寻找 Wine(http://www.winehq.org/),它可以让您在 linux 上运行 Windows 应用程序(如果它们使用 Wine 已经解决的 API)。
Edit:I'm assuming your code is using Windows APIs (Win32, etc.) in this answer. If you're only using Standard C++ and libraries available cross-platform, then there's lots of other answers.
编辑:我假设您的代码在此答案中使用了 Windows API(Win32 等)。如果您只使用标准 C++ 和跨平台可用的库,那么还有很多其他答案。
回答by Darryl
I don't believe there are any compilers that allow you to compile on Windows and then take the resulting binary and run it on a Linux machine. Your best bet is to set up two development environments, one on Windows and one on Linux. Do most of your development and debugging in the Windows environment if that's what suits you, then frequently compile and test under Linux to be sure your code is truly cross-platform. You'll also want to use a cross-platform build system and testing framework. I recommend using cmakeand Google Test, respectively.
我不相信有任何编译器可以让您在 Windows 上编译,然后获取生成的二进制文件并在 Linux 机器上运行它。最好的办法是设置两个开发环境,一个在 Windows 上,一个在 Linux 上。如果适合您,请在 Windows 环境中进行大部分开发和调试,然后经常在 Linux 下编译和测试以确保您的代码真正跨平台。您还需要使用跨平台构建系统和测试框架。我建议分别使用cmake和Google Test。
回答by Ben Voigt
crosstool-ng supports building cross-compilers with cygwin host.
crosstool-ng 支持使用 cygwin host 构建交叉编译器。
Overview:
概述:
- Install cygwin, with development packages
- Download
crosstool-ng
- Extract the tarball (
tar xvjf crosstool-ng*
) - Run
./configure
- When configure complains about a missing tool, go back to cygwin setup and install the corresponding package (e.g.
flex
,libtool
,ncurses-devel
) You'll need at least the following:- Devel/gperf
- Devel/bison
- Devel/byacc
- Devel/wget
- Devel/patch
- Devel/make (GNU version)
- Devel/automake 1.10+
- Libs/libncursesw10
- Libs/libncursesw10-devel
make
make install
- Create a new directory for building the cross-compiler, e.g.
/usr/src/cross-linux-gnu-root
- Enable system-wide case sensitivity support in the registry (see https://superuser.com/questions/266110/how-do-you-make-windows-7-fully-case-sensitive-with-respect-to-the-filesystem#answer-842670)
- Mount the directory case-sensitive (e.g.
mount c:/cygwin/usr/src/cross-linux-gnu-root /usr/src/cross-linux-gnu -o binary,posix=1
) - From inside the cross-compiler build directory,
ct-ng menuconfig
- In the menus, set target architecture to
x86
(probably) and subarchitecture toi686
(avoids GCC 4.8 issues, thanks osm0sis), target kernel tolinux
, and target C library toglibc
, and enable the C++ compiler. - To work around make 4.0 issues, also enable EXPERIMENTAL in
Paths and misc options
then, go inCompanion tools
(at top-level) and enableBuild some companion tools
and thenmake 3.81
(Thanks osm0sis) - wget has issues with the latest kernel.org certificates so use the .wgetrc method in this accepted answer: How do I fix certificate errors when running wget on an HTTPS URL in Cygwin?(Thanks osm0sis)
- Currently 3 file patches are required to avoid further errors:
- the _libintl_gettext erroras mentioned in the comments
- the byteshift headers, and
- the ELF headers(Thanks Duncan Calvert & osm0sis)
ct-ng build
- 安装cygwin,带开发包
- 下载
crosstool-ng
- 提取压缩包 (
tar xvjf crosstool-ng*
) - 跑
./configure
- 当 configure 抱怨缺少工具时,返回 cygwin setup 并安装相应的包(例如
flex
,libtool
,ncurses-devel
)您至少需要以下内容:- 开发/gperf
- 开发/野牛
- 开发/byacc
- 开发/wget
- 开发/补丁
- 开发/制作(GNU 版)
- 开发/汽车制造 1.10+
- 库/libncursesw10
- 库/libncursesw10-devel
make
make install
- 创建一个用于构建交叉编译器的新目录,例如
/usr/src/cross-linux-gnu-root
- 在注册表中启用系统范围的区分大小写支持(请参阅https://superuser.com/questions/266110/how-do-you-make-windows-7-fully-case-sensitive-with-respect-to-the-文件系统#answer-842670)
- 挂载目录区分大小写(例如
mount c:/cygwin/usr/src/cross-linux-gnu-root /usr/src/cross-linux-gnu -o binary,posix=1
) - 从交叉编译器构建目录中,
ct-ng menuconfig
- 在菜单中,将目标体系结构设置为
x86
(可能)并将子体系结构设置为i686
(避免GCC 4.8 问题,感谢 osm0sis),目标内核为linux
,目标 C 库为glibc
,并启用 C++ 编译器。 - 要解决make 4.0 问题,还要在
Paths and misc options
then 中启用 EXPERIMENTAL ,进入Companion tools
(在顶级)并启用Build some companion tools
然后make 3.81
(感谢 osm0sis) - wget 对最新的 kernel.org 证书有问题,因此请在此接受的答案中使用 .wgetrc 方法:How do I fix certificate errors when running wget on an HTTPS URL in Cygwin?(感谢 osm0sis)
- 目前需要 3 个文件补丁来避免进一步的错误:
- 评论中提到的 _libintl_gettext 错误
- 字节移位标头,和
- ELF 标头(感谢 Duncan Calvert 和 osm0sis)
ct-ng build
Of course, this is NOT going to enable you to build linux applications from inside Visual Studio. (VS2010 and later let you build with other toolchains such as gcc, but you'd need an appropriate toolchain description in addition to the cross-compiler built by crosstool-ng). But you'll have a working g++-linux-gnu
, which you can either run directly or using a Makefile.
当然,这不会使您能够从 Visual Studio 内部构建 linux 应用程序。(VS2010 及更高版本允许您使用其他工具链(例如 gcc)进行构建,但是除了由 crosstool-ng 构建的交叉编译器之外,您还需要适当的工具链描述)。但是您将有一个g++-linux-gnu
可以运行的 . 文件,您可以直接运行它,也可以使用 Makefile 运行它。
NOTE: Building the cross-compiler takes a LONG time. osm0sis has provided a prebuilt gcc 4.8.1 here, along with his notes on building the cross-compiler (used to update this answer).
注意:构建交叉编译器需要很长时间。osm0sis在这里提供了一个预构建的 gcc 4.8.1,以及他关于构建交叉编译器的注释(用于更新这个答案)。
回答by supercheetah
Your best bet is to use a cross platform IDE like Code::Blocksthat can import MSVC projects, and generate a Makefile for Linux, which you can then run on a Linux (with even the same program if you so wish).
最好的办法是使用像Code::Blocks这样的跨平台 IDE ,它可以导入 MSVC 项目,并为 Linux 生成一个 Makefile,然后你可以在 Linux 上运行它(如果你愿意,甚至可以使用相同的程序)。
So your work flow would then look something like the following:
因此,您的工作流程将如下所示:
Code, Compile, and debug on MSVC -> import into Code::Blocks & generate Makefile
-> test compile and debug with GCC
-> copy to Linux with Code::Blocks -> test compile & debug on Linux
It's a little obtuse, probably, but at least Code::Blocks takes care of a lot of those small details with things like Makefiles, and what not.
这可能有点迟钝,但至少 Code::Blocks 用 Makefile 之类的东西处理了很多这些小细节,还有什么不是。
回答by Angel T
Compile to Linux from Windows without to use virtualization or cross compiler but only natively via CoLinux. Create native Linux executable files without leave (reboot) windows. C/C++ Compiling for Linux under Windows through Cooperative Linux. The fastest switching between Windows and Linux through Alt+Tab(no restarting or rebooting needed):
从 Windows 编译到 Linux,无需使用虚拟化或交叉编译器,而只能通过 CoLinux 本地编译。无需离开(重新启动)窗口即可创建本机 Linux 可执行文件。通过 Cooperative Linux 为 Windows 下的 Linux 编译 C/C++。通过Alt+在 Windows 和 Linux 之间的最快切换Tab(无需重新启动或重新引导):
- Install CoLinux
- Use CoLinux root file system image: Ubuntu-9.04-1gb.7z
- Start CoLinux
- Change string
archive.ubuntu.com
toold-releases.ubuntu.com
with:
- 安装CoLinux
- 使用CoLinux根文件系统映像:Ubuntu-9.04-1gb.7z
- 启动 CoLinux
- 将字符串更改
archive.ubuntu.com
为old-releases.ubuntu.com
:
$ sudo vi /etc/apt/sources.list
- Update your configuration:
- 更新您的配置:
$ sudo apt-get update
- Install two software packages to be able to compile your programs (c/c++, gcc compilers, mysql dev 5.1):
- 安装两个软件包以便能够编译您的程序(c/c++、gcc 编译器、mysql dev 5.1):
$ sudo apt-get install build-essential
$ sudo apt-get install libmysqlclient15-dev
- Accessing Windows directories from CoLinux:
- 从 CoLinux 访问 Windows 目录:
$ mount cofs0 /mnt/ -t cofs
$ cd /mnt
- Compile
- 编译
$ c++ your.source.cc -o executable.name