学习和跨平台开发(C ++)

时间:2022-06-16 02:59:49

I am writing a small C++ program for fun and for extending my C++ skill. Since its scope is relatively small, I also planning to try out cross-platform development by making this program support both Windows and Linux.

我正在编写一个小型C ++程序,用于娱乐和扩展我的C ++技能。由于其范围相对较小,我还计划通过使该程序支持Windows和Linux来尝试跨平台开发。

I reckon my C++ proficiency is sitting somewhere between casual and intermediate level: OO, a bit of templates and design patterns, used STL before and trying to look into it more in details, ... However, while coding this little program, I find that the deeper I dig into C++, the more pain I feel, especially when I come to understanding and dealing with differences between different platform's/vendor's implementation.

我认为我的C ++熟练程度介于休闲和中级之间:OO,一些模板和设计模式,之前使用STL并试图更详细地研究它......但是,在编写这个小程序时,我发现我越深入挖掘C ++,我就越感到痛苦,特别是当我开始理解和处理不同平台/供应商实现之间的差异时。

The use of cross-platform frameworks like Qt, ACE, Boost seems help to speed up development a lot thus make life easier, but I worry if this will beat my purpose. Can somebody give some advice if there is any "best practice" for doing C++ cross-platform development? Thanks.

使用像Qt,ACE,Boost这样的跨平台框架似乎有助于加快开发速度,从而使生活更轻松,但我担心这是否会超出我的目的。如果有任何进行C ++跨平台开发的“最佳实践”,有人可以给出一些建议吗?谢谢。

10 个解决方案

#1


16  

Can somebody give some advice if there is any "best practice" for doing C++ cross-platform development?

如果有任何进行C ++跨平台开发的“最佳实践”,有人可以给出一些建议吗?

There are three things:

有三件事:

  1. Write your own code so that it's portable

    编写自己的代码,使其可移植

  2. Wrap platform-specific APIs behind an abstraction/insulation/utility layer

    在抽象/绝缘/实用程序层后面包装特定于平台的API

  3. Choose cross-platform libraries

    选择跨平台库

You can choose option #2 and/or #3.

您可以选择选项#2和/或#3。

Advantages of #3 over #2 tend to be things like, "It's already written, debugged, and supported"; and the disadvantages are like, "I have to learn it, I might have to pay for it, I can't necessarily support it myself, and it may not do exactly what I want."

#3优于#2的优点往往是“它已经被编写,调试和支持”;而且缺点是,“我必须学习它,我可能要付出代价,我不一定能自己支持它,它可能不会完全符合我的要求。”

Developers will often prefer option #3 instead of #2, especially if it's free open source (which all three of the libraries that you cited are).

开发人员通常更喜欢选项#3而不是#2,特别是如果它是免费的开源(你引用的所有三个库都是)。

#2


13  

http://blog.backblaze.com/2008/12/15/10-rules-for-how-to-write-cross-platform-code/

Should provide more detail to the answers already given.

应该为已经给出的答案提供更多细节。

Also I suggest using existing libraries that abstract endianness, data type sizes and differences. The following should be considered before starting your cross-platform project.

另外,我建议使用现有的库来抽象字节序,数据类型大小和差异。在开始跨平台项目之前,应考虑以下事项。

GUI

  1. Qt
  2. XVT
  3. wxWidgets

General Libraries/frameworks

  1. STL (Incorporated in most platform libraries already)
  2. STL(已纳入大多数平台库)

  3. Boost

Game Development

  1. SDL
  2. Cocos2d-x

#3


3  

Use gcc. It's available on both Windows and Linux and the libraries and language syntax is identical on both platforms.

使用gcc。它可以在Windows和Linux上使用,并且两个平台上的库和语言语法都是相同的。

For cross platform GUI applications, Qt is a good idea. There is no getting away from having a dependency on a GUI framework if you are trying to achieve platform independence.

对于跨平台GUI应用程序,Qt是个好主意。如果您正在尝试实现平*立性,那么就无法摆脱对GUI框架的依赖。

#4


2  

Unless you are doing GUI stuff cross platform isn't a big problem.

除非你在做GUI,否则跨平台不是一个大问题。

There are some small issues to do with filesystems ( different / \ separators, allowed characters in filenames etc) but these are at the application level rather than the c++.

文件系统有一些小问题(不同的\ \分隔符,文件名中允许的字符等)但这些问题在应用程序级别而不是c ++。

Doing major applications gets more complex, you need to handle help, file locations an possibly security and user info in a cross platform way. For simple algorithm type programming there isn't a problem.

使主要应用程序变得更加复杂,您需要以跨平台的方式处理帮助,文件位置以及可能的安全性和用户信息。对于简单的算法类型编程,没有问题。

Qt is mainly a GUI library, although it has extra cross platform filesystems stuff. STL, Boost, ACE are cross platform but that isn't there main point.

Qt主要是一个GUI库,虽然它有额外的跨平台文件系统的东西。 STL,Boost,ACE是跨平台的,但这不是主要观点。

#5


2  

Use them! Seriously. The only reason you may not want to use them is if you plan on working in an environment where they're not available. But, given their cross-platform nature, that's not likely.

使用它们!认真。您可能不想使用它们的唯一原因是您计划在无法使用的环境中工作。但是,鉴于它们的跨平台性质,这是不可能的。

You will find that the benefit you get from using them is immense, even if they weren't cross-platform. The "best practice" you speak of is to be able to deliver your "product" as quicly and easily as possible.

你会发现使用它们带来的好处是巨大的,即使它们不是跨平台的。您所说的“最佳实践”是尽可能快速,轻松地提供您的“产品”。

I once answered a question from someone who stated he didn't want to use GUI libraries at a level above Xlib. If he'd actually ever used Xlib, he'd know the pain we'd all felt when forced to code at such a low level of abstraction. This makes about as much sense as wanting to code in assembler because C/C++?Python/Perl/everthting-else is simply a higher-level abstarction.

我曾经回答过一个问题,他说他不想在Xlib以上的级别使用GUI库。如果他真的曾经使用过Xlib,他就会知道当*在如此低的抽象级别编码时我们都会感受到的痛苦。这与想要在汇编程序中编码一样有意义,因为C / C ++?Python / Perl / everthting-else只是一个更高级别的abstarction。

#6


2  

this answers are really good and you can make a list for find where is the beginning. but i think you should read some articles about "porting application".not relevant with cross-platform development but this can give you very large perspective about cross platform development. In cross-platform developing, one of more importing thing is memory issues like "endian" (byte order- byte order can show differences for tehnologies or platforms)

这个答案真的很好,你可以列出一个列表,找到起点。但我认为你应该阅读一些关于“移植应用程序”的文章。与跨平台开发无关,但这可以为你提供跨平台开发的非常大的视角。在跨平台开发中,一个更重要的导入是内存问题,如“endian”(字节顺序 - 字节顺序可以显示tehnologies或平台的差异)

#7


2  

use boost. they take care of cross-platform stuff for you.

使用提升。他们会为您处理跨平台的事情。

boost::filesystem is a great example

boost :: filesystem就是一个很好的例子

#8


1  

i think you can learn a lot from using ACE or equivalent libraries. they will boost your understanding of c++ and design patterns. i think this is the best thing you can do to improve your coding skills.

我想你可以从使用ACE或同等的库中学到很多东西。它们将提高您对c ++和设计模式的理解。我认为这是提高编码技能的最佳方法。

#9


1  

If your are really interested in making your code as cross-platform as possible, use as many compilers as possible. If you are using Windows and Linux, use VC and gcc, at the minimum. This will ensure that you don't use complier specific features, and that you don't rely on system specific behavior. Use more compilers (Intel, IBM, etc) and OSs (OS X, Solaris) if you have access to them.

如果您真的对使代码尽可能跨平台感兴趣,请使用尽可能多的编译器。如果您使用的是Windows和Linux,请至少使用VC和gcc。这将确保您不使用编译器特定功能,并且您不依赖于系统特定的行为。如果您有权访问它们,请使用更多编译器(英特尔,IBM等)和操作系统(OS X,Solaris)。

#10


0  

You can try using U++ > http://www.ultimatepp.org/index.html

您可以尝试使用U ++> http://www.ultimatepp.org/index.html

#1


16  

Can somebody give some advice if there is any "best practice" for doing C++ cross-platform development?

如果有任何进行C ++跨平台开发的“最佳实践”,有人可以给出一些建议吗?

There are three things:

有三件事:

  1. Write your own code so that it's portable

    编写自己的代码,使其可移植

  2. Wrap platform-specific APIs behind an abstraction/insulation/utility layer

    在抽象/绝缘/实用程序层后面包装特定于平台的API

  3. Choose cross-platform libraries

    选择跨平台库

You can choose option #2 and/or #3.

您可以选择选项#2和/或#3。

Advantages of #3 over #2 tend to be things like, "It's already written, debugged, and supported"; and the disadvantages are like, "I have to learn it, I might have to pay for it, I can't necessarily support it myself, and it may not do exactly what I want."

#3优于#2的优点往往是“它已经被编写,调试和支持”;而且缺点是,“我必须学习它,我可能要付出代价,我不一定能自己支持它,它可能不会完全符合我的要求。”

Developers will often prefer option #3 instead of #2, especially if it's free open source (which all three of the libraries that you cited are).

开发人员通常更喜欢选项#3而不是#2,特别是如果它是免费的开源(你引用的所有三个库都是)。

#2


13  

http://blog.backblaze.com/2008/12/15/10-rules-for-how-to-write-cross-platform-code/

Should provide more detail to the answers already given.

应该为已经给出的答案提供更多细节。

Also I suggest using existing libraries that abstract endianness, data type sizes and differences. The following should be considered before starting your cross-platform project.

另外,我建议使用现有的库来抽象字节序,数据类型大小和差异。在开始跨平台项目之前,应考虑以下事项。

GUI

  1. Qt
  2. XVT
  3. wxWidgets

General Libraries/frameworks

  1. STL (Incorporated in most platform libraries already)
  2. STL(已纳入大多数平台库)

  3. Boost

Game Development

  1. SDL
  2. Cocos2d-x

#3


3  

Use gcc. It's available on both Windows and Linux and the libraries and language syntax is identical on both platforms.

使用gcc。它可以在Windows和Linux上使用,并且两个平台上的库和语言语法都是相同的。

For cross platform GUI applications, Qt is a good idea. There is no getting away from having a dependency on a GUI framework if you are trying to achieve platform independence.

对于跨平台GUI应用程序,Qt是个好主意。如果您正在尝试实现平*立性,那么就无法摆脱对GUI框架的依赖。

#4


2  

Unless you are doing GUI stuff cross platform isn't a big problem.

除非你在做GUI,否则跨平台不是一个大问题。

There are some small issues to do with filesystems ( different / \ separators, allowed characters in filenames etc) but these are at the application level rather than the c++.

文件系统有一些小问题(不同的\ \分隔符,文件名中允许的字符等)但这些问题在应用程序级别而不是c ++。

Doing major applications gets more complex, you need to handle help, file locations an possibly security and user info in a cross platform way. For simple algorithm type programming there isn't a problem.

使主要应用程序变得更加复杂,您需要以跨平台的方式处理帮助,文件位置以及可能的安全性和用户信息。对于简单的算法类型编程,没有问题。

Qt is mainly a GUI library, although it has extra cross platform filesystems stuff. STL, Boost, ACE are cross platform but that isn't there main point.

Qt主要是一个GUI库,虽然它有额外的跨平台文件系统的东西。 STL,Boost,ACE是跨平台的,但这不是主要观点。

#5


2  

Use them! Seriously. The only reason you may not want to use them is if you plan on working in an environment where they're not available. But, given their cross-platform nature, that's not likely.

使用它们!认真。您可能不想使用它们的唯一原因是您计划在无法使用的环境中工作。但是,鉴于它们的跨平台性质,这是不可能的。

You will find that the benefit you get from using them is immense, even if they weren't cross-platform. The "best practice" you speak of is to be able to deliver your "product" as quicly and easily as possible.

你会发现使用它们带来的好处是巨大的,即使它们不是跨平台的。您所说的“最佳实践”是尽可能快速,轻松地提供您的“产品”。

I once answered a question from someone who stated he didn't want to use GUI libraries at a level above Xlib. If he'd actually ever used Xlib, he'd know the pain we'd all felt when forced to code at such a low level of abstraction. This makes about as much sense as wanting to code in assembler because C/C++?Python/Perl/everthting-else is simply a higher-level abstarction.

我曾经回答过一个问题,他说他不想在Xlib以上的级别使用GUI库。如果他真的曾经使用过Xlib,他就会知道当*在如此低的抽象级别编码时我们都会感受到的痛苦。这与想要在汇编程序中编码一样有意义,因为C / C ++?Python / Perl / everthting-else只是一个更高级别的abstarction。

#6


2  

this answers are really good and you can make a list for find where is the beginning. but i think you should read some articles about "porting application".not relevant with cross-platform development but this can give you very large perspective about cross platform development. In cross-platform developing, one of more importing thing is memory issues like "endian" (byte order- byte order can show differences for tehnologies or platforms)

这个答案真的很好,你可以列出一个列表,找到起点。但我认为你应该阅读一些关于“移植应用程序”的文章。与跨平台开发无关,但这可以为你提供跨平台开发的非常大的视角。在跨平台开发中,一个更重要的导入是内存问题,如“endian”(字节顺序 - 字节顺序可以显示tehnologies或平台的差异)

#7


2  

use boost. they take care of cross-platform stuff for you.

使用提升。他们会为您处理跨平台的事情。

boost::filesystem is a great example

boost :: filesystem就是一个很好的例子

#8


1  

i think you can learn a lot from using ACE or equivalent libraries. they will boost your understanding of c++ and design patterns. i think this is the best thing you can do to improve your coding skills.

我想你可以从使用ACE或同等的库中学到很多东西。它们将提高您对c ++和设计模式的理解。我认为这是提高编码技能的最佳方法。

#9


1  

If your are really interested in making your code as cross-platform as possible, use as many compilers as possible. If you are using Windows and Linux, use VC and gcc, at the minimum. This will ensure that you don't use complier specific features, and that you don't rely on system specific behavior. Use more compilers (Intel, IBM, etc) and OSs (OS X, Solaris) if you have access to them.

如果您真的对使代码尽可能跨平台感兴趣,请使用尽可能多的编译器。如果您使用的是Windows和Linux,请至少使用VC和gcc。这将确保您不使用编译器特定功能,并且您不依赖于系统特定的行为。如果您有权访问它们,请使用更多编译器(英特尔,IBM等)和操作系统(OS X,Solaris)。

#10


0  

You can try using U++ > http://www.ultimatepp.org/index.html

您可以尝试使用U ++> http://www.ultimatepp.org/index.html