For Programmers: Free Programming Magazines  


Home > Archive > C > January 2007 > How to identify the platform & compiler while compiling?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How to identify the platform & compiler while compiling?
aki27

2007-01-27, 3:56 am

Is there a way to find the platform and compiler name during compile
time so that conditional compilation can be performed in C? thanks.

santosh

2007-01-27, 7:56 am

aki27 wrote:
> Is there a way to find the platform and compiler name during compile
> time so that conditional compilation can be performed in C? thanks.


There is no standard way to do what you're asking. Most compilers
define preprocessor constants, that identify the compiler, it's version
etc. Identifying the platform is bit more complex.

Why don't you look at tools like autoconf and autoheader that do this,
and much more, automatically for you? They're also available for many
platforms.

<http://sources.redhat.com/autobook/>

For further questions on this topic, consider a group like
comp.programming.

CBFalconer

2007-01-27, 7:56 am

aki27 wrote:
>
> Is there a way to find the platform and compiler name during compile
> time so that conditional compilation can be performed in C? thanks.


The pre-existance of __STDC__ and possibly __STDC_VERSION__ will
tell you whether you have a system handling C90 or C99. This is
enough to handle all portable code. For other non-portable things
read your systems documentation. Those non-portable things are
off-topic on comp.lang.c.

--
Some informative links:
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/> (taming google)
<http://members.fortunecity.com/nnqweb/> (newusers)


Mark McIntyre

2007-01-27, 6:56 pm

On 27 Jan 2007 01:52:45 -0800, in comp.lang.c , "aki27"
<akilan27@gmail.com> wrote:

>Is there a way to find the platform and compiler name during compile
>time so that conditional compilation can be performed in C? thanks.


Most compilers have a macro of some sort that assists with this. Look
at the headers of many portable gnu projects and you'll see 'em in
action.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Chih-Chung Chang

2007-01-27, 6:57 pm

On Jan 27, 5:52 pm, "aki27" <akila...@gmail.com> wrote:
> Is there a way to find the platform and compiler name during compile
> time so that conditional compilation can be performed in C? thanks.


There is a large list of predefined C/C++ compiler macros in http://
predef.sourceforge.net/

aki27

2007-01-28, 3:57 am


Thanks to all. My special thanks to Chih-Chung Chang. I found his
links usefull.

Chris Torek

2007-01-28, 3:57 am

In article <1169891565.245849.11090@v45g2000cwv.googlegroups.com>
aki27 <akilan27@gmail.com> wrote:
>Is there a way to find the platform and compiler name during compile
>time so that conditional compilation can be performed in C? thanks.


It is often (but not always) possible to do so. However, it is
often -- perhaps even usually -- unwise to do so.

In particular, people seem to like to do things like this:

#ifdef WIN32
some bizarre special-case code
#endif
#ifdef LINUX
some code
#endif
#ifdef HPUX
some code essentially identical to linux version
#endif
#ifdef BSD
third variant of code again essentially identical
#endif
#ifdef SOLARIS
fourth variant, with some extra goop for Solaris threads
#endif

What one *should* do, instead, is:

% cat win.c
... special windows-specific code ...
(no ifdefs at all here)

% cat everythingelse.c
... generic Unixy version of code ...
#ifdef USE_POSIX_THREADS
extra goop for POSIX threads
#endif
#ifdef USE_SOLARIS_THREADS
extra goop for Solaris threads
#endif
... more generic code ...

In other words, take the special (in this case, Windows-specific)
code and put it in a special file used only on the special system.
Take the common code and put it in a common file, which is used on
the common systems. If "#ifdef"s are needed -- which should be
made as rare as possible by separating out the "special" code --
make them conditional on the *feature* being exploited, not the
*system*. Here, although "Solaris threads" is the conditional, it
is the *feature*, not the system, that is being tested -- and note
that "Solaris threads" are not even present in sufficiently ancient
versions of Solaris. The code still works on those, even though
the platform and compiler are "Solaris"; you simply compile without
turning on "USE_SOLARIS_THREADS".

(You can of course set the default configuration based on the
platform and compiler. Just give the software engineer a clear
and obvious place to *change* those settings. If the default
for FooBlick systems turns out to be incorrect for the FooBlick
42, the place to adjust that will then be clear and obvious.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com