在跨平台库上工作

时间:2022-11-29 12:14:51

What are the best practices on writing a cross platform library in C++?

在C ++中编写跨平台库的最佳实践是什么?

My development environment is Eclipse CDT on Linux, but my library should have the possibility to compile natively on Windows either (from Visual C++ for example).

我的开发环境是Linux上的Eclipse CDT,但是我的库应该可以在Windows上进行原生编译(例如,从Visual C ++)。

Thanks.

7 个解决方案

#1


5  

To some extent, this is going to depend on exactly what your library is meant to accomplish.

在某种程度上,这将取决于您的图书馆要完成的目标。

If you were developing a GUI application, for instance, you would want to focus on using a well-tested cross-platform framework such as wxWidgets.

例如,如果您正在开发GUI应用程序,那么您可能希望专注于使用经过良好测试的跨平台框架,例如wxWidgets。

If your library depends primarily on File IO, you would want to make sure you use an existing well-tested cross-platform filesystem abstraction library such as Boost Filesystem.

如果您的库主要依赖于文件IO,那么您需要确保使用现有经过良好测试的跨平台文件系统抽象库,例如Boost Filesystem。

If your library is none of the above (i.e. there are no existing well-tested cross-platform frameworks for you to use), your best bet is to make sure you adhere to standard C++ as much as possible (this means don't #include <linux.h> or <windows.h>, for instance). When that isn't possible (i.e. your library reads raw sound data from a microphone), you'll want to make sure the implementation details for a given platform are sufficiently abstracted away so that you minimize the work involved in porting your library to another platform.

如果您的库不属于上述类型(即没有现成的经过良好测试的跨平台框架供您使用),最好的办法是确保尽可能遵守标准C ++(这意味着不要#例如,包括 )。如果不可能(即您的库从麦克风读取原始声音数据),您将需要确保给定平台的实现细节被充分抽象化,以便最大限度地减少将库移植到另一个库所涉及的工作。平台。

#2


2  

To my knowledge, there are a few things you can do:

据我所知,你可以做一些事情:

  1. You can divide the platform specific code into different namespaces.

    您可以将特定于平台的代码划分为不同的命名空间。

  2. You can use the PIMPL idiom to hide platform specific code.

    您可以使用PIMPL惯用法隐藏特定于平台的代码。

  3. You can use macros do know what code to compile (in this case the code will be platform specific). Check this link for more information.

    您可以使用宏确实知道要编译的代码(在这种情况下代码将是特定于平台的)。请查看此链接以获取更多信息。

  4. Test your library in multiple environments.

    在多种环境中测试您的库。

  5. Depending on what you are doing it might be good to use libraries such as Boost because it is not specific to a platform. The downside (or possibly the good side) is that you will force the use of the libraries you included.

    根据您正在做的事情,使用诸如Boost之类的库可能会很好,因为它不是特定于平台的。缺点(或可能是好的一面)是您将强制使用您包含的库。

#3


1  

Couple of suggestions from my practical experience:

根据我的实践经验提出的几点建议:

1) Make sure of regular compilation of sources in your targeted platforms. Don't wait till the end. This'd help point to errors early. Use a continuous build system -- it makes life a lot easier.

1)确保定期编译目标平台中的源代码。不要等到最后。这有助于尽早指出错误。使用连续构建系统 - 它使生活更容易。

2) Never use platform specific headers. Not even for writing native code -- for all you know some stuff in a windows header might expect some string which was ABC in XP but got changed to ABC.12 in Win7.

2)切勿使用特定于平台的标头。甚至不是为了编写本机代码 - 对于所有你知道的东西,在Windows标题中的某些东西可能会期望一些字符串在XP中是ABC但在Win7中被改为ABC.12。

3) Use ideas from STL and BOOST and then build on top of them. Never consider these to be a panacea for problems though -- STL is easy to ship with your code but BOOST is not.

3)使用来自STL和BOOST的想法,然后在它们之上构建。永远不要认为这些是解决问题的灵丹妙药 - STL很容易随你的代码一起提供,但BOOST不是。

4) Do not use compiler specific constructs like __STDCALL. This is asking for hell.

4)不要使用像__STDCALL这样的编译器特定结构。这是在寻求地狱。

5) The same code when compiled with similar compiler options in g++ and cl might result in different behavior. Please have a copy of your compiler manual very handy.

5)在g ++和cl中使用类似的编译器选项编译时,相同的代码可能会导致不同的行为。请准备好您的编译器手册的副本。

#4


0  

Anytime I work on something like this I try and build it in the different environments that I want to be supported. Similarly if you were making a web page and you wanted to make sure it worked in IE, Firefox, and Chrome you'd test it in all three of those browsers. Test it in the different environments you want to support, and you'll know what systems you can safely say it works for.

无论何时我在这样的事情上工作,我都会尝试在我想要支持的不同环境中构建它。同样,如果你正在创建一个网页,并且你想确保它在IE,Firefox和Chrome中运行,你可以在所有这三种浏览器中测试它。在您想要支持的不同环境中进行测试,您将知道可以安全地说它适用的系统。

#5


0  

question as stated is bit abstract.but you can give QT a consideration

如上所述的问题有点抽象。但你可以考虑QT

#6


-2  

It's really just as simple as "don't using anything platform specific". The wealth of freely available tools availalble these days makes writing cross-platform code in C++ a snap. For those rare but occasional cases where you really do need to use platform specific APIs, just be sure to separate them out via #defines or, better in my opinion, distinct .cpp files for each platform.

这真的就像“不使用任何特定平台”一样简单。如今可用的大量免费工具使得用C ++编写跨平台代码变得轻而易举。对于那些您确实需要使用平台特定API的罕见但偶然的情况,请确保通过#defines将它们分开,或者在我看来,更好地将每个平台的不同.cpp文件分开。

There are many alternatives for cross platform libraries but my personal preferences are:

跨平台库有很多替代方案,但我的个人偏好是:

  • GUI: Qt
  • OS abstraction (though Qt does a great job of this all by itself): Boost
  • 操作系统抽象(尽管Qt本身做得很好):提升

  • Cross-platform Makefiles: CMake
  • 跨平台Makefile:CMake

The last one, CMake, has been a tremendous help for me over the last few years for keeping my build environment sane while doing dual-development on Windows & Linux. It has a rather steep learning curve but once it's up and running, it works exceptionally well.

最后一个,CMake,在过去的几年里,对我来说是一个巨大的帮助,可以在Windows和Linux上进行双重开发时保持构建环境的合理性。它有一个相当陡峭的学习曲线,但一旦它启动并运行,它的效果非常好。

#7


-3  

You mean besides continuous integration and testing on target platforms? Or besides using design to abstract away the implementation details?

您的意思是除了在目标平台上持续集成和测试之外或者除了使用设计抽象出实现细节之外?

No, can't think of anything.

不,想不出什么。

#1


5  

To some extent, this is going to depend on exactly what your library is meant to accomplish.

在某种程度上,这将取决于您的图书馆要完成的目标。

If you were developing a GUI application, for instance, you would want to focus on using a well-tested cross-platform framework such as wxWidgets.

例如,如果您正在开发GUI应用程序,那么您可能希望专注于使用经过良好测试的跨平台框架,例如wxWidgets。

If your library depends primarily on File IO, you would want to make sure you use an existing well-tested cross-platform filesystem abstraction library such as Boost Filesystem.

如果您的库主要依赖于文件IO,那么您需要确保使用现有经过良好测试的跨平台文件系统抽象库,例如Boost Filesystem。

If your library is none of the above (i.e. there are no existing well-tested cross-platform frameworks for you to use), your best bet is to make sure you adhere to standard C++ as much as possible (this means don't #include <linux.h> or <windows.h>, for instance). When that isn't possible (i.e. your library reads raw sound data from a microphone), you'll want to make sure the implementation details for a given platform are sufficiently abstracted away so that you minimize the work involved in porting your library to another platform.

如果您的库不属于上述类型(即没有现成的经过良好测试的跨平台框架供您使用),最好的办法是确保尽可能遵守标准C ++(这意味着不要#例如,包括 )。如果不可能(即您的库从麦克风读取原始声音数据),您将需要确保给定平台的实现细节被充分抽象化,以便最大限度地减少将库移植到另一个库所涉及的工作。平台。

#2


2  

To my knowledge, there are a few things you can do:

据我所知,你可以做一些事情:

  1. You can divide the platform specific code into different namespaces.

    您可以将特定于平台的代码划分为不同的命名空间。

  2. You can use the PIMPL idiom to hide platform specific code.

    您可以使用PIMPL惯用法隐藏特定于平台的代码。

  3. You can use macros do know what code to compile (in this case the code will be platform specific). Check this link for more information.

    您可以使用宏确实知道要编译的代码(在这种情况下代码将是特定于平台的)。请查看此链接以获取更多信息。

  4. Test your library in multiple environments.

    在多种环境中测试您的库。

  5. Depending on what you are doing it might be good to use libraries such as Boost because it is not specific to a platform. The downside (or possibly the good side) is that you will force the use of the libraries you included.

    根据您正在做的事情,使用诸如Boost之类的库可能会很好,因为它不是特定于平台的。缺点(或可能是好的一面)是您将强制使用您包含的库。

#3


1  

Couple of suggestions from my practical experience:

根据我的实践经验提出的几点建议:

1) Make sure of regular compilation of sources in your targeted platforms. Don't wait till the end. This'd help point to errors early. Use a continuous build system -- it makes life a lot easier.

1)确保定期编译目标平台中的源代码。不要等到最后。这有助于尽早指出错误。使用连续构建系统 - 它使生活更容易。

2) Never use platform specific headers. Not even for writing native code -- for all you know some stuff in a windows header might expect some string which was ABC in XP but got changed to ABC.12 in Win7.

2)切勿使用特定于平台的标头。甚至不是为了编写本机代码 - 对于所有你知道的东西,在Windows标题中的某些东西可能会期望一些字符串在XP中是ABC但在Win7中被改为ABC.12。

3) Use ideas from STL and BOOST and then build on top of them. Never consider these to be a panacea for problems though -- STL is easy to ship with your code but BOOST is not.

3)使用来自STL和BOOST的想法,然后在它们之上构建。永远不要认为这些是解决问题的灵丹妙药 - STL很容易随你的代码一起提供,但BOOST不是。

4) Do not use compiler specific constructs like __STDCALL. This is asking for hell.

4)不要使用像__STDCALL这样的编译器特定结构。这是在寻求地狱。

5) The same code when compiled with similar compiler options in g++ and cl might result in different behavior. Please have a copy of your compiler manual very handy.

5)在g ++和cl中使用类似的编译器选项编译时,相同的代码可能会导致不同的行为。请准备好您的编译器手册的副本。

#4


0  

Anytime I work on something like this I try and build it in the different environments that I want to be supported. Similarly if you were making a web page and you wanted to make sure it worked in IE, Firefox, and Chrome you'd test it in all three of those browsers. Test it in the different environments you want to support, and you'll know what systems you can safely say it works for.

无论何时我在这样的事情上工作,我都会尝试在我想要支持的不同环境中构建它。同样,如果你正在创建一个网页,并且你想确保它在IE,Firefox和Chrome中运行,你可以在所有这三种浏览器中测试它。在您想要支持的不同环境中进行测试,您将知道可以安全地说它适用的系统。

#5


0  

question as stated is bit abstract.but you can give QT a consideration

如上所述的问题有点抽象。但你可以考虑QT

#6


-2  

It's really just as simple as "don't using anything platform specific". The wealth of freely available tools availalble these days makes writing cross-platform code in C++ a snap. For those rare but occasional cases where you really do need to use platform specific APIs, just be sure to separate them out via #defines or, better in my opinion, distinct .cpp files for each platform.

这真的就像“不使用任何特定平台”一样简单。如今可用的大量免费工具使得用C ++编写跨平台代码变得轻而易举。对于那些您确实需要使用平台特定API的罕见但偶然的情况,请确保通过#defines将它们分开,或者在我看来,更好地将每个平台的不同.cpp文件分开。

There are many alternatives for cross platform libraries but my personal preferences are:

跨平台库有很多替代方案,但我的个人偏好是:

  • GUI: Qt
  • OS abstraction (though Qt does a great job of this all by itself): Boost
  • 操作系统抽象(尽管Qt本身做得很好):提升

  • Cross-platform Makefiles: CMake
  • 跨平台Makefile:CMake

The last one, CMake, has been a tremendous help for me over the last few years for keeping my build environment sane while doing dual-development on Windows & Linux. It has a rather steep learning curve but once it's up and running, it works exceptionally well.

最后一个,CMake,在过去的几年里,对我来说是一个巨大的帮助,可以在Windows和Linux上进行双重开发时保持构建环境的合理性。它有一个相当陡峭的学习曲线,但一旦它启动并运行,它的效果非常好。

#7


-3  

You mean besides continuous integration and testing on target platforms? Or besides using design to abstract away the implementation details?

您的意思是除了在目标平台上持续集成和测试之外或者除了使用设计抽象出实现细节之外?

No, can't think of anything.

不,想不出什么。