Thread: learning cross-platform development

  1. #1
    UK2
    Join Date
    Sep 2003
    Posts
    112

    learning cross-platform development

    Hello,

    I am want to be able to write applications that will run on windows and linux.

    I know there are libraries that help you do this, apache-runtime, glib, etc.

    However, before I get into those I want to be able to do it at the low level, using things like this, and I am just a beginner:

    Code:
    #ifdefine WIN32
    /* Do windows stuff
    #else
    /* do linux stuff */
    #endif
    I would like to understand this a lot a the low lever, and wondering if there are any good tutorials or ebooks that would help me get started?

    Many thanks for any advice,

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    There is no real tutorial for these things because there is no need for any.
    The first rule is to stick to ISO C. ISO C is always portable.
    Second rule is when you have to use something else than ISO C, use a portable library, if possible.
    Third rule is if you can't use a library, then put the functionality you need into a function and surround it by ifdefs like above, to customize that function to work differently for each platform.

    These 3 basic rules will carry you to portability.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Cross-platform development is just using things that work on multiple platforms. If it doesn't work on say, both Linux and Windows, then you write a wrapper function that calls the appropriate calls on the appropriate OS, usually using techniques like you showed. For example, if I wanted a sub-second 'sleep' function, I'd have to write a function that, in Linux, calls usleep or nanosleep, but in Windows would probably call Sleep(). However, if I'm just opening a file for a quick write, I wouldn't use open() / CreateFile(), because there's already a portable function: fopen().

    It's all just a translation of "what do I want to do" to "what OS calls do I need to make, and what's the most portable way to do it?".
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Your example is not the best implementation what would be defined as "cross platform".

    Good cross platform code has as few of those techniques as possible.

    I would suggest learning C first, from a command line standpoint, using a C reference that has as little slant to any platform as you can stand.
    Mainframe assembler programmer by trade. C coder when I can.

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    142
    Actually there are two books that deal with multiplatform development:

    1. Write Portable Code: An Introduction to Developing Software for Multiple Platforms
    2. Cross-Platform Development in C++: Building Mac OS X, Linux, and Windows Applications

    I have an application that works on Mac OS 9/OS X and Windows and writing it was very hard and it took a lot of time and energy, but now it's a real pleasure. I just spend my days on Mac adding features and then just drag common files to Windows, recompile and voila: pure magic. Once you have it that way, it's like eating the cake and having it too.


  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    If you want to go really cross-platform, when you do the linux stuff go for POSIX compliance. This basically means avoiding a few GNU specific functions, which you probably want to do anyway.

    But POSIX stuff compiles on linux flavours, unix (eg Sun), FreeBSD, etc. When I did my thing for debian, they asked that I keep everything POSIX compliant. It is a close subset of the C standard:

    The Open Group Base Specifications Issue 7
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cross platform XML library
    By Josh Kasten in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2007, 04:04 PM
  2. Choosing a development platform
    By bobthebullet990 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-11-2007, 12:48 AM
  3. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  4. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM
  5. cross platform game programming
    By xddxogm3 in forum Game Programming
    Replies: 13
    Last Post: 08-22-2004, 09:40 AM