Arthur
Arthur

Reputation: 11

How to make C++ program work across compilers

I wanted to know how I would make my C++ program work across compilers. I wanted to make the program so if it's being compiled with borland it will use the clrscr() function otherwise it'd use system("CLS"). I've seen code that has done something similar but I couldn't find an explanation of what it does or how it works. Any help would be appreciated.

Upvotes: 1

Views: 1750

Answers (5)

Ashalynd
Ashalynd

Reputation: 12563

One easy answer from the top of the head is define your own function calls and then translate it into real calls depending on the compiling parameters (with #ifdef preprocessing definitions - look which values are corresponding to which compiler). example:

#if defined(__COMPILER_ONE__)
#define ClearScreen() clrscr()
#elif defined(__COMPILER_TWO__)
#define ClearScreen() system("CLS")
#else 
#error "I do not know what to do!"
#endif

You would have to create a dedicated header file for this and to include it everywhere, of course.

(Of course you have to substitute COMPILER_ONE and COMPILER_TWO with relevant definitions :) )

Upvotes: 3

Hello71
Hello71

Reputation: 816

You can do this by checking compiler macros with the #ifdef compiler macro:

#ifdef BORLAND
    borland();
#else
    otherCompiler();
#endif

Upvotes: 0

user197015
user197015

Reputation:

In general, to make a C or C++ program work across multiple compilers you want to confine yourself to standard C or C++ as much as possible. Sometimes you have to use compiler/platform specific functionality, though, and one way to handle that is via the preprocessor.

The predef project on SourceForge lists a bunch a preprocessor symbols that are defined automatically by various compilers, for various platforms, et cetera. You can use that information to implement what you need, for example:

void clearScreen() {
  // __BORLANDC__ is defined by the Borland C++ compiler.
  #ifdef __BORLANDC__
    clrscr();
  #else
    system("cls");
  #endif
}

Upvotes: 9

Josh Matthews
Josh Matthews

Reputation: 13026

The way it's typically done is through the magic of the preprocessor or makefiles. Either way, you hide the implementation details behind a common interface in a header file, such as void clearscreen(). Then in a single source file you can hide the Borland implementation behind #ifdef BORLAND, and similarly for other implementations. Alternatively, you can put each implementation in a separate source file, and only compile the proper one based on a variable in a makefile.

Upvotes: 0

Chris Huang-Leaver
Chris Huang-Leaver

Reputation: 6089

How to make something work across different compilers is simple question which is very complex to answer! Your specific query about clearing the screen;

I would attempt it like this, first you have your own function say

      void clear_screen();

And define it like this:

      void clear_screen()
      {
        #ifdef LINUX
           ...

        #eleif MS_WIN

          ...
        #endif
      }

Please note I have just guessed what the #define 's are. This is know as conditional complication, generally regarded as evil, but containing it in a function reduces the harm a little.

Upvotes: 0

Related Questions