DSPRelated.com
Forums

problem with conditional compilation

Started by Raj October 20, 2003
Wojciech

What Jeff proposed is a non-standard technique that is often used to keep
tabs on global variables. This shortcut allows one header file to declare
and define global variables in one central location. Non-standard, but very
efficient!

As far as the original problem, a possibility is that the header files are
nested, such that the main program includes a header, that in turn includes
the header with the "extern" declarations. In any case, your suggestion
to "walk before you run" is always a good idea.

Bill

> -----Original Message-----
> From: Wojciech Rewers [mailto:]
> Sent: Tuesday, October 21, 2003 12:23 PM
> To: Jeff Brower; Rajesh
> Cc:
> Subject: Re: [c6x] problem with conditional compilation > --- 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
>
> __________________________________
>

_____________________________________
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/



Wojciech-

> 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...

Yeah yeah Mr. Purist, I know it was sort of ugly. I was just trying to
help Rajesh do it "his way", but still keep it clean and understandable.

But you're right, my method is not clean. The truth is I wouldn't let our
engineers here do it that way either :-)

I sure hope Rajesh is learning something about code organization and
maintainability from this thread.

-Jeff > 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
>
> __________________________________
>





--- William Zimmerman <> wrote:
> What Jeff proposed is a non-standard technique that
> is often used to keep
> tabs on global variables. This shortcut allows one
> header file to declare
> and define global variables in one central location.
> Non-standard, but very
> efficient!

you mean - it allows to _define_ _and_ _declare_
everything in one file? well - you're right - it's
non-standard... is it efficient? in terms of
compilation speed? in terms of the learning curve for
somebody who's trying to read the code? in terms
of...? is it really a shortcut? I really doubt it...

so - even if what you are saying that this "shortcut"
defines and declares all variables in one place is
true - I still think mine is better - all you need is
two standard and clean C language files: a source file
and a header file...

in most of my projects I have those two files:
declarations.h where all my global variables are
declared
variables.c where all my global variables are defined

good luck to you all ;-)

Wojciech Rewers

__________________________________


Hi, Wojciech Rewers,

You are right! But this is also make some mess in debug because you always need
to add or delete some variables. I always define the variables when they are
first used. And if the variable is used in other files I declare them in the
head file. My method is also of inconvenience in debug. If the project is not
big it can be used. If the project is big and when the rtos is needed you can
refer to the style UCOS II used.

Tao Wang
ShangHai

----- Original Message -----
From: Wojciech Rewers <>
Date: Tue, 21 Oct 2003 09:37:19 -0700 (PDT)
To: Raj <>, Jeff Brower <>
Subject: Re: [c6x] problem with conditional compilation

Re: --- Raj <> wrote:
Re: > I want all the global variables(say a b c d) at one
Re: > place say some define.h and the in all the project
Re: > files I want to include this define.h. For every
Re: > such inclusion excepting the file that is linked
Re: > first, I get the error message variable a b c d are
Re: > already declared(if i include in 5 files I get 16
Re: > errors).
Re:
Re: well... are you really sure the compiler tells you
Re: that your variables are already _declared_? from what
Re: you're explaining - you are double (or multiple)
Re: _defining_ them! Learn what the difference between
Re: definition and declaration is!
Re:
Re: definition is the process of creating (instantiating)
Re: a variable - you are supposed to do that in *.c (and
Re: not *.h) file...
Re:
Re: declaration is just to tell the compiler the type of
Re: the specified variable - which itself (that variable)
Re: is defined somewhere in the souce (*.c) file and will
Re: be linked later on...
Re:
Re: god... those are the C language basics! it's really
Re: useful to learn them!
Re:
Re: > somebody said u declare all the variables in .c file
Re: > and make extern declarations in .h file and include
Re: > that .h file in all the files.
Re:
Re: well - that was me - but you misunderstood me (or
Re: can't even quote) - I told you to _define_ all the
Re: variables in *.c file and _declare_ them as extern in
Re: the header (*.h) file! then you include that header
Re: file in some source (*.c) files where the compiler
Re: needs to know the type of your variables defined in
Re: some other source files!
Re:
Re: > This approach also didn't work
Re: > So I am trying to figure out what is the problem.
Re: > I guess this explains my problem better
Re:
Re: well... many times I only give "hints" and I'm not
Re: really sure about whether it's gonna work, or not...
Re: well - this time is different... this time I know for
Re: sure that what I'm saying is 100% correct!!! you just
Re: have to know the difference between _definition_ and
Re: _declaration_
Re:
Re: god - I did repeat everything at least 3 times -
Re: didn't I?
Re:
Re: well - good luck to you all ;-)
Re:
Re: Wojciech Rewers
Re:
Re: __________________________________
Re:
Re:
Re: _____________________________________
Re: 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.
Re:
Re: _____________________________________
Re: About this discussion group:
Re:
Re: To Join: Send an email to
Re:
Re: To Post: Send an email to
Re:
Re: To Leave: Send an email to
Re:
Re: Archives: http://www.yahoogroups.com/group/c6x
Re:
Re: Other Groups: http://www.dsprelated.com
Re:
Re:
Re: ">http://docs.yahoo.com/info/terms/
Re:
Re:

--
__________________________________________________________
Sign-up for your own personalized E-mail at Mail.com
http://www.mail.com/?sr=signup

CareerBuilder.com has over 400,000 jobs. Be smarter about your job search
http://corp.mail.com/careers



hi Rewers & Jeff,
I thank you for your suggestions. Rewers approach
worked finally. But then the main point that I want to
raise is that I have the same code running on 2
paltforms (suse linux 8.1) and MSVC6.0, there I don't
have this problem. there must be some difference
between the CCS's C compiler and the other compilers.
I am new 2 C prog for DSP but not for C prog itself!!
Rajesh

________________________________________________________________________
Yahoo! India Matrimony: Find your partner online.
Go to http://yahoo.shaadi.com


Wojciech-

> you mean - it allows to _define_ _and_ _declare_
> everything in one file? well - you're right - it's
> non-standard... is it efficient? in terms of
> compilation speed? in terms of the learning curve for
> somebody who's trying to read the code? in terms
> of...? is it really a shortcut? I really doubt it...
>
> so - even if what you are saying that this "shortcut"
> defines and declares all variables in one place is
> true - I still think mine is better - all you need is
> two standard and clean C language files: a source file
> and a header file...
>
> in most of my projects I have those two files:
> declarations.h where all my global variables are
> declared
> variables.c where all my global variables are defined
>
> good luck to you all ;-)

Earlier this year, on Apr 1, I heard that in the next ANSI C standard that they
will
introduce a new file extension, called .b (both), which will incorporate my
technique
and make it standard and legitimate to use.

-Jeff



Re: oh - god - I agree... of course you can define some
Re: "temporary" local variables and all that... of course
Re: it helps in dubegging - I'm not saying that you must
Re: define all the variables in one file! but hey - look

I think we are all talk about global variables. The problem
is that we want to share some variable resources among some
routines, is not it?

Re: but hey - what does this have to do with C6x? ;-)

We talk about this problem because we are not pure C programmar,
but embedded system programmer. Our superiority is the
integration of hardware and software. So sometime we talk about
some very simple problem to pure C programmar. And C6x is not
very standard C. It is TI's C. For example you can not use 'fwrite'
and 'fprintf' as in stardard C. You should have time to familiar
with the library and routines of TI. And you also need to alarm
with bugs special of TI while not standard C.

Wang Tao
ShangHai
--
__________________________________________________________
Sign-up for your own personalized E-mail at Mail.com
http://www.mail.com/?sr=signup

CareerBuilder.com has over 400,000 jobs. Be smarter about your job search
http://corp.mail.com/careers