Global variables in IAR

Go To Last Post
12 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi to all,
I searched for some hours here but I cannot solve my IAR problem.
I want to declare a global variable in IAR and use it in different linked files.The test was done by a very simple code. Target chip is ATXMEGA64A3:

main.c

#include
#include "f.h"
void main(void)
{
 f=0x55;
 ff();
 while(1);
}

f.c

#include
#include "f.h"
void ff(void)
{
 f=0xAA;
}

f.c was added to the project.I have tested following f.h files.

f.h

extern volatile unsigned char f;
void ff(void);

Using extern generates : Error[e46]: Undefined external "f" referred in f ( C:\ test\Debug\Obj\f.r90)

f.h

static volatile unsigned char f;
void ff(void);

Using static generates no error. But in AVRStudio disassembler the address of "f" variable is different for each section( 0x55 stores at 0x2041 and 0xAA stores at 0x2040).

f.h

__no_init volatile unsigned char f @ 0x2040;
void ff(void);

The recent file works and the address of "f" is the same at all (0x2040). But what is the correct declaration for "f" with variable address specified by compiler?

Ozhan KD
Knowledge is POWER

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
extern volatile unsigned char f;

You also need a definition in one of your .c files:

volatile unsigned char f;
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

You also need a definition in one of your .c files:

Code:
volatile unsigned char f;


He has

static volatile unsigned char f; 

It's the static that breaks it. Remove that and everything should turn out a little better.

Sooner or you will run into problems due to having definitions in .h files. Place it in a .c file instead. Pick the one where the variable most naturally is "at home". Then, in a header file you put the declaration (ie the one with the extern keyword) and include that in other .c files that needs to see the variable.

Why did the "static" get there in the first place? It makes little or no sense at all.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
You also need a definition in one of your .c files

Thanks, TimothyEBaldwin. Problem solved by defining “f” in f.c and now addresses are the same. The second question: is there any need for static variables in multiple file projects with common global variables? In other words, can be such a global variable static?

Quote:
Why did the "static" get there in the first place?

I tested “static volatile” and “ volatile static” and the results are the same in IAR.

Ozhan KD
Knowledge is POWER

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

static (in this instance) roughly means "nothing outside this .c file can "see" this variable"

Therefore there's no such thing as a "global static". A variable is either global, static or local to a function (possibly static within it but that then has a different meaning).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

A static var in a function has function scope, in a file it has file scope. So in ANOTHER file, you cant see it.

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for answers.
What about static definition of a function return value?

static unsigned char my_function(void)

Ozhan KD
Knowledge is POWER

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

That makes the function static, not the return value. The return value is not a variable (and therefore does not take up memory itself). Static here simply limits the scope that the function is visible in.

Quote:
possibly static within it but that then has a different meaning
Not really. A static variable is one that is persistent and limited to the scope that it is defined. This definition of static pertains to both. The only real difference is that a "global" variable is already persistent, and a local variable is already limited in scope.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What is the difference between defined scope and memory allocation of static and nonstatic functions?

Ozhan KD
Knowledge is POWER

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

What is the difference between defined scope and memory allocation of static and nonstatic functions?

You mean where does the linker place them in .text? AFAIK the linker just allocated space in .text as it comes to each item - it doesn't group specific kinds of functions or anything like that.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
#include
static unsigned char my_function(void);
main()
{  
 my_function();
 while(1);
}
static unsigned char my_function(void)
{
 unsigned char a=1;
 a++;
 return a;
}   
#include
unsigned char my_function(void);
main()
{  
 my_function();
 while(1);
}

unsigned char my_function(void)
{
 unsigned char a=1;
 a++;
 return a;
}   

Both above codes have a common assembly code:

my_function();
+000000F4:   D001        RCALL     PC+0x0002      
while(1);
+000000F5:   CFFF        RJMP      PC-0x0000      
@000000F6: my_function
unsigned char a=1;
+000000F6:   E001        LDI       R16,0x01       
a++;
+000000F7:   9503        INC       R16            
13:        return a;
+000000F8:   9508        RET                      

When a variable is static,this means it remains valid through the entire execution of application(Refer to IAR C/C++ compiler Reference).But for a function, What is the difference between calling static and nonstatic functions? Are the function variables static? As we can see from assembly code,static function in first example does not tend variables to be static.

Ozhan KD
Knowledge is POWER

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

But for a function, What is the difference between calling static and nonstatic functions?

There's no difference in the generated code but if you look to the .map file the static version won't be listed. All "static" does for a function is limit it's name scope to determine whether it is linkable or not.
Quote:

Are the function variables static?

No. A variable is only static if the word static appears on its definition.

I think you really, really need to read a good book about C programming. I assume you have a copy of K&R. Maybe re-read the section on "static" within that and see if things are clearer?

(BTW a local is normally created on the (data) stack but a local that is static will be in .bss - yet it's name will not be listed in .map as it's not a linkable entity)