How to identify the platform & compiler while compiling?

A

aki27

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

santosh

aki27 said:
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.
 
C

CBFalconer

aki27 said:
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)
 
M

Mark McIntyre

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
 
C

Chih-Chung Chang

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/
 
C

Chris Torek

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.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top