NCS_CENV.H: Useful C/C++ header for identifying compiler and target operating system

Programmers often find it necessary to selectively compile code based on the characteristics of the target environment, which vary in word size and, of course, operating system.

Over the years I've developed this header file for use with my own projects. It supports various Microsoft, Watcom, and Borland C and C++ compilers for 16, 32, and 64-bit code targeting DOS, OS/2, and all versions of Windows from 3.x to the current releases. In my opinion it is a very clean approach to this problem and would serve as a good framework for incorporating support for additional compilers or environments.

While retaining all copyrights to the code, I encourage you to use or modify it in your own programs as long as you don't claim ownership of it. I would love to see your modifications.

Here is a direct link to the source file, which is also listed below:

/****************************************************************************/
/*                                                                          */
/*  NCS_CENV.H                                                              */
/*                                                                          */
/*  Header for compiler environment support                                 */
/*                                                                          */
/*  Copyright (C) 1990-2015 by Allen Keith Nash                             */
/*  All rights reserved.                                                    */
/*                                                                          */
/****************************************************************************/

/* TLIB[@status] "%n [%v] %f" */
/* "ncs_cenv.h [1] 06-May-14,09:57:24" */

#ifndef _NCS_CENV_H
#define _NCS_CENV_H

/*****************************************************************************

Environments supported:

    __WATCOM_DOS16__    Watcom, DOS                 16-bit

    __WATCOM_DOS32__    Watcom, DOS4GW DOS extender 32-bit

    __WATCOM_WIN16__    Watcom, Windows 3.x         16-bit

    __WATCOM_WIN32__    Watcom, Windows 95 & NT     32-bit

    __WATCOM_OS216__    Watcom, OS/2 1.x            16-bit

    __WATCOM_OS232__    Watcom, OS/2 2.x+           32-bit

    __BORLAND_DOS16__   Borland, DOS                16-bit

    __BORLAND_WIN16__   Borland, Windows 3.x        16-bit

    __BORLAND_OS232__   Borland, OS/2 2.x+          32-bit

    __MSOFT_DOS16__     Microsoft, DOS              16-bit

    __MSOFT_OS216__     Microsoft, OS/2             16-bit

    __MSOFT_WIN16__     Microsoft, Windows 3.x      16-bit

    __MSOFT_WIN32__     Microsoft, Win32            32-bit
    
    __MSOFT_WIN64__     Microsoft, Win64            64-bit


Other identifiers we use/create:

    __WATCOMC__         Defined if compiler is WATCOM
    __BORLANDC__        Defined if compiler is Borland
    __MSC__             Defined if compiler is Microsoft

    __MSDOS__           Defined if target OS is DOS
    __WINDOWS__         Defined if target OS is Windows
    __OS2__             Defined if target OS is OS/2

    __DOS16__           Defined if target OS is 16-bit DOS
    __DOS32__           Defined if target OS is 32-bit DOS

    __OS216__           Defined if target OS is 16-bit OS/2 (1.x)
    __OS232__           Defined if target OS is 32-bit OS/2 (2.x+)

    __WIN16__           Defined if target OS is 16-bit Windows (Win3.x)
    __WIN32__           Defined if target OS is 32-bit Windows (Win95/NT/etc.)
    __WIN64__           Defined if target OS is 64-bit Windows

    __16BIT__           Defined if word size is 16 bits
    __32BIT__           Defined if word size is 32 bits
    __64BIT__           Defined if word size is 64 bits

*****************************************************************************/

/****************************************************************************/
/*                                                                          */
/*  Identify compiler and environment                                       */
/*                                                                          */
/****************************************************************************/

/**********/
/* Watcom */
/**********/

#if defined(__WATCOMC__)

    #if !defined(NCS_WARN_OF_EMPTY_FUNCTIONS)
        #pragma warning 656 5;
        #pragma warning 657 5;
    #endif

    /* Watcom doesn't have the POSIX/ANSI version of these, so 
       we will just work around the lack with macros: 
       */

    #define _getcwd getcwd
    #define _chdir chdir
    #define _open  open 
    #define _sopen sopen 
    #define _close close
    #define _setmode setmode
    #define _read  read 
    #define _write write
    #define _tell tell
    #define _rmdir rmdir
    #define _mkdir mkdir
    #define _filelength filelength
    #define _tzset tzset
    #define _getpid getpid
    #define _ultoa ultoa

    #if defined(__DOS__)

        #if defined(M_I86)

            #define __WATCOM_DOS16__
            #define __16BIT__
            #define __DOS16__

        #elif defined(M_I386)

            #define __WATCOM_DOS32__
            #define __32BIT__
            #define __DOS32__

        #endif

        #if !defined(__MSDOS__)
            #define __MSDOS__
        #endif

    #elif defined(__OS2__)

        #if defined(M_I86)

            #define __WATCOM_OS216__
            #define __16BIT__
            #define __OS216__

        #elif defined(M_I386)

            #define __WATCOM_OS232__
            #define __32BIT__
            #define __OS232__

        #endif

    #elif defined(__NT__)

        #define __WATCOM_WIN32__

        #if !defined(__WINDOWS__)
            #define __WINDOWS__
        #endif

        #if defined(__FLAT__)
            #define __32BIT__
        #endif

        #define __WIN32__

    #elif defined(__WINDOWS__)

        #define __WATCOM_WIN16__
        #define __16BIT__
        #define __WIN16__

    #else

        #error Unknown Watcom environment!

    #endif
#endif

/***********/
/* Borland */
/***********/

#if defined(__BORLANDC__)

    #if defined(_Windows)

        #define __16BIT__
        #define __WINDOWS__
        #define __BORLAND_WIN16__
        #define __WIN16__
        #if defined(__MSDOS__)
            #undef __MSDOS__
        #endif

    #elif defined(__OS2__)

        #define __BORLAND_OS232__    
        #define __32BIT__
        #define __OS232__

    #elif defined(__MSDOS__)

        #define __16BIT__
        #define __BORLAND_DOS16__    
        #define __DOS16__

    #else

        #error Unknown Borland environment!

    #endif
#endif

/*************/
/* Microsoft */
/*************/

#if defined(_MSC_VER) && (_MSC_VER >= 600)

    #define __MSC__

    #if (_MSC_VER >= 800)
        #pragma warning( disable : 4505 ) /* Utterly useless warning */  
        #pragma warning( disable : 4705 ) /* Thinks constructors have no effect */
        #pragma warning( disable : 4127 ) /* Doesn't like 'while (TRUE)' */  
        #pragma warning( disable : 4201 ) /* Nameless struct/union */  
        #pragma warning( disable : 4214 ) /* Doesn't like unsigned bitfields */  
    #endif

    #if defined(_WIN64)

        #define __64BIT__
        #define __MSOFT_WIN64__
        #define __WINDOWS__
        #define __WIN64__

    #elif defined(_WIN32)

        #define __32BIT__
        #define __MSOFT_WIN32__
        #define __WINDOWS__
        #define __WIN32__

    #elif defined(_WINDOWS)

        #define __16BIT__
        #define __MSOFT_WIN16__
        #define __WINDOWS__
        #define __WIN16__

    #elif defined(MSDOS)

        #define __16BIT__
        #define __MSOFT_DOS16__
        #define __DOS16__
        #if !defined(__MSDOS__)
            #define __MSDOS__
        #endif

    #elif defined(_MT)

        #define __16BIT__
        #if !defined(__OS2__)
            #define __OS2__
        #endif
        #define __MSOFT_OS216__
        #define __OS216__

    #else

        #error Unknown Microsoft environment!

    #endif
#endif

/************************************/
/* Make sure we have an environment */
/************************************/

#if !defined(__MSC__) && !defined(__BORLANDC__) && !defined(__WATCOMC__)
    #error Unknown compiler!
#endif

#if !defined(__MSDOS__) && !defined(__OS2__) && !defined(__WINDOWS__)
    #error Unknown operating system/environment!
#endif

#if !defined(__16BIT__) && !defined(__32BIT__) && !defined(__64BIT__)
    #error Unknown integer word size!
#endif

#endif  /* _NCS_CENV_H */

Comments, corrections, questions? Email me here:

Valid HTML 4.01 Transitional