This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

How to make BLE_NUS_C_DEF(m_ble_nus_c) a global variable

I am adapting the example ble_app_uart_c_pca10056_s140, which is a BLE Nordic Uart Service central device. I would like that other c files can access the variable m_ble_nus_c, which is declared in main.c. Normally to declare a global variable in c, you use the keyword extern and declare the variable in a header file as follows: 

extern ble_nus_c_t m_ble_nus_c;

Then in one of the local files, you initialize the variable. However, this doesn't work here. I notice that BLE_NUS_C_DEF is a #define.

What is the best way to make m_ble_nus_c a global variable?

Parents
  • I would like that other c files can access the variable m_ble_nus_c,

    Why?

    Would probably be better to just provide accessor functions?

    You can see that  preprocessor macros are used to create these "instance" variables, and those macros make them 'static'.

    You could re-write that manually without the 'static' if you really felt it necessary ...

    What is the best way to make m_ble_nus_c a global variable

    I don't think that making it global is the "best" thing to do.  Wink

    Normally to declare a global variable in c, you use the keyword extern

    That's not quite correct.

    By default, variables defined at file scope are global - unless specified 'static'.

    You make an extern declaration (usually in a header) so that other compilation units can see it

    http://c-faq.com/decl/decldef.html

Reply
  • I would like that other c files can access the variable m_ble_nus_c,

    Why?

    Would probably be better to just provide accessor functions?

    You can see that  preprocessor macros are used to create these "instance" variables, and those macros make them 'static'.

    You could re-write that manually without the 'static' if you really felt it necessary ...

    What is the best way to make m_ble_nus_c a global variable

    I don't think that making it global is the "best" thing to do.  Wink

    Normally to declare a global variable in c, you use the keyword extern

    That's not quite correct.

    By default, variables defined at file scope are global - unless specified 'static'.

    You make an extern declaration (usually in a header) so that other compilation units can see it

    http://c-faq.com/decl/decldef.html

Children
No Data
Related