Passing variables when compiling/building project

ns_esp
Posts: 6
Joined: Sun Jun 13, 2021 2:36 am

Passing variables when compiling/building project

Postby ns_esp » Fri Apr 29, 2022 9:05 pm

Hello,
I'd like to be able to pass variables into my code when building a project, so that parameters such as IP address, device name, and password can be changed without editing my code.
I know that in C there is the compiler flag -D that can do this, but I'm not sure how to use it in the esp idf.py build.
To attempt this I modified the blink.c example to print out the integer passed through the compiler/

Code: Select all

/* Blink Example

   This example code is in the Public Domain (or CC0 licensed, at your option.)

   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.
*/
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "sdkconfig.h"

#define BLINK_GPIO CONFIG_BLINK_GPIO

void app_main()
{
    gpio_pad_select_gpio(BLINK_GPIO);
    /* Set the GPIO as a push/pull output */
    gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);
    while(1) {
        /* Blink off (output low) */
	printf("Turning off the LED\n");
        gpio_set_level(BLINK_GPIO, 0);
        vTaskDelay(1000 / portTICK_PERIOD_MS);
        /* Blink on (output high) */
	printf("Turning on the LED\n");
        gpio_set_level(BLINK_GPIO, 1);
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    printf("%i\n",test);
    }
}
I then called:
idf.py build -Dtest=1

I received the warning when compiling:

Code: Select all

CMake Warning:
  Manually-specified variables were not used by the project:

    test
But the compile failed since:

Code: Select all

../main/blink.c: In function 'app_main':
../main/blink.c:48:19: error: 'test' undeclared (first use in this function)
     printf("%i\n",test);
                   ^~~~
../main/blink.c:48:19: note: each undeclared identifier is reported only once for each function it appears in
Is there something I am missing? Any advice would be appreciated.

Thank you very much!

Who is online

Users browsing this forum: Bing [Bot] and 369 guests