How to make C++ code Portable?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • siyuangao
    New Member
    • Sep 2009
    • 4

    #1

    How to make C++ code Portable?

    I am doing a group project that sometimes is going to be tested under Linux operating system but some function like
    system("cls") only works on windows. I found a answer here says to use a header file:
    Code:
    // #define LINUX
    // #define WINDOWS
    // #define OSX
    then add this in the source file
    Code:
    #ifdef LINUX
    char os[] = "Linux";
    #else
    #ifdef WINDOWS
    char os[] = "Windows";
    #else
    char os[] = "Mac OSX";
    #endif
    #endif
    I just want to check if there a easier way to do, I am using GCC compiler.
    Thank you.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    The system() call should work in Linux but I believe the arguments to system() are OS dependent. That is, "cls" may only work on Windows and some things that work on Linux won't work on Windows.

    Comment

    Working...