C++ 的最佳实践 便携式开源应用程序

发布于 2024-07-16 00:51:14 字数 1436 浏览 6 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

我们只是彼此的过ke 2024-07-23 00:51:14

您的项目要赶上的最重要的事情是可移植性。 它应该很容易构建和使用。 为大家奔跑。

GCC (g++) 确实是首选的编译器。 它来自开源世界,因此被最广泛采用。

然而,简单的 Makefile 并不能解决问题。 使用 CodeBlocks 或任何其他 IDE 生成它有一个问题:由于他们的平台,其他开发人员可能必须生成自己的,但手头不一定有 CodeBlocks,或者只是不想使用它。

存在多种不同的跨平台构建系统,它们与 IDE 无关。 他们中的一些人创建 Makefile,另一些人不使用 make,而是自己构建。

  • 最广泛采用的构建系统是 Autotools。 然而,它很难学,很混乱,而且总体上很痛苦。
  • 在众多其他选择中,我推荐Waf。 它已经被几个较大的开源项目所证明,XMMS2 就是一个很好的例子(虽然不是一个非常受欢迎的项目,但它有一个包含大量插件的大型构建,并且构建在包括 OS X 和 Windows 在内的许多平台上)。 虽然 WAF 并未得到广泛采用,但它与源代码一起提供并且易于设置。 我给你的推荐。

编辑:要开始您的开源项目,我还推荐 Karl Fogel 的这本书 (可在线阅读)。 玩得开心!

The most important thing for your project to catch up is portability. It should be easy to build & run for everybody.

GCC (g++) is indeed the compiler of choice. It comes from the opensource world and is therefore most widely adopted by it.

However, a simple Makefile won't cut it. Generating it using CodeBlocks or any other IDE has a problem: Due to their platform, other developers will probably have to generate their own, but won't necessarily have CodeBlocks at hand, or just don't want to use it.

There exist several different cross-platform build systems, which are IDE-agnostic. Some of them create Makefiles, others don't use make but build on their own.

  • The most widely adopted build system is Autotools. However, it is hard to learn, cluttered, and an overall pain in the ass.
  • Out of many other choices, I recommend Waf. It is proven by several larger open source projects already, XMMS2 being a good example (while not a very popular project, it has a large build with a lot of plugins and builds on a lot of platforms including OS X and Windows). While waf is not very broadly adopted, it is meant to be shipped with the source and easy to set-up. My recommendation for you.

Edit: to get started with your Open Source project, I also recommending this book by Karl Fogel (available for reading online). Have fun!

三生一梦 2024-07-23 00:51:14

GNU C++ 编译器对于跨平台工作来说是一个相对较好的选择,只不过在 Windows 上仅原生支持相对较旧的版本 (3.4)。 将 4.x 系列移植到 Windows 的工作正在进行中,但到目前为止还没有准备好。

我不会关注使用什么编译器,而是关注使用哪种语言。 编写 ANSI 标准 C++ 将大大有助于使您的代码跨平台。 尽可能将特定于平台的行为隐藏在良好的工具包(例如 Qt)后面。

对于跨平台构建环境,这可能取决于您使用的工具包。 Qt有QMake,比较好。 CMake 是另一个引人注目的选择。 我会避免使用 Autotools,因为它在 UNIX 之外的可移植性非常差——在 Win32 上使用 Autotools 通常是对那些该死的人的折磨。

最后,现在就开始在多个平台上工作。VMware 对于此类事情的价值是无价的。 让您的代码在 Linux、FreeBSD 和 Windows 上进行编译。 如果您能够实现这三个目标,那么将来迁移到其他平台将变得更加容易。

The GNU C++ compiler is a relatively good choice for crossplatform work, except that on Windows only a relatively old version (3.4) is natively supported. Work is underway to port a 4.x-series to Windows, but so far it's not ready for prime time.

Rather than focus on what compiler to use, I'd instead focus on which language to use. Writing ANSI standard C++ will go a long way towards making your code crossplatform. As far as possible, hide platform-specific behavior behind a good toolkit, such as Qt.

For cross-platform build environments, this may depend on what toolkit you use. Qt has QMake, which is relatively good. CMake is another compelling choice. I would avoid Autotools, since it has very poor portability outside of UNIX -- using Autotools on Win32 is very often the torment of the damned.

Finally, start working on multiple platforms now. VMware is invaluable for something like this. Get your code to compile on Linux, on FreeBSD and on Windows. If you can hit those three targets, moving to other platforms will be enormously easier in the future.

孤君无依 2024-07-23 00:51:14
  • 现在就开始在多个平台上构建:它会尽早暴露问题。
  • 如果您只关心 Linux 和 Mac OS X,那么 g++ 应该就可以了。 有各种可用的 IDE 和构建系统。 您可能需要考虑使用 autotools 来提供配置支持。
  • 如果你想针对Windows,事情会变得有点复杂。 您可以使用 cygwin 和/或 mingw,但这有时可能很复杂。 各种项目使用诸如 CMake 之类的跨平台构建和配置系统来处理许多问题。
  • Start building on multiple platforms now: it exposes issues early on.
  • If you only care about Linux and Mac OS X, then g++ should be just fine. There are various IDEs and build systems available. You might want to consider using autotools to help out with configuration support.
  • If you want to target Windows, it gets a bit more complicated. You can use cygwin and/or mingw, but that can be complicated sometimes. Various projects use cross-platform build and configuration systems like CMake to take care of many issues for them.
悲凉≈ 2024-07-23 00:51:14

根据具体平台的不同,Qt 可能会提供答案。 尤其是有了新的许可证。

Depending on the specific platforms, Qt may hold the answers. Especially with the new license.

若沐 2024-07-23 00:51:14

编译器:我打算使用g++,听说它是跨平台的。 这是一个好的选择吗?

是的,但是哪个版本? 您是否有要使用的版本的已知问题列表?

Make 文件:我见过 Code::Blocks 编辑器,它会动态生成 make 文件,您不必手动编写文件。 这是最佳实践还是我需要创建 make 文件?

经典的选择。 当项目增长时,您可能需要调整它们。

开发跨平台应用程序时需要注意的其他设置是什么?

g++/makefile 端口可用于所有主要平台。 因此,一旦您确定没有使用任何特定于平台的 API 或构造,您就可以开始了。

那么下面的呢?

  • 第三方库? 确保您不会迷恋特定平台。
  • 许可问题(您的代码和其他)
  • SCM

Compiler : I plan to use g++ and heard that it is cross platform. Is that a good choice?

Yes, but which version? Do you have a list of known issues for the version you are going to use?

Make files : I have seen Code::Blocks editor and it generates make files on fly and you don't have to write one manually. Is this the best practice or do I need to create make files?

A classical choice. You probably will need to tweak them when the project grows.

What are the other settings to be taken care when developing cross-platform applications?

A g++/makefile port is available for ever major platform. So, once you have made sure you are not using any platform specific API or construct you should be good to go.

What about the following?

  • 3rd party libraries? Make sure you don't fall for a platform specific one.
  • Licensing issues (your code and others)
  • SCM
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文