idf.py build ignores defines in c_cpp_settings.json

Aleksei
Posts: 4
Joined: Sat Sep 10, 2022 3:11 pm

idf.py build ignores defines in c_cpp_settings.json

Postby Aleksei » Sat Sep 10, 2022 3:32 pm

Hello
I have a problem with compiling my project. I've created some debug configuration for my project in c_cpp_settings.json and there is a "__DEBUG" item in sections defines. But when I try to check it in my main.cpp file I see that no __DEBUG directive has been added (#else branch works):

extern "C" void app_main(void)
{
#ifdef __DEBUG
Target = TargetBase::CreateInstance(DEVICE_TYPE);
#else
Target = TargetBase::CreateInstance();
#endif
// Some code below
}

My c_cpp_settings.json:
{
"configurations": [
{
"name": "Debug",
"compilerPath": "C:\\esp\\tools\\xtensa-esp32-elf\\esp-2022r1-RC1-11.2.0\\xtensa-esp32-elf\\bin\\xtensa-esp32-elf-gcc.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"compileCommands": "${workspaceFolder}/build/compile_commands.json",
"includePath": [
"${config:idf.espIdfPathWin}/components/**",
"${workspaceFolder}/**"
],
"defines": [
"__DEBUG"
],
"browse": {
"path": [
"${config:idf.espIdfPathWin}/components",
"${workspaceFolder}"
],
"limitSymbolsToIncludedHeaders": false
},
"intelliSenseMode": "linux-gcc-x86",
"compilerArgs": []
},
],
"version": 4
}

What can I do for solving this problem?

Aleksei
Posts: 4
Joined: Sat Sep 10, 2022 3:11 pm

Re: idf.py build ignores defines in c_cpp_settings.json

Postby Aleksei » Tue Sep 13, 2022 5:13 pm

I've reproduced this problem with example project from Espressif-IDF plugin for VS Code:
1. Eviroment: Windows 10, Visual studio code v1.7.0, Espressif IDF plugin v1.5.0.
2. Create "Hello world" example project (View->Command Pallete->ESP-IDF: Show example projects->hellow_world)
3. In main.c replace string printf("Hello world!\n); with
#ifdef __DEBUG
printf("Hello world debug!\n");
#else
printf("Hello world!\n");
#endif
4. In c_cpp_settings.json add defines section into default configuration "ESP-IDF":
"defines": [
"__DEBUG"
]
5. Build and flash

Expected behavior: "Hello world debug!" printed. Fact: "Hello world!"
Does anybody have an idea? What's wrong?

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

Re: idf.py build ignores defines in c_cpp_settings.json

Postby ESP_igrr » Tue Sep 13, 2022 8:37 pm

Hi Aleksei,

c_cpp_settings.json file only defines settings for code analysis (intellisense) for the MS C/C++ extension.
Build configuration used both by idf.py and IDF extension is determined by the project and component CMake files. If you need to add a compiler definition, you have to use CMake add_compile_definitions or target_compile_definitions functions.

Aleksei
Posts: 4
Joined: Sat Sep 10, 2022 3:11 pm

Re: idf.py build ignores defines in c_cpp_settings.json

Postby Aleksei » Wed Sep 14, 2022 5:36 pm

Hello, ESP_igrr
Thank you for your answer. And one more question. I would like to have two configurations in VS Code - Release and Debug. My aim is to compile respective image when I switch between these configurations. So, my question is: how can I pass parameters to CMake from VSCode configuration? May I ask you to give an example?

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

Re: idf.py build ignores defines in c_cpp_settings.json

Postby ESP_igrr » Thu Sep 15, 2022 6:35 am

ESP-IDF project configuration is normally contained in sdkconfig file. This includes settings such as debug/release. In VS Code you can access these settings via the configuration editor, opened when you click on an sdkconfig file.

However it seems there is also a way to pass additional CMake variables to the build via the idf.cmakeCompilerArgs setting in VSCode. I think you can add a new CMake variable via this setting.

For example, you can add -DVAR=VALUE which will define a CMake variable VAR to the value "VALUE".

Aleksei
Posts: 4
Joined: Sat Sep 10, 2022 3:11 pm

Re: idf.py build ignores defines in c_cpp_settings.json

Postby Aleksei » Sat Sep 17, 2022 8:03 am

Hi ESP_igrr! It seems to me that both of suggested variants are independ of configrations described in c_cpp_properties.json. But at least I can update sdkconfig and add desired define into it. In any case, thank you for your help!

Who is online

Users browsing this forum: No registered users and 20 guests