Multi-platform support issues.

This is a discussion on Multi-platform support issues. within the C Programming forums, part of the General Programming Boards category; I've been recently writing an application that will support many different platforms. Basically, this utility will support different flavors of ...

  1. #1
    Welcome to the real world
    Join Date
    Feb 2004
    Posts
    50

    Multi-platform support issues.

    I've been recently writing an application that will support many different platforms. Basically, this utility will support different flavors of *nix to win32 platforms.

    I was wondering if anybody sees any problem using preprocessor directives within source code (not just header files). For example:

    Code:
    #include <stdio.h>
    
    int main (int argc, char *argv[]) {
        #ifdef WIN32
            ... do something here win32 specific
        #else
            ...do something *nix specific
        #endif
    
        return 0;
    }
    Are there any issues I need to look out for while implementing this project? Thank you.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Personally, I don't see any problem with it. The idea would be of course to make as much of your code generic as possible, with the necessary platform specific stuff handled with preprosessor directives. I'm sure you could find a way to make it horribly unreadable doing so, but done even half way decent, I don't see a problem. Furthermore, that may be the only way to do it in some cases.

    For example, if you had to have a screen paint function, you'd likely have to do more than just header file manipulation to make it paint correctly for both platforms. You could generalize the function prototypes perhaps, but when it came down to the actual code itself, it's likely that you'd be forced to do it that way.

    I hate to say forced, because you can find some way around most situations, but some times it's best to go for what works well, or is clear to the reader. Or both.

    Just my thought on the matter.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    sockets mad
    Join Date
    Mar 2002
    Posts
    126
    Try to put the any code which requires OS preprocessor directives into subroutines so that the code that contains preprocessor directives is not normally visible. That way you only need see the preprocessor ridden code when you need to edit it directly.

    I guess what I'm saying is create a sort of platform specific level in your code which hides the implementation of OS specific code where-ever possible. Does wonders for readability and keeps all those preprocessor directives hidden away.

    I normally have a platform.h header file which I include in all my .cpp files which contains a lot of platform specific defines and includes.

    Here's an example of a simple platform.h that supports sockets.

    Code:
    #ifndef _INCLUDED_PLATFORM_H_
    #define _INCLUDED_PLATFORM_H_
    
    #ifdef _WIN32
    #define WIN32_LEAN_AND_MEAN
    #include <winsock2.h>
    #include <windows.h>
    #define ERRNO (WSAGetLastError())
    typedef int socklen_t;
    #else
    typedef int SOCKET;
    #include <errno.h>
    #include <arpa/inet.h>
    #include <netinet/in.h>
    #include <sys/socket.h>
    #define wsprintf sprintf
    #define closesocket(s) close(s)
    #define ERRNO errno
    #define stricmp(x,y) strcasecmp(x,y)
    #define strnicmp(x,y,z) strncasecmp(x,y,z)
    #endif
    
    #ifndef INADDR_NONE
    #define INADDR_NONE 0xffffffff
    #endif
    
    #ifndef INADDR_ANY
    #define INADDR_ANY 0
    #endif
    
    #ifndef SHUT_RDWR
    #define SHUT_RDWR 2
    #endif
    
    #endif // INCLUDED__PLATFORM_H_
    Hope that helps.

    -Daniel
    Last edited by codec; 04-27-2004 at 06:47 PM.
    C/C++ Support IRC Channel

    Server: irc.dal.net Channel: #csupport

    Come along and help make it a great resource for the community.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 02:49 PM
  2. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 09:43 AM
  3. Dev-cpp - compiler options
    By tretton in forum C Programming
    Replies: 7
    Last Post: 01-06-2006, 05:20 PM
  4. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 06:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21