简体   繁体   English

构建跨平台应用程序的最简单方法

[英]Easiest way to build a cross-platform application

I have read a few articles in the cross-platform tag.我已经阅读了跨平台标签中的几篇文章。 However, as I'm starting a fresh application (mostly a terminal/console app), I'm wondering about the easiest way to make it cross-platform (ie working for Linux, Mac OS X, and Windows).然而,当我开始一个新的应用程序(主要是终端/控制台应用程序)时,我想知道使它跨平台的最简单方法(即适用于 Linux、Mac OS X 和 Windows)。 I have thought about the following:我想过以下问题:

  • adding various macro/tags in my code to build different binary executables for each operating system在我的代码中添加各种宏/标签来为每个操作系统构建不同的二进制可执行文件
  • use Qt platform to develop a cross-functional app (although the GUI and platform component would add more development time as I'm not familiar with Qt)使用 Qt 平台开发一个跨功能的应用程序(虽然 GUI 和平台组件会增加更多的开发时间,因为我不熟悉 Qt)

Your thoughts?你的意见? Thanks in advance for your contribution!预先感谢您的贡献!

Edit: Sounds like there are a lot of popular responses on Java and Qt.编辑:听起来有很多关于 Java 和 Qt 的流行回应。 What are the tradeoffs between these two while we're at it?我们在这两者之间有什么权衡?

Do not go the first way.不要走第一种方式。 You'll encounter a lot of problems that are already solved for you by numerous tools.您会遇到很多问题,这些问题已经由众多工具为您解决了。

Qt is an excellent choice if you definitely want C++.如果你肯定想要 C++,Qt 是一个很好的选择。 In fact, it will speed up development even if you aren't familiar with it, as it has excellent documentation and is easy to use.事实上,即使您不熟悉它,它也会加快开发速度,因为它具有出色的文档并且易于使用。 The good part about it is that it isn't just a GUI framework, but also networking, XML, I/O and lots of other stuff you'll probably need.它的优点在于它不仅是一个 GUI 框架,而且还是网络、XML、I/O 和许多您可能需要的其他东西。

If not necessary C++, I'd go with Java.如果不需要 C++,我会选择 Java。 C++ is far too low level language for most applications.对于大多数应用程序来说,C++ 是太低级的语言。 Debugging memory management and corrupt stacks can be a nightmare.调试内存管理和损坏的堆栈可能是一场噩梦。

To your edited question:对于您编辑的问题:

  • The obvious one: Java has garbage collection, C++ doesn't.显而易见的一个:Java 有垃圾收集,C++ 没有。 It means no memory leaks in Java (unless you count possible bugs in JVM), no need to worry about dangling pointers and such.这意味着 Java 中没有内存泄漏(除非您计算 JVM 中可能的错误),无需担心悬空指针等。
  • Another obvious one: it is extremely easy to use platform-dependent code in C++ using #ifdefs.另一个明显的问题是:使用#ifdefs 在 C++ 中使用平台相关代码非常容易。 In Java it is a real pain.在 Java 中,这是一个真正的痛苦。 There is JNI but it isn't easy to use at all.有 JNI,但它根本不容易使用。
  • Java has very extensive support of exceptions. Java 对异常有非常广泛的支持。 While C++ has exceptions too, Qt doesn't use them, and some things that generate exceptions in Java will leave you with corrupt memory and crashes in C++ (think buffer overflows).虽然 C++ 也有异常,但 Qt 不使用它们,而且在 Java 中产生异常的一些东西会让你在 C++ 中内存损坏和崩溃(想想缓冲区溢出)。
  • "Write once, run everywhere." “写一次,到处跑。” Recompiling C++ program for many platforms can be daunting.为许多平台重新编译 C++ 程序可能令人生畏。 Java programs don't need to be recompiled. Java 程序不需要重新编译。
  • It is open to debate, but I think Java has more extensive and well-defined library.这是有争议的,但我认为 Java 有更广泛和定义明确的库。 The abstraction level is generally higher, the interfaces are cleaner.抽象级别通常更高,接口更干净。 And it supports more useful things, like XML schemas and such.它支持更多有用的东西,比如 XML 模式等等。 I can't think of a feature that is present in Qt, but absent in Java.我想不出 Qt 中存在但 Java 中不存在的功能。 Maybe multimedia or something, I'm not sure.也许是多媒体什么的,我不确定。
  • Both languages are very fast nowadays, so performance is usually not an issue, but Java can be a real memory hog.现在这两种语言都非常快,所以性能通常不是问题,但 Java 可能是一个真正的内存猪。 Not extremely important on modern hardware too, but still.在现代硬件上也不是非常重要,但仍然如此。
  • The least obvious one: C++ can be more portable than Java.最不明显的一点:C++ 可以比 Java 更具可移植性。 One example is FreeBSD OS which had very poor support for Java some time ago (don't know if it is still the case).一个例子是前段时间对Java的支持很差的FreeBSD OS(不知道现在是否仍然如此)。 C++/Qt works perfectly there. C++/Qt 在那里完美运行。 If you plan on supporting a wide range of Unix systems, C++ may be a better choice.如果您计划支持广泛的 Unix 系统,C++ 可能是更好的选择。

Use Java.使用 Java。 As much bashing as it gets/used to get, it's the best thing to get stuff working across any platform.与它得到/习惯得到的一样多的抨击,让东西在任何平台上工作都是最好的。 Sure, you will still need to handle external OS related functions you may be using, but it's much better than using anything else.当然,您仍然需要处理您可能正在使用的外部操作系统相关功能,但这比使用其他任何东西都要好得多。

Apart from Java, there are a few things you can run on the JVM - JRuby, Jython, Scala come to mind.除了 Java 之外,您还可以在 JVM 上运行一些东西 - JRuby、Jython、Scala。

You could also write with the scripting languages directly( Ruby, Python, etc ).您也可以直接使用脚本语言(Ruby、Python 等)进行编写。

C/C++ is best left for applications that demand complete memory control and high controllability. C/C++ 最适合需要完全内存控制和高度可控性的应用程序。

I'd go with the QT (or some other framework) option.我会选择 QT(或其他一些框架)选项。 If you went with the first you'd find it considerably harder.如果你选择第一个,你会发现它要困难得多。 After all, you have to know what to put into the various conditionally compiled sections for all the platforms you're targeting.毕竟,您必须知道在针对您所针对的所有平台的各种条件编译部分中放入什么内容。

I would also suggest Github's electron which allows to build cross platform desktop applications using NodeJs and the Google's Chromium.我还建议使用 Github 的电子,它允许使用 NodeJs 和谷歌的 Chromium 构建跨平台桌面应用程序。 The only drawback for this method is that an electron application run much slower than a native C++ application due to the abstraction layers between Javascript and native C++.这种方法的唯一缺点是,由于 Javascript 和本机 C++ 之间的抽象层,电子应用程序的运行速度比本机 C++ 应用程序慢得多。

I would suggest using a technology designed for cross-platform application development.我建议使用专为跨平台应用程序开发设计的技术。 Here are two technologies I know of that -- as long as you read the documentation and use the features properly -- you can build the application to run on all 3 platforms:以下是我所知道的两种技术——只要您阅读文档并正确使用这些功能——您就可以构建在所有 3 个平台上运行的应用程序:

Of course, there is always the web.当然,网络总是存在的。 I mostly use web applications not just for their portability, but also because they run on my Windows PC, my Ubuntu computer, and my Mac.我主要使用 Web 应用程序不仅是因为它们的可移植性,还因为它们可以在我的 Windows PC、我的 Ubuntu 计算机和我的 Mac 上运行。

We mainly build web applications because the web is the future.我们主要构建 Web 应用程序,因为 Web 是未来。 Local applications are viewed in my organization as mostly outdated, unless there is of course some feature or technology the web doesn't yet support that holds that application back from being fully web-based.在我的组织中,本地应用程序被视为大部分已过时,除非当然有某些功能或技术 Web 尚不支持,使该应用程序无法完全基于 Web。

If you're making a console app, you should be able to use the same source for all three platforms if you stick to the functions defined in the POSIX libraries.如果您正在制作控制台应用程序,并且坚持使用 POSIX 库中定义的函数,那么您应该能够对所有三个平台使用相同的源代码。 Setting up your build environment is the most complicated part, especially if you want to be able to build for multiple platforms out of the same source tree.设置构建环境是最复杂的部分,特别是如果您希望能够从同一个源代码树中为多个平台构建。

I'd say if you really want to use C++, QT is the easiest way for cross-platform application, I found myself using QT when I need an UI even though QT has a large set of library which makes pretty much everything easier in C++.我想说如果你真的想使用 C++,QT 是跨平台应用程序的最简单的方法,我发现自己在需要 UI 时使用 QT,即使 QT 有大量的库,这使得 C++ 中的一切变得更容易.

If you don't want to use QT then you need a good design and a lot of abstraction to make cross-platfform application.如果你不想使用 QT,那么你需要一个好的设计和大量的抽象来制作跨平台的应用程序。

However I'm using more and more Python bindinq to QT for medium size application.但是,我越来越多地将 Python bindinq 用于 QT 以用于中等规模的应用程序。 If you are working on a console application and you know a bit of python, you might find Python scripting much more comfortable than C++.如果您正在开发控制台应用程序并且了解一些 Python,您可能会发现 Python 脚本比 C++ 更舒适。 It keeps the time comsuming stuff away to be able to focus on your application.它可以节省时间,从而能够专注于您的应用程序。

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

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