Static and non-static functions in Pebble apps

Having just started developing Pebble , and also returning to my core C skills that have not been supported for many many years, I am trying to understand the basic structure of these Pebble applications.

I know the difference between static and non-static, but would be very happy if anyone could help shed some light on the impact on my application in this case. I've pasted the sample code below below that shows how the Pebble app is structured in two cases.

Static version

#include <pebble.h>

static Window *window;

static void handle_init(void) {
    window = window_create();
    window_stack_push(window, true);

}

static void handle_deinit(void) {
    window_destroy(window);
}

int main(void) {
    handle_init();
    app_event_loop();
    handle_deinit();
}

      

Non-static version

#include <pebble.h>

Window *window;

void handle_init(void) {
    window = window_create();
    window_stack_push(window, true);

}

void handle_deinit(void) {
    window_destroy(window);
}

int main(void) {
    handle_init();
    app_event_loop();
    handle_deinit();
}

      

My question is:

What are the implications of using non-static vs static variables and functions?

I tried looking for information on the Pebble developer site, but the static and non-static examples seem to be used without much consistency and I haven't found a good official guide.

+3


source to share


1 answer


It has to do with snapping and visibility. In short, the globals marked static

are not exported from the translation unit (source file) in which it is defined. This means that if you have multiple source files in your project, if you declare a global variable or function static

in the same file, that variable or function cannot be "seen" or used from any other source file.



And that's (or should be) basic C knowledge, nothing special about Pebble.

+9


source







All Articles