Author Topic: Cross Platform Programming issue  (Read 1985 times)

Offline Dr.Optix

  • Multiple posting newcomer
  • *
  • Posts: 30
  • I'm studying OS Developing.
    • DrOptix Blog
Cross Platform Programming issue
« on: May 18, 2009, 07:18:56 pm »
Hope I don't violate forum post policy but here is my little problem

I want to create a GUI framework in c++ the aim is to hit both linux and windows.

Because you are cross platform developers I ask you how you would do it.

1. Write separate versions of the framework(or any piece of cross platform app), one around Win32 API and one around GTK+?
2. You would write one project and use a lot of
    #ifdef _WIN32
        //win32 stuff here
    #else
        //linux/gtk+ stuff here

Thanx for your time and hope someone will tell me how it is recommended to be done

~Dr.Optix
Occupation: Hobby Programmer
IDE: Code::Blocks Nightly / CodeLite Latest Stable
Compiler: TDM's GCC/mingw32
Blog: DrOptix.WordPress.Com

Offline killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 5215
Re: Cross Platform Programming issue
« Reply #1 on: May 18, 2009, 08:37:48 pm »
or hide it behind interfaces, and have one cpp implement it for linux, and another cpp for windows.
And then you can create an app with CB, with a linux target and windows target. You add the files to the project but only to the target they should belong to (the linux file only to the linux target) etc ...

That's one way ;-)

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: Cross Platform Programming issue
« Reply #2 on: May 19, 2009, 11:24:51 am »
Because you are cross platform developers I ask you how you would do it.
Well, in this project, we went the "easy" way and just used wxWidgets. That's nearly as much trouble as writing your own cross-platform kit, though (and some things still don't work properly on non-Windows).

What I'm using outside this project is a set of headers that implement platform-agnostic wrappers around the most important "concepts". A few typedefs can work wonders for becoming platform agnostic, all that's left then is to call the proper OS functions. In some cases (for example sockets, dns lookup, basically almost everything related to networking), you only need the typedefs and 1-2 helper functions at all, the remainder of the code is identical for all platforms.
If you keep things simple, a surprising amount of your toolkit can be wrapped in 1-2 lines of inline code in the headers. This is not what everyone prefers, but it works nicely and does not bloat up your code, nor does it need any extra files/libraries.
As for #ifdef, I find them extremely ugly and hard to maintain, therefore I clone the entire definition of a class and put everything belonging to one platform into one #ifdef block (much different to what most people do), or even into separate include files (with only an #include statement inside #ifdef in the "real" header).
Whatever is not suitable for being in the headers for some reason is compiled into a static library, which is more convenient than having to add .cpp files to the project. The linker sorts it out just fine.
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."