How to pass variables to west

I want to pass a variable to my program so I'm using the command:
west build -b nrf52840dk_nrf52840 -p --build-dir build -- -Dmy_variable=40

Then in my program I want to print it:

int main()
{
  #ifdef my_variable
    printf("my_variable = %i",my_variable);
  #else
    printf("my_variable is not defined");
  #endif
}


But the result is "my_variable is not defined". What am I doing wrong?

Parents
  • Hi,

    The slightly more complex but more in the "Zephyr spirit", method would be to add a Kconfig file to your project defining it:

    config MY_VARIABLE
    int "My variable explain explain explain"
    range 10 100
    help
        This is an example for how to set a custom variable for your application
    
    menu "Zephyr Kernel"
    source "Kconfig.zephyr"
    endmenu
    

    And then use -DCONFIG_MY_VARIABLE.

    This will let you define it in prj.conf if you want as well.
    And you can take advantages of features of Kconfig, such as the range attribue I show in my example. (That can also be removed, it is optional).

    Regards,
    Sigurd Hellesvik

Reply
  • Hi,

    The slightly more complex but more in the "Zephyr spirit", method would be to add a Kconfig file to your project defining it:

    config MY_VARIABLE
    int "My variable explain explain explain"
    range 10 100
    help
        This is an example for how to set a custom variable for your application
    
    menu "Zephyr Kernel"
    source "Kconfig.zephyr"
    endmenu
    

    And then use -DCONFIG_MY_VARIABLE.

    This will let you define it in prj.conf if you want as well.
    And you can take advantages of features of Kconfig, such as the range attribue I show in my example. (That can also be removed, it is optional).

    Regards,
    Sigurd Hellesvik

Children
Related