Determine OS and compiler

S

Sisyphus

Hi,

Is there anything in the ANSI C standard covering the means by which I
can determine the operating system and compiler being used (at the
preprocess stage) ?

If so, then how might I determine (at the preprocess stage) whether the
operating system upon which the app is being built is Win32 or not, and
whether the compiler being used is gcc or not ?

If, on the other hand, this question is OT here, can someone provide me
with the name of a newsgroup where I could (legitimately) ask about
achieving such a determination.

Cheers,
Rob
 
E

E. Robert Tisdale

Sisyphus said:
Is there anything in the ANSI C standard covering the means
by which I can determine the operating system and compiler
being used (at the preprocess stage)?
No.

If so, then how might I determine (at the preprocess stage)
whether the operating system upon which the application is being built
is Win32 or not, and whether the compiler being used is gcc or not?

If, on the other hand, this question is OT here,
can someone provide me with the name of a newsgroup
where I could (legitimately) ask about achieving such a determination.

gnu.gcc.help

You can get gcc to print out
all of the predefined C preprocessor macros for you:
> cat empty.c
> gcc -std=c99 -E -dM empty.c
#define __HAVE_BUILTIN_SETJMP__ 1
#define __unix__ 1
#define __i386__ 1
#define __SIZE_TYPE__ unsigned int
#define __ELF__ 1
#define __GNUC_PATCHLEVEL__ 0
#define __linux 1
#define __unix 1
#define __linux__ 1
#define __STDC_VERSION__ 199901L
#define __USER_LABEL_PREFIX__
#define __STDC_HOSTED__ 1
#define __WCHAR_TYPE__ long int
#define __gnu_linux__ 1
#define __WINT_TYPE__ unsigned int
#define __GNUC__ 3
#define __STRICT_ANSI__ 1
#define __GXX_ABI_VERSION 102
#define __GNUC_MINOR__ 2
#define __STDC__ 1
#define __PTRDIFF_TYPE__ int
#define __tune_i386__ 1
#define __REGISTER_PREFIX__
#define __NO_INLINE__ 1
#define __i386 1
#define __VERSION__ "3.2 20020903 (Red Hat Linux 8.0 3.2-7)"
 
D

Dave Vandervies

Hi,

Is there anything in the ANSI C standard covering the means by which I
can determine the operating system and compiler being used (at the
preprocess stage) ?

Not specified in the standard, but there is a mechanism that the standard
allows and that most compilers use:
If so, then how might I determine (at the preprocess stage) whether the
operating system upon which the app is being built is Win32 or not, and
whether the compiler being used is gcc or not ?

Most compilers will define a preprocessor macro that identifies
the compiler and operating system. (There's nothing preventing an
implementation for the DeathStation from defining all such macros and
then blowing up your terminal when it sees code that uses extensions
specific to some other compiler, but with implementations that attempt
to provide a nonnegative QoI this is usually not a problem.)

The standard allows implementations to use most symbols beginning with "_"
(and all symbols beginning with "__") for any use the implementors can
come up with, so these macros typically look something like "__unix__"
or "_WIN32".

If, on the other hand, this question is OT here, can someone provide me
with the name of a newsgroup where I could (legitimately) ask about
achieving such a determination.

Given the information above, you'd be better off checking your compiler's
documentation to find what macro(s) it defines. If you can't find it
there, a newsgroup for your OS or compiler should be able to tell you.

Potentially helpful newsgroups, from the CLC welcome message:
--------
Languages and Programming

The C++ Programming Language
x86 assembly language programming
Non-language specific programming
Issues of computer graphics

Compilers

The gcc free C compiler
x86 version of the free gcc C
compiler

Operating Systems

DOS, BIOS, Memory Models,
interrupts, screen handling,
hardware
MS/Windows: Mice, DLLs, hardware
MS 32-bit API
OS/2 Programming
Macintosh Programming
General Unix: processes, pipes,
POSIX, curses, sockets
news:comp.unix.[vendor] Various Unix vendors
Linux application programming
 
C

Christian Bau

Sisyphus said:
Hi,

Is there anything in the ANSI C standard covering the means by which I
can determine the operating system and compiler being used (at the
preprocess stage) ?

If so, then how might I determine (at the preprocess stage) whether the
operating system upon which the app is being built is Win32 or not, and
whether the compiler being used is gcc or not ?

Why would you want to know whether the compiler runs on Windows or not?
For example, the Metrowerks CodeWarrior compilers will produce identical
code, whether you run them on a Macintosh or on Windows. What you want
to know is what platform you are compiling for.
If, on the other hand, this question is OT here, can someone provide me
with the name of a newsgroup where I could (legitimately) ask about
achieving such a determination.

Read the documentation of various compilers. Most compilers will have a
few predefined #define's that let you determine which compiler you are
using. Of course you cannot draw any conclusions for compilers that you
haven't seen yet, you can only hope that any compiler that is not a gcc
compiler won't have __GCC_COMPILER__ predefined (or whatever gcc uses to
identify itself).
 

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

Forum statistics

Threads
474,137
Messages
2,570,797
Members
47,342
Latest member
eixataze

Latest Threads

Top