简体   繁体   English

Way Cross编译可在Windows,Linux和Mac OS上运行的C / C ++代码?

[英]Way Cross Compile C/C++ code to run on Windows, Linux, and Mac OS?

Is there a way to take my C++ code and cross compile it to run on Windows, Mac OS, and Linux? 有没有办法获取我的C ++代码并交叉编译它以在Windows,Mac OS和Linux上运行? Is there a tool to do this, or does it have to be manually compiled on each OS via Terminal/Cygwin? 是否有工具可以执行此操作,还是必须通过Terminal / Cygwin在每个操作系统上手动编译?

The easiest way is to use a Mac OS X machine with VMs for Linux and Windows (using eg Parallels or VMware Fusion). 最简单的方法是将Mac OS X计算机与适用于Linux和Windows的VM配合使用(例如使用Parallels或VMware Fusion)。 You can put the source code in a directory which is shared with the VMs and then use the native tools for each OS to compile it. 您可以将源代码放在与VM共享的目录中,然后使用每个操作系统的本机工具进行编译。

You can cross compile with what is known as a cross-compiler. 您可以使用所谓的交叉编译器进行交叉编译。 Mingw can be installed as such. Mingw可以这样安装。 I believe MacOS just uses g++ so you can probably make a cross compiler for it as well. 我相信MacOS只使用g ++,所以你也可以为它做一个交叉编译器。 Never done it myself. 从来没有自己做过。 You can scour the web for directions. 您可以在网上搜索方向。 Here's the top link I got from google: 这是我从谷歌获得的顶级链接:

http://linux.bytesex.org/cross-compiler.html http://linux.bytesex.org/cross-compiler.html

You should go through your code and convert as much as you can to ANSI C++. 您应该检查代码并尽可能多地转换为ANSI C ++。 Factor remaining platform dependent code into separate modules. 将与平台相关的代码依赖于独立的模块。 These modules may need to be rewritten for other platforms, and let the build tool select the appropriate modules. 可能需要为其他平台重写这些模块,并让构建工具选择适当的模块。 The remaining code should compile with few problems on the other platforms. 剩下的代码应该在其他平台上编译时几乎没有问题。

Also, verify that your GUI, if you are using one, is cross-platform compatible. 此外,验证您的GUI(如果使用的是GUI)是跨平台兼容的。 Otherwise it will be a mess to convert to the other platforms. 否则转换到其他平台将是一团糟。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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