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!

CatalinM
Posts: 4
Joined: Sat Dec 28, 2024 5:22 pm

Re: Passing variables when compiling/building project

Postby CatalinM » Mon Jan 26, 2026 1:33 pm

I know it's been 4 years, but I was looking for something like this and I couldn't find any solution on the internet.

However, I did find a way to get this going, so I want to post an answer for anyone else looking for a similar solution.

In you main `CMakeLists.txt` file, you need to define the option you want to pass from command line, like so:

Code: Select all

option(DEV_MODE "Enable Project development mode" 0)

Then, in your sources `CMakeLists.txt`, you can check for that option and pass a flag into your code:

Code: Select all

if(DEV_MODE)
        target_compile_definitions(${COMPONENT_TARGET} PUBLIC "-DDEVELOPMENT_MODE")
endif()

nopnop2002
Posts: 347
Joined: Thu Oct 03, 2019 10:52 pm
Contact:

Re: Passing variables when compiling/building project

Postby nopnop2002 » Tue Jan 27, 2026 1:24 am

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.
For this purpose, use Kconfig.projbuild.

https://github.com/espressif/esp-idf/bl ... .projbuild
Last edited by nopnop2002 on Tue Jan 27, 2026 8:42 am, edited 1 time in total.

MicroController
Posts: 2661
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Passing variables when compiling/building project

Postby MicroController » Tue Jan 27, 2026 8:42 am

The canonical way in IDF is via Kconfig: https://docs.espressif.com/projects/esp ... guide.html

nopnop2002
Posts: 347
Joined: Thu Oct 03, 2019 10:52 pm
Contact:

Re: Passing variables when compiling/building project

Postby nopnop2002 » Fri Jan 30, 2026 12:59 am

There is also a method like this.

Code: Select all

# The following lines of boilerplate have to be in your project's
# CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.5)


include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(version)

idf_build_set_property(COMPILE_OPTIONS "-DOPTION_INT=10" APPEND)
idf_build_set_property(COMPILE_OPTIONS "-DOPTION_HEX=0x10" APPEND)
idf_build_set_property(COMPILE_OPTIONS "-DOPTION_FLOAT=12.3" APPEND)
idf_build_set_property(COMPILE_OPTIONS "-DOPTION_STR=abcdefg" APPEND)
You can get this option value.

Code: Select all

#include <stdio.h>

#define TO_STR(x) #x
#define TO_STR_WRAPPER(x) TO_STR(x)

void app_main()
{
    printf("app_main\n");
#ifdef OPTION_INT
    printf("OPTION_INT=%d\n", OPTION_INT);
#endif
#ifdef OPTION_HEX
    printf("OPTION_HEX=0x%x\n", OPTION_HEX);
#endif
#ifdef OPTION_FLOAT
    printf("OPTION_FLOAT=%f\n", OPTION_FLOAT);
#endif
#ifdef OPTION_STR
    printf("OPTION_STR=[%s]\n", TO_STR_WRAPPER(OPTION_STR));
#endif
}
idf.py build

Code: Select all

I (284) main_task: Started on CPU0
I (294) main_task: Calling app_main()
app_main
OPTION_INT=10
OPTION_HEX=0x10
OPTION_FLOAT=12.300000
OPTION_STR=[abcdefg]
I (294) main_task: Returned from app_main()

ESP_igrr
Posts: 2076
Joined: Tue Dec 01, 2015 8:37 am

Re: Passing variables when compiling/building project

Postby ESP_igrr » Fri Jan 30, 2026 3:11 pm

so that parameters such as IP address, device name, and password can be changed without editing my code.
And yet another option, if you want to avoid even rebuilding the app when these variables change, you can use a component I wrote:
https://components.espressif.com/compon ... vs-dotenv/

With it, you can put the variables into a .env file in your project directory. The content of .env will be converted into an NVS partition binary at build time, which will be uploaded to the device along with your application. In the application you can then call the standard getenv function to read these values.

CatalinM
Posts: 4
Joined: Sat Dec 28, 2024 5:22 pm

Re: Passing variables when compiling/building project

Postby CatalinM » Sat Feb 07, 2026 10:05 am

The canonical way in IDF is via Kconfig: https://docs.espressif.com/projects/esp ... guide.html
It is and it works great for settings that rarely change, but my use case was slightly different - I want to start the ESP without WiFi in "production" and only turn it on when receiving a hardware signal. However, when developing I always want WiFi on so I can test stuff. It's easier to config this from the CLI when I run `idf.py build flash monitor` instead of going to Kconfig to change settings.

MicroController
Posts: 2661
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Passing variables when compiling/building project

Postby MicroController » Sat Feb 07, 2026 10:39 am

I want to ... in "production" [...] However, when developing I always want ...
You may want to check out Cmake Build Configurations.

Who is online

Users browsing this forum: Baidu [Spider], Bing [Bot], Bytespider, ChatGPT-User and 7 guests