DSPRelated.com
Forums

problem with conditional compilation

Started by Raj October 20, 2003
hi all,
I have a set of 40-50 global variables. I put all of
them in one .h file say define.h. This define.h is
included in some 6 files. CCS says that the variables
are already defined (this is due 2 multiple
inclusions) and gives error. I tried 2 work around the
problem using the K & R approach:
#ifndef _DEFINE_H
#define _DEFINE_H
/*contents of define.h*/
#endif
But this also doesn't work
what might be the possible reason?
Rajesh ________________________________________________________________________
Yahoo! India Matrimony: Find your partner online.
Go to http://yahoo.shaadi.com




--- Raj <> wrote:
> I have a set of 40-50 global variables. I put all
> of
> them in one .h file say define.h.

!!! error !!!

header files *.h are not meant for defining
variables... they are meant for declaring variables,
so the compiler knows the type of the variable and
that the variable is defined somewhere else...

so - my advice is - define all your global variables
in a *.c file and then declare them in the *.h file...
then you can include that *.h file in any *.c file you
want - using compiler directives #ifndef and #define
the way you described further on is obviously a good
manner...

> This define.h is
> included in some 6 files. CCS says that the
> variables
> are already defined (this is due 2 multiple
> inclusions) and gives error. I tried 2 work around
> the
> problem using the K & R approach:
> #ifndef _DEFINE_H
> #define _DEFINE_H
> /*contents of define.h*/
> #endif
> But this also doesn't work
> what might be the possible reason?

seems to me like "C language" problem rather than C6x
problem ;-)

good luck...

Wojciech Rewers

__________________________________


Rajesh-

> I have a set of 40-50 global variables. I put all of
> them in one .h file say define.h. This define.h is
> included in some 6 files. CCS says that the variables
> are already defined (this is due 2 multiple
> inclusions) and gives error. I tried 2 work around the
> problem using the K & R approach:
> #ifndef _DEFINE_H
> #define _DEFINE_H
> /*contents of define.h*/
> #endif
> But this also doesn't work
> what might be the possible reason?

I know why it does not work. But please I ask you to try to explain the problem
better; for example, what is your error message? Simply saying "this does not
work,
please help me" is not showing everyone a good effort and makes it harder for
people
to help you.

At least half of engineering is just communicating and asking the right
questions :-)

-Jeff




hi ,
I want all the global variables(say a b c d) at one place say some define.h and
the in all the project files I want to include this define.h. For every such
inclusion excepting the file that is linked first, I get the error message
variable a b c d are already declared(if i include in 5 files I get 16 errors).
To come around this problem i used K &R approach
#ifndef _DEFINE_H
#define _DEFINE_H
/* contents of define.h*/
#endif.
Even then I get the same error messages.
Somebody had suggested u make it as my_define.h that also doesn't work.
somebody said u declare all the variables in .c file and make extern
declarations in .h file and include that .h file in all the files.
This approach also didn't work
So I am trying to figure out what is the problem.
I guess this explains my problem better
Rajesh

Jeff Brower <> wrote:
Rajesh-

> I have a set of 40-50 global variables. I put all of
> them in one .h file say define.h. This define.h is
> included in some 6 files. CCS says that the variables
> are already defined (this is due 2 multiple
> inclusions) and gives error. I tried 2 work around the
> problem using the K & R approach:
> #ifndef _DEFINE_H
> #define _DEFINE_H
> /*contents of define.h*/
> #endif
> But this also doesn't work
> what might be the possible reason?

I know why it does not work. But please I ask you to try to explain the problem
better; for example, what is your error message? Simply saying "this does not
work,
please help me" is not showing everyone a good effort and makes it harder for
people
to help you.

At least half of engineering is just communicating and asking the right
questions :-)

-Jeff

_____________________________________
Note: If you do a simple "reply" with your email client, only the author of this
message will receive your answer. You need to do a "reply all" if you want your
answer to be distributed to the entire group.

_____________________________________
About this discussion group:

To Join: Send an email to

To Post: Send an email to

To Leave: Send an email to

Archives: http://www.yahoogroups.com/group/c6x

Other Groups: http://www.dsprelated.com ">http://docs.yahoo.com/info/terms/

i
Yahoo! India Matrimony: Find your partner online.Post your profile.





Hi Raj,

         Are you using the -d option?

Bhooshan

>hi all,

>I have a set of 40-50 global variables. I put all of
>them in one .h file say define.h. This define.h is
>included in some 6 files. CCS says that the variables
>are already defined (this is due 2 multiple
>inclusions) and gives error. I tried 2 work around the
>problem using the K & R approach:
>#ifndef _DEFINE_H
>#define _DEFINE_H
>/*contents of define.h*/
>#endif
>But this also doesn't work
>what might be the possible reason?
>Rajesh
>
>
>________________________________________________________________________
>Yahoo! India Matrimony: Find your partner online.
>Go to http://yahoo.shaadi.com
>
>
>_____________________________________
>Note: If you do a simple "reply" with your email client, only the author of this message will receive your answer. You need to do a "reply all" if you want your answer to be distributed to the entire group.
>
>_____________________________________
>About this discussion group:
>
>To Join: Send an email to c...@yahoogroups.com
>
>To Post: Send an email to c...@yahoogroups.com
>
>To Leave: Send an email to c...@yahoogroups.com
>
>Archives: http://www.yahoogroups.com/group/c6x
>
>Other Groups: http://www.dsprelated.com
>
>
>
>
>


Calling all NRIs! Avail of the best financial services. Smile all the way with ICICI Bank.

Raj-

> I want all the global variables(say a b c d) at one place say some
> define.h and the in all the project files I want to include this
> define.h. For every such inclusion excepting the file
> that is linked first, I get the error message variable a b c d are
> already declared(if i include in 5 files I get 16 errors). To
> come around this problem i used K &R approach
> #ifndef _DEFINE_H
> #define _DEFINE_H
> /* contents of define.h*/
> #endif.
> Even then I get the same error messages.

Try something like this:

#ifndef _GLOBALS

#define DECLSPEC

#else

#define DECLSPEC extern

#endif DECLSPEC int a;
DECLSPEC int b;
.
.
.

then pick only *one* .c file and define _GLOBALS prior to including define.h,
for
example main.c:

#define _GLOBALS
#include define.h
#undef _GLOBALs

The idea is that *one* time the variables are declared (instantiated) and use up
mem
space. All other times they are referenced as extern.

-Jeff




I second Jeff's suggestion. I have used this method for over 10 years and it
works great.

Paul

-----Original Message-----
From: Jeff Brower [mailto:]
Sent: Tuesday, October 21, 2003 1:36 AM
To: Rajesh
Cc:
Subject: Re: [c6x] problem with conditional compilation Raj-

> I want all the global variables(say a b c d) at one place say some
> define.h and the in all the project files I want to include this
> define.h. For every such inclusion excepting the file
> that is linked first, I get the error message variable a b c d are
> already declared(if i include in 5 files I get 16 errors). To
> come around this problem i used K &R approach
> #ifndef _DEFINE_H
> #define _DEFINE_H
> /* contents of define.h*/
> #endif.
> Even then I get the same error messages.

Try something like this:

#ifndef _GLOBALS

#define DECLSPEC

#else

#define DECLSPEC extern

#endif DECLSPEC int a;
DECLSPEC int b;
.
.
.

then pick only *one* .c file and define _GLOBALS prior to including
define.h, for
example main.c:

#define _GLOBALS
#include define.h
#undef _GLOBALs

The idea is that *one* time the variables are declared (instantiated) and
use up mem
space. All other times they are referenced as extern.

-Jeff

_____________________________________
Note: If you do a simple "reply" with your email client, only the author of
this message will receive your answer. You need to do a "reply all" if you
want your answer to be distributed to the entire group.

_____________________________________
About this discussion group:

To Join: Send an email to

To Post: Send an email to

To Leave: Send an email to

Archives: http://www.yahoogroups.com/group/c6x

Other Groups: http://www.dsprelated.com ">http://docs.yahoo.com/info/terms/


--- Jeff Brower <> wrote:
> Try something like this:
>
> #ifndef _GLOBALS
>
> #define DECLSPEC
>
> #else
>
> #define DECLSPEC extern
>
> #endif > DECLSPEC int a;
> DECLSPEC int b;
> .
> .
> .
>
> then pick only *one* .c file and define _GLOBALS
> prior to including define.h, for
> example main.c:
>
> #define _GLOBALS
> #include define.h
> #undef _GLOBALs
>
> The idea is that *one* time the variables are
> declared (instantiated) and use up mem
> space. All other times they are referenced as
> extern.

god... Jeff... with all the respect... I think what
you're proposing is nasty! if you go back to the first
"C language lesson" one can learn what the difference
between *.h files and *.c files is...

I told the original poster what to do:

1. define all variables in *.c file and
2. declare them in *.h file

then all you need to do is to include the *.h file in
all your c files where you want to have the access to
the variables... the header files with all those
declarations are just for the compiler so it knows
what the variables' types are... it's all clean and
it's just C - nothing more...

plus - let's keep the C naming style - you say:

> The idea is that *one* time the variables are
> declared (instantiated) and use up mem
> space. All other times they are referenced as
> extern.

I would say - the variable needs to be _defined_ once
in the *.c source file and it needs to be _declared_
in the *.h header file...

and to the OP: try creating a simple project with a
couple of variables - learn to walk before you run -
as he explained in the first post that he has some 40
something variables... well - as I said - keep it
simple and clean...

good luck to all you ;-)

Wojciech Rewers

__________________________________



--- Raj <> wrote:
> I want all the global variables(say a b c d) at one
> place say some define.h and the in all the project
> files I want to include this define.h. For every
> such inclusion excepting the file that is linked
> first, I get the error message variable a b c d are
> already declared(if i include in 5 files I get 16
> errors).

well... are you really sure the compiler tells you
that your variables are already _declared_? from what
you're explaining - you are double (or multiple)
_defining_ them! Learn what the difference between
definition and declaration is!

definition is the process of creating (instantiating)
a variable - you are supposed to do that in *.c (and
not *.h) file...

declaration is just to tell the compiler the type of
the specified variable - which itself (that variable)
is defined somewhere in the souce (*.c) file and will
be linked later on...

god... those are the C language basics! it's really
useful to learn them!

> somebody said u declare all the variables in .c file
> and make extern declarations in .h file and include
> that .h file in all the files.

well - that was me - but you misunderstood me (or
can't even quote) - I told you to _define_ all the
variables in *.c file and _declare_ them as extern in
the header (*.h) file! then you include that header
file in some source (*.c) files where the compiler
needs to know the type of your variables defined in
some other source files!

> This approach also didn't work
> So I am trying to figure out what is the problem.
> I guess this explains my problem better

well... many times I only give "hints" and I'm not
really sure about whether it's gonna work, or not...
well - this time is different... this time I know for
sure that what I'm saying is 100% correct!!! you just
have to know the difference between _definition_ and
_declaration_

god - I did repeat everything at least 3 times -
didn't I?

well - good luck to you all ;-)

Wojciech Rewers

__________________________________


Wojciech Rewers wrote:
--- Jeff Brower <j...@signalogic.com> wrote:
Try something like this:
#ifndef _GLOBALS
#define DECLSPEC
#else
#define DECLSPEC extern
#endif
DECLSPEC int a;
DECLSPEC int b;
.
.
.
then pick only *one* .c file and define _GLOBALS
prior to including define.h, for
example main.c:
#define _GLOBALS
#include define.h
#undef _GLOBALs
The idea is that *one* time the variables are
declared (instantiated) and use up mem
space. All other times they are referenced as
extern. 
god... Jeff... with all the respect... I think what
you're proposing is nasty! if you go back to the first
"C language lesson" one can learn what the difference
between *.h files and *.c files is...
I told the original poster what to do:
1. define all variables in *.c file and
2. declare them in *.h file
I think he already did that by looking at his original posting. He tried to put all the variables in one define.h and include this define.h in other project files.

My guess of his real problem is he somehow  *re-declares* those variables in the .c files, which  is not likely to happen  for  most  engineers. Given the fact that he is still at the very begining stage of the C programming, I wouldn't think my guess is too wild.

Hey Raj, why don't you post your define.h file and one project file that has the compile error. Just cut and paste the relevant section of code. That will solve the puzzle of "what the heck is going on". :-)

then all you need to do is to include the *.h file in
all your c files where you want to have the access to
the variables... the header files with all those
declarations are just for the compiler so it knows
what the variables' types are... it's all clean and
it's just C - nothing more...
plus - let's keep the C naming style - you say:
The idea is that *one* time the variables are
declared (instantiated) and use up mem
space. All other times they are referenced as
extern. 
I would say - the variable needs to be _defined_ once
in the *.c source file and it needs to be _declared_
in the *.h header file...
and to the OP: try creating a simple project with a
couple of variables - learn to walk before you run -
as he explained in the first post that he has some 40
something variables... well - as I said - keep it
simple and clean...
good luck to all you ;-)
Wojciech Rewers
Good luck to everybody! We do need some luck along the road, right? :-) Jane