multiple macro defns

S

sinbad

how to make a macro definition not to be redefined by some other
instance.
for example there are two instances of macro_1,

#define macro_1 20 in some file, and
#define macro_1 30 in some other file which i probably don't know,
more precisely i don't care
even if such defn exists; The thing is i always want macro_1 to be
20.

the simple thing i could've done is remove all other defn's except the
one i wanted. But
that will not work, because #define macro_1 20 will be visible only if
my feature is enabled.

so, i could have done

some_file.c
#ifdef my_feat
#define macro_1 20
#endif

some_other_file.c

#ifndef my_feat
#define macro_1 30
#endif

But this requires me to change some_other_file.c, which i don't want.
Is there any way
of doing this.
 
G

Guest

how to make a macro definition not to be redefined by some other
instance. for example there are two instances of macro_1,

#define macro_1 20 in some file, and
#define macro_1 30 in some other file which i probably don't know,
more precisely i don't care
even if such defn exists; The thing is i always want macro_1 to be
20.

the simple thing i could've done is remove all other defn's except the
one i wanted. But
that will not work, because #define macro_1 20 will be visible only if
my feature is enabled.

so, i could have done

some_file.c
#ifdef my_feat
#define macro_1 20
#endif

some_other_file.c

#ifndef my_feat
#define macro_1 30
#endif

But this requires me to change some_other_file.c, which i don't want.
Is there any way
of doing this.

does #undef help?
 
Joined
Mar 25, 2009
Messages
1
Reaction score
0
even though my_feat is enabled .....
the sln gives compilation error as "undeclared identifier macro_1 in some_other_file.c"
because the macro_1 scope was limited to some_file.c and some_other_file.c doen't know about macro_1 defined in some_file.c unless it is declared in common header file.
 
J

James Kuyper

sinbad said:
how to make a macro definition not to be redefined by some other
instance.
for example there are two instances of macro_1,

#define macro_1 20 in some file, and
#define macro_1 30 in some other file which i probably don't know,
more precisely i don't care
even if such defn exists; The thing is i always want macro_1 to be
20.

Are you sure that it's wise not to care about that possibility? If
someone else wrote code that sets the macro to a different value,
there's a good chance that there is some good reason why it needs to
have that value, even if you don't care about it directly. I think that
what you really need is to choose a macro naming convention that
minimizes the chance of such conflicts. A special prefix on all macros
connected with a given package is a common approach to meeting this
goal. This is also a good idea for any object or function names with
external linkage.
 
G

Guest

No it doesn't because i dont' know the order in which the pre
processor takes up the macro defns'

my problem is I don't understand what you want.
Some points

1. it is invalid to have more than one definition

#define BAD "you can't"
#define BAD "do this"

2. the preprocessor processes macro definitions in the order in which
it
encounters them. That is, in the lexical order they appear in the
translation unit (file + includes). (delta #ifdef etc.)

[this may or may not help you resolve what "take up" means]

3. you can undefine macros

so you could do

#ifdef macro_1
# undef macro_1
#endif

#define macro_1 20

but if you want to change a macro definition in another file
then I can't see any way to do it apart from changeing the file.
 
K

Kenny McCormack

pete said:
I don't understand your problem.
I have a program with
#define macro_1 20
in one c file, and with
#define macro_1 30
in another c file, and the program is fine.

I don't think you have to work that hard. Consider (and forget about
the CLC niceties for this one):

main()
{
#define foo 10
printf("foo = %d\n",foo);
#undef foo
#define foo 20
printf("foo = %d\n",foo);
}

Without the #undef, it compiles and runs fine - giving the expected
output. However, the compiler does warn about foo being redefined.
Adding the #under makes the warning go away.

Note, incidentally, that you shouldn't have to do any fancy:

#ifdef symbol
#undef symbol
#endif

You can just go ahead and #undef it.
 
K

Keith Thompson

3. you can undefine macros

so you could do

#ifdef macro_1
# undef macro_1
#endif

#define macro_1 20
[...]

Note that the #ifdef is unnecessary; #undef does nothing when applied
to a name that's not currently defined as a macro:

#undef macro_1
#define macro_1 20

Of course, this all assumes that it's reasonable to ignore any
existing definition; that's likely to be an unsafe assumption.
See James Kuyper's followup.
 
K

Keith Thompson

pete said:
I don't understand your problem.
I have a program with
#define macro_1 20
in one c file, and with
#define macro_1 30
in another c file, and the program is fine.
[...]

That's fine if the two #define's are in separate translation units.
They could be in different files but the same translation unit if both
are #included by another file, or if one #includes the other, directly
or indirectly.
 
S

sinbad

Keith said:
pete said:
I don't understand your problem.
I have a program with
    #define macro_1 20
in one c file, and with
    #define macro_1 30
in another c file, and the program is fine. [...]

That's fine if the two #define's are in separate translation units.
They could be in different files but the same translation unit if both
are #included by another file, or if one #includes the other, directly
or indirectly.

I wonder what OP meant.

All,

I want the macro_1 to be defined as 20 if my_feat is defined.
After reading the discussions, i figured out there is no good way of
doing
it without changing all other instances of defn's. So the following is
the
only correct solution for my problem.

some_file.c

#ifdef my_feat
#define macro_1 20
#endif

some_other_file.c

#ifndef my_feat
#define macro_1 30
#endif

by doing above, i will always be sure that if my_feat is defined.
macro_1 can only be defined to 20.

Thanks everyone
Sinbad
 
K

Keith Thompson

sinbad said:
I want the macro_1 to be defined as 20 if my_feat is defined.
After reading the discussions, i figured out there is no good way of
doing
it without changing all other instances of defn's. So the following is
the
only correct solution for my problem.

some_file.c

#ifdef my_feat
#define macro_1 20
#endif

some_other_file.c

#ifndef my_feat
#define macro_1 30
#endif

by doing above, i will always be sure that if my_feat is defined.
macro_1 can only be defined to 20.

Why not just do the following:

#ifdef MY_FEAT
#define MACRO_1 20
#else
#define MACRO_1 30
#endif

in a single header file, which you #include in both some_file.c and
some_other_file.c? (I've taken the liberty of using all-caps for the
macro names.)
 

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
473,777
Messages
2,569,604
Members
45,224
Latest member
BettieToom

Latest Threads

Top