Cypress Perform

Home > Design Support > Cypress Developer CommunityTM > Cypress Forums > PSoC® 5 > Global variables definitions for project with multiples files ?

Bookmark and Share
Cypress Developer CommunityTM
Forums | Videos | Blogs | Training | Rewards Program | Community Components



Global variables definitions for project with multiples files ?
Moderator:
ANCY

Post Reply
Follow this topic



Global variables definitions for project with multiples files ?

jpvr posted on 29 Jan 2013 6:37 AM PST
Member
8 Forum Posts

 

Dear All,

 

Can you tell me the way to declare global variables (only once) in a Psoc 5 project that contain many sub routines files ?

I tried to create a single .h file containing all the global variables and include him into each sub files, but the compiler seems to not be happy with this solution.

Is there a proper way to do that ?

Thank you,

JP

 




Re: Global variables definitions for project with multiples files ?

hli posted on 29 Jan 2013 07:57 AM PST
Top Contributor
675 Forum Posts

You write

 extern int value;

in the header file, which you then include in all place where you need access to the value. Then you write

 int value=0;

in just one C-file. That way, the variable gets defined only once, and not multiple times. For a complete explanation, see here



Re: Global variables definitions for project with multiples files ?

jpvr posted on 29 Jan 2013 11:23 AM PST
Member
8 Forum Posts

 

Thank you infinitely for the answer !

I have already used the "extern" keyword but your link gave me the good way to use only one header file with him.

Thank you again  

JP



Re: Global variables definitions for project with multiples files ?

hli posted on 30 Jan 2013 12:32 AM PST
Top Contributor
675 Forum Posts

Actually you don't need to use a single header file. The 'extern' statement is to declare the variable, so the compiler knows that it exists. Since include files are just that - text-based includes - it doesn't matter how many of them you have containing this declaration.

Whats important is that you have exactly one definition of the variable (this is, the line without 'extern'), because that is the place which defines how the variable really looks like, and reserver the space in memory for storing it. YOu will get a linker error when you have multiple definitions.

Having dais that, it still is advisable and good practice to use only a single header file. That way you ensure the declaration is always the same. Also, one can see the dependencies between different parts of the code.



Re: Global variables definitions for project with multiples files ?

Bob Marlowe posted on 30 Jan 2013 03:23 AM PST
Top Contributor
1768 Forum Posts

I feel it's the time to say something about header-files.

Header-files ( .h ) usually contain "declarations" only, no definitions. Thus only the compiler is told about some new objects to come. >very often used to declare prototypes of functions, typedefs and structures that must be known to the calling ( main() ) program.

So accidently there can pop-up an errormessage when you #include the same file in several places within your project stating that something is defined more than once.

There is a standard way out of this and commonly used (though not as commonly known) by C programmers:

A  .h-file usually starts with the lines

#ifndef mysymbol // __Filename_h would be a very good value for mysymbol since it has to be unique within the project

#define mysymbol // now the preprocessor "knows" this file has been included

// here goes the .h-file body

#endif

 

Happy coding

Bob

 



Re: Global variables definitions for project with multiples files ?

jpvr posted on 30 Jan 2013 01:04 PM PST
Member
8 Forum Posts

Hello guys,

Thank you for all of your valuable answers.

Following the link of Hli, I have used a .h file for definition and declaration of my global variables :

#ifdef DEFINE_VARIABLES

#define EXTERN

#else

#define EXTERN extern

#endif

EXTERN type myvariable1

EXTERN type myvariable2

 

The "DEFINE_VARIABLES" keyword, help to switch between definition and declaration of the variables.

Naturally, one can extend (and bulletproof) the method with more pragma directives (see above link from Hli) but I am already happy with these few lines because, now, I have just to change / add variable(s) in one single file.

JP

 

 

 



Re: Global variables definitions for project with multiples files ?

jpvr posted on 31 Jan 2013 01:39 AM PST
Member
8 Forum Posts

and for a header file named global.h, an improvement will be maybe :

#undef

EXTERN

#ifndef

__GLOBAL_H

#define

__GLOBAL_H

#define

EXTERN

#else

#define

EXTERN extern

#endif

EXTERN type myvariable



Re: Global variables definitions for project with multiples files ?

Bob Marlowe posted on 31 Jan 2013 02:50 AM PST
Top Contributor
1768 Forum Posts

That's a good idea, but it will fail when the variable is initialized as

 

EXTERN uint8 MyVar = 27;

 

Bob



Re: Global variables definitions for project with multiples files ?

jpvr posted on 31 Jan 2013 06:54 AM PST
Member
8 Forum Posts

Always following the link from Hli :

EXTERN

#undef

INITIALIZER

#undef

__GLOBAL_H

#ifndef

__GLOBAL_HEXTERN INITIALIZER(...) = __VA_ARGS__

#else

EXTERN extern INITIALIZER(...)

#endif

EXTERN uint8 myvariable INITIALIZER(7);

#define

 

#define

 

#define

#define

#define

But maybe a little tricky...

(Sorry for the text format, my version of IE seems to bug with the editor).

 

 

 

 

 

 



Re: Global variables definitions for project with multiples files ?

jpvr posted on 31 Jan 2013 06:59 AM PST
Member
8 Forum Posts

Argh, the previous post is completely wrong






ALL CONTENT AND MATERIALS ON THIS SITE ARE PROVIDED "AS IS". CYPRESS SEMICONDUCTOR AND ITS RESPECTIVE SUPPLIERS MAKE NO REPRESENTATIONS ABOUT THE SUITABILITY OF THESE MATERIALS FOR ANY PURPOSE AND DISCLAIM ALL WARRANTIES AND CONDITIONS WITH REGARD TO THESE MATERIALS, INCLUDING BUT NOT LIMITED TO, ALL IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT OF ANY THIRD PARTY INTELLECTUAL PROPERTY RIGHT. NO LICENSE, EITHER EXPRESS OR IMPLIED, BY ESTOPPEL OR OTHERWISE, IS GRANTED BY CYPRESS SEMICONDUCTOR. USE OF THE INFORMATION ON THIS SITE MAY REQUIRE A LICENSE FROM A THIRD PARTY, OR A LICENSE FROM CYPRESS SEMICONDUCTOR.

Content on this site may contain or be subject to specific guidelines or limitations on use. All postings and use of the content on this site are subject to the Terms and Conditions of the site; third parties using this content agree to abide by any limitations or guidelines and to comply with the Terms and Conditions of this site. Cypress Semiconductor and its suppliers reserve the right to make corrections, deletions, modifications, enhancements, improvements and other changes to the content and materials, its products, programs and services at any time or to move or discontinue any content, products, programs, or services without notice.

Spec No: None; Sunset Owner: GRAA; Secondary Owner: RAIK; Sunset Date: 01/01/20