need help on compiler-specific defines 
Author Message
 need help on compiler-specific defines

I've written an application in C that I want to compile slightly
differently on different platforms.

For example, when compiled under Watcom C++, I want it to
execute one particular line of code; if it's compiled under Turbo C,
I want it to execute a different line instead; under Unix
(gcc) I want it to execute still another line.  I know
that this can be done using #ifdef, but I can't figure out
exactly what is defined for the specific compilers. I've
looked through the manuals, help files, C faq and still
can't find the answer.

The code should look something like this:

#ifdef WATCOM
        32bit_subroutine();
#ifdef TURBOC
        16bit_subroutine();
#ifdef UNIX
        unix_subroutine();
#endif

Also, I've seen defines look this way:
#ifdef (__TURBOC__)

Why two underscore characters and the parenthesis before and
after the TURBOC ?

Many thanks for any help.

--Tim



Wed, 05 Jan 2000 03:00:00 GMT  
 need help on compiler-specific defines



Quote:
>I've written an application in C that I want to compile slightly
>differently on different platforms.

>For example, when compiled under Watcom C++, I want it to
>execute one particular line of code; if it's compiled under Turbo C,
>I want it to execute a different line instead; under Unix
>(gcc) I want it to execute still another line.  I know
>that this can be done using #ifdef, but I can't figure out
>exactly what is defined for the specific compilers. I've
>looked through the manuals, help files, C faq and still
>can't find the answer.

There are no standards for this, you'll have to look it up in the
documentation for the specific compiler. It should be there somewhere.
Sometimes there is a compiler options which outputs the predefined
macros the compiler will use.

Quote:
>The code should look something like this:

>#ifdef WATCOM
>        32bit_subroutine();

#endif

Quote:
>#ifdef TURBOC
>        16bit_subroutine();

#endif

Quote:
>#ifdef UNIX
>        unix_subroutine();
>#endif

Do you really need to do it like that? It is usually better to select on
a particular feature rather than simply per compiler.

Quote:
>Also, I've seen defines look this way:
>#ifdef (__TURBOC__)

>Why two underscore characters and the parenthesis before and
>after the TURBOC ?

This isn't valid as written. You may have seen things of the form:

#ifdef __TURBOC__

and

#if defined __TURBOC__

and

#if defined(__TURBOC__)

These all do the same thing. The difference is that #if takes an expression
and defined is a preprocessor operator that tests whether its operand is a
defined macro. That allows you to build more complex tests e.g.

#if defined(__TURBOC__) || defined(__GNC__)

These sort of macro names tend to start (indeed should start) with a
double underscore or an underscore followed by an uppercase letter since
the C language reserves those for use by the implementation. Therefore
by using that form the compiler won't interfere with the working of a
correctly written program.

--
-----------------------------------------


-----------------------------------------



Wed, 05 Jan 2000 03:00:00 GMT  
 need help on compiler-specific defines

Quote:

> I've written an application in C that I want to compile slightly
> differently on different platforms.

> For example, when compiled under Watcom C++, I want it to
> execute one particular line of code; if it's compiled under Turbo C,
> I want it to execute a different line instead; under Unix
> (gcc) I want it to execute still another line.  I know
> that this can be done using #ifdef, but I can't figure out
> exactly what is defined for the specific compilers.

Hi Timothy Barmann,

The defines for specific compilers are compiler specific :-)
As funny as it sounds, it's really true: some compilers offer a
predefined macro that allows you to (sortof) recognize the compiler at
compile time by using "#ifdef". But the problems are:
  - some compilers *do not* supply such are macro
  - the name of the macro is very different for each compiler
    (absolutely no standardisation here) and is mostly hidden deep
    inside the compiler manuals

Using compiler supplied macros is the easier way, but the better
solution is to use your own macro and define the macro when calling the
compiler. This is not as complicated as it sounds, because you can use
makefiles or compiler project settings for this purpose. You have your
set of sources witht he "#ifdef"s and create a different makefile or
project file for each target system.

Quote:
> Also, I've seen defines look this way:
> #ifdef (__TURBOC__)

> Why two underscore characters and the parenthesis before and
> after the TURBOC ?

The underscores derive from the imagination of Borland compiler
developers (who invented the name) and the paranthese are a bit dubious
to me too. For a simple macro name they should not be required.

Stephan
(initiator of the campaign against grumpiness in c.l.c)



Fri, 07 Jan 2000 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Question about defining compiler specific things

2. Need to make compiler search directories in specific order

3. Need to make compiler search directories in specific order

4. Help needed to move some specific work into a more common function

5. Need help with Arrays: Specific program question

6. Need Help locating C code in a specific memory location

7. HELP , HELP , HELP - I need a compiler

8. Help with compiler error: fatal error LNK1169: one or more multiply defined symbols found

9. Ansi C -- no system-specific pre-#defines

10. Need help with #define

11. Need help with #define [2]

12. Help needed with #define's

 

 
Powered by phpBB® Forum Software