views:

139

answers:

7

What are the best practices on writing a cross platform library in 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).

Thanks.

A: 

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.

Noah Roberts
A: 

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.

Ben Burnett
Web development and application development is quite different. ;)
Alerty
Not in the context of what I wrote. You need to test your code on every platform you intend for it to be used on.
Ben Burnett
I agree with you. The only thing is that Alon might not know anything about Web development.
Alerty
Actually, I'm coming from the web development background :)
TTT
You must hate IE. XD
Alerty
+3  A: 

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.

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.

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.

Mark Rushakoff
True. Also [http://www.gtk.org/download-windows.html glib] has abstractions for all the lowest-level C-like stuff such as malloc and strcmp.
Pat Wallace
As an addendum, I would add that it is typically useful to have your platform-specific code (if there is any) separated into its own module. The rest of your code is platform-agnostic and can perform platform-specific tasks by calling interface functions. That way, porting your library simplifies down (for the most part) into porting that one single module.
bta
The only thing I would point out is that you do not necessarily want to force the need for another library. One might want it to work independently (STL being an exception because it is part of the C++ standard).
Alerty
A: 

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

pushkarpriyadarshi
A: 

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.

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.

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.

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

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.

Fanatic23
A: 

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.

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
  • Cross-platform Makefiles: 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.

Rakis
-1: You can use platform specific code. One simply needs to put the necessary measures so that this code is not compiled for a certain environment.
Alerty
@Alerty Please *read* answers before down voting.
Rakis
@Rakis: The first thing you suggest is to not use platform specific code. Not to mention that you are suggesting to use existing libraries when it is not necessarily the best course of action. Of course, you are saying that it is possible to use platform specific code. The problem is that you are defining this has being a rare but occasional case without knowing what type of library Alon is doing.
Alerty
@Alerty You're right. I'm just one of those crazy people that think *general* questions merit *general* answers. Not to worry though, through the magic combination of origami and tin foil, my mind-reading device will be ready in no time :)
Rakis
+1  A: 

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.

  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.

Alerty