Reputation: 408
I am looking at ArduCopter code. I see that there are few variables declared as static in ArcuCopter.pde. However these variables are called in other .pde files. For example object g is defined here as static and it is used in other Attitude.pde file many times like this example.
As per my knowledge static memeber has a scope limited to a file and cannot be called in other file. So my question is how this can be done? Is there any way to access static member defined in other file as it is?
Upvotes: 1
Views: 680
Reputation: 8589
Are you perhaps confusing 'file' and 'translation unit'.
Refer here http://port70.net/~nsz/c/c99/n1256.html#6.2.2
You can use identifiers for static objects declared in other files so long as they are part of the same translation unit (and appropriately declared at the point of use...).
A translation unit is a (normally notional) file made up of a source file (.c or .cpp probably) with all it's #include
directives 'expanded'.
You can 'use' data declared static in another translation unit. But not an identifier. How?
daft.h:
static int x=0;
int nextX(void);
daft.c
#include "daft.h"
int nextX(void){
return ++x;
}
my.c
#include <stdlib.h>
#include <stdio.h>
#include "daft.h"
int main(void){
printf("%d\n", nextX());//outputs 1.
printf("%d\n", x);//prints 0.
return EXIT_SUCCESS;
}
Where we assume that daft.c
and my.c
are source files. Both their translation units have an identifier x
for an internally linked int
.
The two identifiers and the storage for the objects are unrelated!
The call to nextX()
accesses the storage for the identifier x
internally linked in daft.c
. The direct access x
accesses the identifier internally linked in my.c
.
Unless identifiers are declared const
having static
linkage identifiers in header files usually leads to trouble.
Upvotes: 1