Coding for multiple platforms

Discussion in 'C++' started by Joe C, Jan 6, 2004.

  1. Joe C

    Joe C Guest

    Just learning C++...
    I've recently made a leap and compiled some code I wrote with Windows in
    mind for Linux. It wen't smoothly (other than having to learn about a new
    compiler/os!!), However, I did have to make some changes to the code. For
    example, the code needs to be aware of how filenames/paths are defined so
    that it can parse filespecs and come up with new names. In the first
    iteration, I just swapped out the symbols. However, As I'd prefer not to do
    such work more than once, I decided to use a header ("version.h") that
    contains some constants such as endianness, operating_system. THen I
    extern-link to these variables and have pieces of code in the various
    translation units that activate, depending on the contents of version.h.

    My this a fairly standard way to deal with platform-specific
    issues in "portable" code, or is there an easier way? So far, I'm not using
    the preprocessor to deal with this stuff.

    An brief example follows...

    const int operating_system(0); // (0 = Win, 1 = Linux);

    extern const int operating_system;

    void parseit(){

    string temp = fs;
    char path_seperator('\\'); // default values...Windows
    char extension_indicator('.'); // default values...Windows

    extern const int operating_system;
    case 0: // Windows
    path_seperator = '\\';
    extension_indicator = '.';
    case 1: // Unix
    path_seperator = '/';
    extension_indicator = '.';
    // do stuff

    Thanks, Joe
    Joe C, Jan 6, 2004
    1. Advertisements

  2. Joe C

    Ron Natalie Guest

    Actually, you usually know the UNIX versus Windows at compile time, so I'd do
    something more along these lines:

    #if WINDOWS
    const char path_separator = '\\';
    #else //UNIX
    const char path_separator = '/';

    and put it some common include files
    Ron Natalie, Jan 6, 2004
    1. Advertisements

  3. Usually, you know your platform at compile time, so you could just
    include a common header file that contained platform-specific macros.
    cK-Gunslinger, Jan 6, 2004
  4. Why not just use / throughout? AFAIK, windows accepts it as well.
    S. Armondi
    Samuele Armondi, Jan 6, 2004
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.