Extern tells the compiler not to reserve space for the variable. This prevents the variable from being allocated once for every C++ file.
To solve your problem, you need one declaration of it in one of the .cpp files as well, without extern.
Ok, so you are trying to have this function assign to a different variable each time it’s called? That means you are using the wrong syntax.
To avoid confusion let’s think of lv_obj_t *
as lv_obj_handle_t
for the purposes of our discussion.
Right now this is what you have.
lv_obj_handle_t settingsMenuButtons; /* global */
lv_obj_handle_t displaySettingsBtn; /* global */
/* Note that this function takes lv_obj_handle_t (lv_obj_t *) and not lv_obj_handle_t * (lv_obj_t **) */
/* This means that object handles will be passed by value */
void settingsMenuBtn(lv_obj_handle_t settingsMenuButtons, char* label, int pos_x, int pos_y, int width, int height) {
/* this assigns to the local variable (function parameter) */
/* and does not update the global */
settingsMenuButtons = lv_btn_create(popup, NULL);
lv_obj_set_event_cb(settingsMenuButtons, event_handler);
}
/* This call passes the current _value_ of displaySettingsBtn to the function, */
/* not a reference. That means the global will never be updated. */
settingsMenuBtn(displaySettingsBtn, ...);
Hopefully the comments explained it. Here is the fixed version.
lv_obj_handle_t settingsMenuButtons; /* global */
lv_obj_handle_t displaySettingsBtn; /* global */
/* adjusted to receive lv_obj_handle_t * (lv_obj_t **) */
void settingsMenuBtn(lv_obj_handle_t settingsMenuButtons, char* label, int pos_x, int pos_y, int width, int height) {
/* this assigns to the global variable referenced by settingsMenuButtons */
/* caution: since the parameter is lv_obj_t ** (a pointer to lv_obj_t *), you need */
/* to dereference it before passing it to LVGL functions */
*settingsMenuButtons = lv_btn_create(popup, NULL);
lv_obj_set_event_cb(*settingsMenuButtons, event_handler);
}
/* This call passes a reference to `displaySettingsBtn` to the function, */
/* so the global will get updated */
settingsMenuBtn(&displaySettingsBtn, ...);