Defining a global MACRO

T

Tagore

hi,
I was working on my first big project in C. Now my project can be
used for 2 different purposes so some portions of my project needed
precondition compiling depending on my purpose. so I use
#ifdef MACRO_NAME
.....code to purpose-1
#elif
.....code for purpose-2
#endif

but the problem is that this precondition compiling involve several
files. so I have to define this macro in all files. Is not there a
better way to define a global Macro.

regards,
 
P

Peter Nilsson

Tagore said:
hi,
   I was working on my first big project in C. Now my project
can be used for 2 different purposes so some portions of my
project needed precondition compiling depending on my purpose.
so I use
#ifdef MACRO_NAME
....code to purpose-1
#elif
....code for purpose-2
#endif

but the problem is that this precondition compiling involve
several files. so I have to define this macro in all files.
Is not there a better way to define a global Macro.

Use an include file.
 
M

Morris Keesan

hi,
I was working on my first big project in C. Now my project can be
used for 2 different purposes so some portions of my project needed
precondition compiling depending on my purpose. so I use
#ifdef MACRO_NAME
....code to purpose-1
#elif
....code for purpose-2
#endif

but the problem is that this precondition compiling involve several
files. so I have to define this macro in all files. Is not there a
better way to define a global Macro.

Many compilers allow you to specify preprocessor defines on their command
lines
e.g.
gcc -DMACRO_NAME -c foo.c

Your compiler may allow this.

If you're working on a big project, you probably want to have something
automatically
controlling the building of the project, such as make, Eclipse, or ant.
These
tools can make it easier to specify what compiler options will be used for
all C source
files.
 
N

Nick Keighley

hi,
   I was working on my first big project in C. Now my project can be
used for 2 different purposes so some portions of my project needed
precondition compiling depending on my purpose. so I use
#ifdef MACRO_NAME
....code to purpose-1
#elif
....code for purpose-2
#endif

but the problem is that this precondition compiling involve several
files. so I have to define this macro in all files. Is not there a
better way to define a global Macro.

what the other posters said is all good stuff. But can you avoid this
altogether? Can you separate the purpose-1 stuff from the purpose-2
stuff? Can you put them in different files? Then you build two
different
applications

purpose-1-app
common-library

purpose-2-app
common-library

No nasty ifdefs anymore!
 
E

Eric Sosman

Tagore said:
hi,
I was working on my first big project in C. Now my project can be
used for 2 different purposes so some portions of my project needed
precondition compiling depending on my purpose. so I use
#ifdef MACRO_NAME
....code to purpose-1
#elif
#else

....code for purpose-2
#endif

but the problem is that this precondition compiling involve several
files. so I have to define this macro in all files. Is not there a
better way to define a global Macro.

#include "file_that_might_or_might_not_define_MACRO_NAME.h"
 
A

adrian.d.arpi.s

El jueves, 8 de octubre de 2009 07:27:09 UTC-5, Eric Sosman escribió:
#include "file_that_might_or_might_not_define_MACRO_NAME.h"

I have same problem, don't you find a better solution for this?
 
K

Keith Thompson

El jueves, 8 de octubre de 2009 07:27:09 UTC-5, Eric Sosman escribió:

I have same problem, don't you find a better solution for this?

You're replying to an article that was posted nearl 4 years ago.

Many compilers let you specify macro values on the command line. gcc is
typical:

gcc -DMACRO_NAME foo.c

You can add this to your Makefile, build script, or whatever you use to
invoke the compiler.

I don't know that that's necessarily a *better* solution.
 

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,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top