Writing portable code

Started by
11 comments, last by Slavik81 12 years, 10 months ago
Hi,

I have a program that needs to compile and run under Linux and Windows. I have a Dialog class that draws a very simple Dialog (on Windows it uses plain WinAPI, on Linux... I don't know. Haven't been so far yet;)

Should I know write two totally different classes (one for windows and one for linux) and just exclude the one that does not fit to the current platform or should I make one single class (one .cpp and one .h) and just code the platform specific parts inside the methods with #ifdefs?
Advertisement
I would make a single class, seems like it would be easier to maintain and there would be a bit less redundant code.
Yes, its certainly easier to maintain, but I fear the could would be a total mess. Each function is totally platform dependent (for example I have a method like Dialog::fillResolutionComboBox() - filling a combobox on Win32 is totally different compared to Linux). Moreover I am using a lot of DirectX types, which are also bound to Windows:/
One way of solving this is with one class but with multiple files.

So, you have a single header which defines the class interface, then you have 3 .cpp files; one which has common code in it which works for both platforms, then in the platform specific .cpp files you only do the functions which need to be done for that platform specifically. Then when building on a platform you only build the .cpp files for that platform + common.

This might require some #ifdef stuff in the header to sort out platform specific header information but that is going to be cleaner than having inline #ifdef stuff in a single .cpp file.

One way of solving this is with one class but with multiple files.

So, you have a single header which defines the class interface, then you have 3 .cpp files; one which has common code in it which works for both platforms, then in the platform specific .cpp files you only do the functions which need to be done for that platform specifically. Then when building on a platform you only build the .cpp files for that platform + common.

This might require some #ifdef stuff in the header to sort out platform specific header information but that is going to be cleaner than having inline #ifdef stuff in a single .cpp file.


I endorse this suggestion.


As someone who has been writing portable code for years, my general recommendation is to stay the hell away from absolutely any library or framework that is not well implemented and well supported on your target platforms (note: some libraries claim to be "cross-platform", but their implementation really sucks on certain systems or is not fully complete). Don't use DirectX, use OpenGL (unless you want to provide both DX and OGL implementations to your users, which some games do). Don't use WinAPI, use SDL for window and input management. And so on. Of course this is just a suggestion and you can choose to use platform-specific libraries if you want, but in the long run I think it will be more work than what it is worth. Cross-platform development is already a challenge so I don't see why you would want to make it even more difficult, unless you take pleasure in pain...:o

Hero of Allacrost - A free, open-source 2D RPG in development.
Latest release June, 2015 - GameDev annoucement

Ok this basically means I have a Dialog.h, a Dialog.cpp (platform independent stuff), DialogWin.cpp and DialogLinux.cpp. And in my Windows project I just exclude DialogLinux.cpp from compilation. Is this correct?
Should I make this exclusion by project settings (makefile etc.) or with #ifdefs, so that DialogLinux.cpp gets to the compiler but isn't compiled since everything is "commented out by ifdef"?
Why don't you just use a cross platform GUI library, like WxWidgets or QT?
if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight

Why don't you just use a cross platform GUI library, like WxWidgets or QT?


+1.
There are already a lot of cross platform GUI libraries, and some are quite lightweight.
wxWidgets is lightweight, I believe some others are more lightweight than it.

So we don't need to reinvent the wheel.

I would spend my most time on app logic/features, etc, rather than cross platform issues when there are libraries can handle them.

https://www.kbasm.com -- My personal website

https://github.com/wqking/eventpp  eventpp -- C++ library for event dispatcher and callback list

https://github.com/cpgf/cpgf  cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.

Well, because I really just need ONE Dialog and I hate dependencies. Even one DLL or .so are too much in my opinion for just one window.

If I picked a GUI toolkit I would definitely go for Qt. But I neither know if it is allowed to link Qt statically nor do I think the extra amount of work (compiling Qt statically will probably last hours) for just ONE Window...

This might require some #ifdef stuff in the header to sort out platform specific header information but that is going to be cleaner than having inline #ifdef stuff in a single .cpp file.


If this gets to be messy, you can also use a pimpl-esque struct containing the member data for each platform:

// Header File

struct DialogData;

class Dialog
{
private:
DialogData* data;

...
};



// CPP File for Windows

struct DialogData
{
HWND windowHandle;
...
};

Dialog::Dialog()
{
data = new DialogData();

// Create window, do other stuff
}


It adds an extra pointer indirection whenever the class needs to access some of its data, but keeps your header files cleaner. It can also help prevent the platform-specific headers from leaking out into cross-platform code.

This topic is closed to new replies.

Advertisement