• Create Account

## Writing portable code

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

12 replies to this topic

### #1schupf  Members

221
Like
1Likes
Like

Posted 19 June 2011 - 10:52 AM

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?

### #2Jethro_T  Members

194
Like
0Likes
Like

Posted 19 June 2011 - 11:10 AM

I would make a single class, seems like it would be easier to maintain and there would be a bit less redundant code.

### #3schupf  Members

221
Like
0Likes
Like

Posted 19 June 2011 - 11:23 AM

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:/

### #4phantom  Members

10708
Like
4Likes
Like

Posted 19 June 2011 - 11:29 AM

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.

### #5Roots  Members

1625
Like
0Likes
Like

Posted 19 June 2011 - 01:49 PM

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...

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

### #6schupf  Members

221
Like
0Likes
Like

Posted 19 June 2011 - 02:17 PM

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"?

### #7ChaosEngine  Members

4739
Like
1Likes
Like

Posted 19 June 2011 - 05:37 PM

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

### #8wqking  Members

756
Like
0Likes
Like

Posted 19 June 2011 - 07:38 PM

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.

http://www.cpgf.org/
cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.
v1.5.5 was released. Now supports tween and timeline for ease animation.

### #9schupf  Members

221
Like
0Likes
Like

Posted 20 June 2011 - 01:51 AM

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...

### #10MJP  Moderators

18184
Like
0Likes
Like

Posted 20 June 2011 - 02:08 AM

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.

### #11Lauris Kaplinski  Members

841
Like
0Likes
Like

Posted 20 June 2011 - 02:18 AM

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...

If you plan to port your code to Linux, you will have to use some GUI library anyways I think. Writing bare XWindows interface is possible, but nothing I'd suggest anyone to do - especially if you only need one dialog ;-)
And if you have to use some GUI library anyways, why not use something like wxWidgets for both Windows and Linux versions? You can link it statically, so only the needed parts of the library will be included in your program - probably about 100kb or less. Yes - it is slightly bigger than a dialog written by bare WinAPI - but in my opinion you save a lot more in source code complexity.

Lauris Kaplinski

First technology demo of my game Shinya is out: http://lauris.kaplinski.com/shinya
Khayyam 3D - a freeware poser and scene builder application: http://khayyam.kaplinski.com/

### #12mrbastard  Members

1576
Like
0Likes
Like

Posted 20 June 2011 - 05:24 AM

Then when building on a platform you only build the .cpp files for that platform + common.

### #13Slavik81  Members

360
Like
0Likes
Like

Posted 20 June 2011 - 01:01 PM

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...

I statically linking against it possible, but is not recommended. Qt is huge.

http://doc.qt.nokia.com/latest/deployment.html#static-vs-shared-libraries

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.