#ifdef

H

Hash

I have a small doubt,

#ifdef _BSD

When we use #ifdef preprocessor directive, the parameter which we pass
(in this case _BSD) is it defined by the user or is it defined somewhere
else?

If the definition is somewhere where can i find the defnitions ?
 
N

Neil Kurzman

Hash said:
I have a small doubt,

#ifdef _BSD

When we use #ifdef preprocessor directive, the parameter which we pass
(in this case _BSD) is it defined by the user or is it defined somewhere
else?

If the definition is somewhere where can i find the defnitions ?

Many places:
header files, C files, the compiler, the command line.
 
S

SM Ryan

# #ifdef _BSD
#
# When we use #ifdef preprocessor directive, the parameter which we pass
# (in this case _BSD) is it defined by the user or is it defined somewhere
# else?

There's a plethora of defines like this. They are defined by the compiler
so that a source code can adapt itself to the compiler and operating system.

# If the definition is somewhere where can i find the defnitions ?

Generally, any _<compiler-name> or _<operating-system-name> are defined or
intentionally undefined by the compilers. The compiler documentation should
explain which ones it defines and to what values.
 
J

John Bode

Hash said:
I have a small doubt,

#ifdef _BSD

When we use #ifdef preprocessor directive, the parameter which we pass
(in this case _BSD) is it defined by the user or is it defined somewhere
else?

If the definition is somewhere where can i find the defnitions ?

It can be defined any number of places: in a header file, on the
command line, in the source file, etc.

In my experience, macros which specify a platform are usually
specified on the command line, for example

gcc -D_BSD -c foo.c
 
B

Benjamin Ketcham

Hash said:
I have a small doubt,

#ifdef _BSD

When we use #ifdef preprocessor directive, the parameter which we pass
(in this case _BSD) is it defined by the user or is it defined somewhere
else?

If the definition is somewhere where can i find the defnitions ?

It may be defined by the user, or somewhere in a header file,
or in other project files such as "Makefile". If the documentation
is any good, it should tell you what symbols are defined, and where.
Often, especially under Unix and its ilk, compiling software
is a two step process: the first step, "configure", queries your
system and creates one or more header files containing bunches of
macro definitions representing what kind of system it is; the
second step, "make", then fires off the actual C compiler, which
makes use of the definitions to customize the compilation to your
environment. The definitions may include "big-picture" attributes
such as is the system more like BSD or more like SYSV, etc., and
much smaller details like what functions are available, what is
the CPU endianness, etc..

I would expect the symbol in question, "_BSD", to be defined
automatically for you, if it is needed, either in a header file
that is part of the software package, or in a system header file
(often located under /usr/include on Unix), or as part of the
"configure" process.

Just a guess, but you may be encountering difficulties related to
this symbol because you have taken a small snippet of code, or a
single C file, out of a larger context such as the OS source code
or the source code for a large application or library, and you
are trying to compile this snippet as a standalone program, or
you are trying to integrate it with a project of your own.
In general, this type of "cannibalism" can have subtle gotchas
as well as obvious ones like undefined symbols, so be careful.
I wouldn't say don't do it, though, because there's much to learn
through trying.

Many compilers have options to verbosely print out directories
where they are searching for header files, and to display the
C code after preprocessing but before compiling. These can help
in tracking down issues with preprocessor symbols.

--Benjamin
 
D

Dave Thompson

Hash said:
I have a small doubt,

#ifdef _BSD

When we use #ifdef preprocessor directive, the parameter which we pass
(in this case _BSD) is it defined by the user or is it defined somewhere
else?

If the definition is somewhere where can i find the defnitions ?

It may be defined by the user, or somewhere in a header file,
or in other project files such as "Makefile". If the documentation
is any good, it should tell you what symbols are defined, and where.
Often, especially under Unix and its ilk, compiling software
is a two step process: the first step, "configure", queries your
system and creates one or more header files containing bunches of
macro definitions representing what kind of system it is; the
second step, "make", then [compiles using these definitions....]
I would expect the symbol in question, "_BSD", to be defined
automatically for you, if it is needed, either in a header file
that is part of the software package, or in a system header file
(often located under /usr/include on Unix), or as part of the
"configure" process.
Agree so far, but especially for macros beginning with underscore
there is a third possibility -- they may be "builtin" to the compiler,
which may mean actually hardcoded or simply set somewhere in the
compiler installation. For the particularly flexible and common case
of gcc, this is in a file (usually?) in a place something like
/usr/lib/gcc-lib/$arch/$version/specs . Either way they should be
described in the compiler's documentation.

Many compilers have options to verbosely print out directories
where they are searching for header files, and to display the
C code after preprocessing but before compiling. These can help
in tracking down issues with preprocessor symbols.
This won't help for builtins however. gcc does have an option -dM to
dump out macros at the end of compilation -- which if you compile a
(perhaps dummy) program that doesn't (significantly) change the
builtins and system settings, will show you what you've got.

- David.Thompson1 at worldnet.att.net
 

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
473,774
Messages
2,569,596
Members
45,130
Latest member
MitchellTe
Top