0

I was searching for some way to change the macro values. I want to use macro values like user-friendly variables. Like we used the scanf function and store the user input to a variable.

If this way is not possible then Can we use the CMake or make to do this? If yes then how?

Example.

header. h

#define LED  8

I want to change this value before compile. Is there any way we can do that?

Please explain in detail. So I can understand perfectly. Possible add some example or link of example.

Thanks.

horsemann07
  • 79
  • 1
  • 7
  • 2
    You could wrap all affected macros in `#ifndef XY #define XY 123 #endif` blocks and provide a value via `-DXY=234` command line option. – Gerhardh Jul 15 '21 at 13:07

1 Answers1

2

You need a compilation flag -DLED=8 where you replace the 8 with whatever value you want.

You should also wrap the LED macro with

#ifndef LED
#define LED 8
#endif

This way, the -DLED=X will define the macro as X and, since it's already defined, the header file won't try to redefine it.

Regarding CMake, try adding add_compile_definitions(LED=8). Check this for more info.

Vasconcelos
  • 300
  • 2
  • 9
  • Can you show me how to add the -DLED in the CMake file and how I can the value. As you explain I think I need to write this line in the CMake file. Right? Can we pass the value using the command line or some other way? – horsemann07 Jul 15 '21 at 13:15
  • Try adding this: `add_compile_definitions(LED=8)` – Vasconcelos Jul 15 '21 at 13:20
  • Check this for more info: https://cmake.org/cmake/help/latest/command/add_definitions.html – Vasconcelos Jul 15 '21 at 13:21
  • Better to use `target_compile_definitions` - see example here -> https://dev.to/iblancasa/learning-cmake-2-43ga – Den-Jason Jul 15 '21 at 13:43
  • Can you tell me how we do the same thing in using make file. I found code -> `CFLAGS= -DLED_GPIO=\$(LED_GPIO)` I put this is code in components.mk file which works the same as making a file. But this does now work. Is there any way we provide the value at command line like `-DFLAG=15`, – horsemann07 Jul 15 '21 at 14:35