By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,949 Members | 1,468 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,949 IT Pros & Developers. It's quick & easy.

How to identify the platform & compiler while compiling?

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

Jan 27 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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.

Jan 27 '07 #2

P: n/a
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)
Jan 27 '07 #3

P: n/a
On 27 Jan 2007 01:52:45 -0800, in comp.lang.c , "aki27"
<ak******@gmail.comwrote:
>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
Jan 27 '07 #4

P: n/a
On Jan 27, 5:52 pm, "aki27" <akila...@gmail.comwrote:
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/

Jan 27 '07 #5

P: n/a

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

Jan 28 '07 #6

P: n/a
In article <11*********************@v45g2000cwv.googlegroups. com>
aki27 <ak******@gmail.comwrote:
>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 (4039.22'N, 11150.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.
Jan 28 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.