Find the answer to your Linux question:
Results 1 to 2 of 2
Hi all, I have a basic question, I have code that is written for linux (has unix specific calls that are not included in windows api) and I need to ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2011
    Posts
    1

    C++ code in Linux and windows


    Hi all,

    I have a basic question, I have code that is written for linux (has unix specific calls that are not included in windows api) and I need to port it to windows. It must remain backwards compatible with unix environment though.

    What is the best way to achieve this? Currently, I've been going through adding
    Code:
    #ifndef WIN32
    // perform unix-specific calls
    #else
    // perform windows specific calls
    #endif
    Is this the best way? The project has about 8 or 9 objects being used, so it is fairly small in size, and the basic operations are file I/O with timestamps.

    Thank you all.

  2. #2
    Super Moderator Roxoff's Avatar
    Join Date
    Aug 2005
    Location
    Nottingham, England
    Posts
    3,882
    You have lots of options, how about encapsulating the different calls into two derived classes providing the service? Sort of like...

    Code:
    class service
    {
    public:
        do_something();  // which calls system_call_x() below to do 'systemsy' stuff
    protected:
        virtual system_call_x();
    }
    
    class service_w : public service
    {
    protected:
        virtual system_call_x();
    }
    
    class service_l : public service
    {
    protected:
        virtual system_call_x();
    }
    Store your instance in a pointer to the parent class, but instantiate the one that matches the system you're compiling for. You don't even need to include the class that handles the 'other' platform in each of your builds.
    Linux user #126863 - see http://linuxcounter.net/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •