简体   繁体   English

编写跨平台程序

[英]Writing a cross-platform program

How could I write a program which runs on both Windows 7, Mac OS X (and maybe linux too)? 我怎么能写一个在Windows 7,Mac OS X(也可能是Linux)上运行的程序?

I heard Qt are a great framework to build cross-platform GUIs, but I think every program version need a recompile, isn't that right? 我听说Qt是构建跨平台GUI的一个很好的框架,但我认为每个程序版本都需要重新编译,不是吗? And should I compile the win version under windows, the mac version under mac os x, the linux version under linux and so on? 我应该编译windows下的win版本,mac os x下的mac版本,linux下的linux版本等等吗?

I'm getting ideas and/or suggestions 我有想法和/或建议

The underlying binary format is different on each platform, so unless you're using a virtual machine (like Java or Flash does) you will have to recompile your program on each platform. 基础二进制格式在每个平台上都是不同的,因此除非您使用虚拟机(如Java或Flash),否则您必须在每个平台上重新编译程序。

Some compilers (like GCC) allow cross-compiling, but it is not trivial to set up. 一些编译器(如GCC)允许交叉编译,但设置并非易事。 Probably the easiest system to cross-compile on is Linux (there are several open source projects that have cross compilation set up from Linux to Windows). 可能最简单的交叉编译系统是Linux(有几个开源项目有从Linux到Windows的交叉编译)。

In case of a GUI application, it depends on the language -- if you're stuck with C++, Qt or wxWindows might be a reasonable choice providing an abstraction layer over the native windowing system. 在GUI应用程序的情况下,它取决于语言 - 如果你坚持使用C ++,Qt或wxWindows可能是一个合理的选择,在本机窗口系统上提供一个抽象层。

If you can go with Java, it makes life simpler, however the windowing system is Java's and not native. 如果你可以使用Java,它会让生活更简单,但是窗口系统是Java而不是原生的。

Another language to think about is FreePascal w/ Lazarus -- it has a pretty good GUI designer that compiles to the native windowing system on every platform (WinAPI on Windows, Cocoa on OSX and GTK on Linux). 另一种需要考虑的语言是FreePascal w / Lazarus - 它有一个非常好的GUI设计器,可以编译到每个平台上的本机窗口系统(Windows上的WinAPI,OSX上的Cocoa和Linux上的GTK)。

Not sure if C++ is a must, but Adobe Air is a great cross platform development environment for desktop, and its growing for mobile development as well. 不确定C ++是否必须,但Adobe Air是桌面的一个伟大的跨平台开发环境,并且它也在不断发展为移动开发。 If you need an example of a major application using Adobe Air to deploy to multiple desktop OSes, just check out tweetdeck http://www.tweetdeck.com/ 如果您需要使用Adobe Air部署到多个桌面操作系统的主要应用程序示例,请查看tweetdeck http://www.tweetdeck.com/

I'd highly suggest also looking into Flex and Flash Builder if you go that route. 如果你走这条路,我强烈建议你也考虑使用Flex和Flash Builder。

There are two separate issues I would highlight when writing cross-platform programs -- how to make your code portable, and how to arrange for it to be built on the various different platforms. 在编写跨平台程序时,我会强调两个独立的问题 - 如何使代码可移植,以及如何安排在不同的平台上构建代码。

As far as the building side of things goes, I would look into a cross-platform build system like CMake ( http://www.cmake.org ). 就建设方面而言,我会研究一个像CMake( http://www.cmake.org )这样的跨平台构建系统。 You essentially write a script and CMake will generate the appropriate project file/makefile for a specific platform. 您基本上编写了一个脚本,CMake将为特定平台生成相应的项目文件/ makefile。 You then build your program on each platform as you would normally. 然后,您可以像平常一样在每个平台上构建程序。 For example, on Windows, you might use CMake to generate a Visual C++ project for you, and then use Visual C++ to actually build your executable. 例如,在Windows上,您可以使用CMake为您生成Visual C ++项目,然后使用Visual C ++实际构建您的可执行文件。 On Linux, you might use CMake to generate a makefile, and then build the executable using g++. 在Linux上,您可以使用CMake生成makefile,然后使用g ++构建可执行文件。

The other aspect is how to make your code portable -- the key is to write C++ standard-compliant code and make use of libraries that are themselves portable across the platforms you're interested in. You can (and may sometimes need to) write platform-specific code for each of the different platforms -- if you do, you should hide it behind a portable interface and have the rest of the code use that. 另一个方面是如何使您的代码可移植 - 关键是编写符合C ++标准的代码并使用本身可以在您感兴趣的平台上移植的库。您可以(有时可能需要)编写针对每个不同平台的特定于平台的代码 - 如果这样做,您应该将其隐藏在便携式接口后面并让其余代码使用它。

Yes, you need to compile for each version when using C++. 是的,您需要在使用C ++时为每个版本进行编译。

The only thing that prevents you from compiling a program, for example, for Windows on Mac is to get a tool for doing that. 唯一阻止你编译程序的东西,例如Mac上的Windows,就是获得一个工具。 It is possible, but the problem is finding the toolset. 这是可能的,但问题是找到工具集。

Also you can use a virtual machine for running diferent OSs and compiling code for all platforms on the same machine. 您还可以使用虚拟机运行不同的操作系统,并为同一台计算机上的所有平台编译代码。

Java在Windows,OS X和Linux上运行

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

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