2

I am not very much familiar with modular structure of C programming, so I decided to ask you for help. Let pretend I have two modules into the project. The one is sx1272.c and the other is sx1272_ll.c. The variable radio_is_on is declared in sx1272_ll.c as follows:

static uint8_t volatile radio_is_on;

and then accessed by function in sx1272.c like this:

foo(){
   ...............
   if(radio_is_on){
      blablabla...
      ............
   }
   ...............
}

So if radio_is_on is static (in module scope) how come it is accessed by other modules?

sx1272_ll.c

sx1272.c

Radoslaw Krasimirow
  • 1,833
  • 2
  • 18
  • 28
  • You might need to show us more code. Generally, what you describe is not possible. There might be weird code that causes this apparent behaviour. – fuz Aug 24 '15 at 09:55
  • Have you compiled and linked this? Without error? Please, read more books, try to write programs and then ask. – i486 Aug 24 '15 at 09:57
  • Are you *sure* you don't have a declaration for `radio_is_on` in either `sx1272-defs.h` or `sx1272.h`? What compiler are you using? – John Bode Aug 24 '15 at 14:12

4 Answers4

4

It seems to me that you included

 #include "sx1272_ll.c"

in you your source file sx1272.c

So the whole content of file sx1272_ll.c is just copy-pasted into sx1272.c, at the position where you have written #include "sx1272_ll.c" during the pre-proccesing stage.

Refer this link: https://en.wikipedia.org/wiki/C_preprocessor#Including_files

K.H.A.J.A.S
  • 514
  • 4
  • 19
  • Yes this is the case in this case :) – Radoslaw Krasimirow Aug 25 '15 at 10:41
  • 1
    For static keyword to work correctly, you need to make each of your source code files in standard way, that is splitting it into "file.h" and "file.c" and including "file.h" header file. Check this Stackoverflow question to get more details about splitting C source code into modules in standard way: http://stackoverflow.com/questions/7109964/creating-your-own-header-file-in-c – K.H.A.J.A.S Aug 27 '15 at 07:10
2

The static keyword outside any function means, that variable (more strictly object represented by that variable) has internal linkage, which you can think as it is more or less private to that module, where it is defined. There would be no symbol generated in object file for such variable (you could inspect it with tools like nm or readelf).

You might either define it with external linkage by omitting static (optionally adding extern keyword), or if you need it to be static for some special reason (?), then write wrapper function, that exposes it into other module. It might be defined as:

sx1272_ll.c:

static uint8_t volatile radio_is_on;

uint8_t volatile *get_radio_is_on(void)
{
    return &radio_is_on;
}

then put prototype into:

sx1272.c:

uint8_t volatile *get_radio_is_on(void);

There is no risk, that such pointer would point into invalid memory region, because file-scope variables have static storage duration, that means lifetime of whole application.

Grzegorz Szpetkowski
  • 36,988
  • 6
  • 90
  • 137
2

you cannot declare variable static and access it from another module.

static has 3 usages:

1) if variable declared static whithin block (eg. function) it will maintial it's value during invocations.

int add(void){
        static int var = 0;
        var++;
        return var; 
}
int main(void){
        printf("Var = %d", add());    // Here var = 1 
        printf("Var = %d", add());    // Here var = 2
        return 0;
}

2) if variable declared static in a file, it can be accessed only within this file and no other file can access it. If we declare static uint8_t volatile radio_is_on; in file X, it will not be accessible in file Y.

3) if function declared static in a file, it can be accessed only within this file and no other file can access it.

Nasr
  • 2,482
  • 4
  • 26
  • 31
1

In the strictest sense its not possible to access static variables from other modules, that's exactly what the keyword is intended for.

But one way to do it is to add an external function is_radio_on() which returns its value.

Serve Laurijssen
  • 9,266
  • 5
  • 45
  • 98