Cannot run basic "Hello World" example from ESP-IDF extension on Visual Studio Code

Hello, so I just created a new project on visual studio code using the esp-idf hello_world template.

This is the code:

/*
 * SPDX-FileCopyrightText: 2010-2022 Espressif Systems (Shanghai) CO LTD
 *
 * SPDX-License-Identifier: CC0-1.0
 */

#include <stdio.h>
#include <inttypes.h>
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_chip_info.h"
#include "esp_flash.h"
#include "esp_system.h"

void app_main(void) 
{
    printf("Hello world!\n");

    /* Print chip information */
    esp_chip_info_t chip_info;
    uint32_t flash_size;
    esp_chip_info(&chip_info);
    printf("This is %s chip with %d CPU core(s), %s%s%s%s, ",
           CONFIG_IDF_TARGET,
           chip_info.cores,
           (chip_info.features & CHIP_FEATURE_WIFI_BGN) ? "WiFi/" : "",
           (chip_info.features & CHIP_FEATURE_BT) ? "BT" : "",
           (chip_info.features & CHIP_FEATURE_BLE) ? "BLE" : "",
           (chip_info.features & CHIP_FEATURE_IEEE802154) ? ", 802.15.4 (Zigbee/Thread)" : "");

    unsigned major_rev = chip_info.revision / 100;
    unsigned minor_rev = chip_info.revision % 100;
    printf("silicon revision v%d.%d, ", major_rev, minor_rev);
    if(esp_flash_get_size(NULL, &flash_size) != ESP_OK) {
        printf("Get flash size failed");
        return;
    }

    printf("%" PRIu32 "MB %s flash\n", flash_size / (uint32_t)(1024 * 1024),
           (chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external");

    printf("Minimum free heap size: %" PRIu32 " bytes\n", esp_get_minimum_free_heap_size());

    for (int i = 10; i >= 0; i--) {
        printf("Restarting in %d seconds...\n", i);
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
    printf("Restarting now.\n");
    fflush(stdout);
    esp_restart();
}

I have not modified the code in anyway, but I get this error when trying to run the code:

PS C:\Users\Public\ProjectName> cd "c:\Users\Public\ProjectName\main\" ; if ($?) { gcc hello_world_main.c -o hello_world_main } ; if ($?) { .\hello_world_main }
hello_world_main.c:9:10: fatal error: sdkconfig.h: No such file or directory
    9 | #include "sdkconfig.h"
      |          ^~~~~~~~~~~~~
compilation terminated.

I am not sure how to prevent the formatting above from occurring, so here is an image instead:

What am I doing wrong?

This is the file structure:

Hi @Ishan286537

That error that you’re getting is due to that library not being installed.

Depending on your use case using the Arduino IDE may be an easier and more user friendly option for uploading code to an ESP. You would also need to add ESP support to the Arduino IDE, there is a great guide available for doing so that you can find here.

Hi Dan,

I have already programmed my esp32 using Arduino IDE, and I wanted to use a more powerful platform to program my esp32.

Regarding the library not being installed, since this is a template project, shouldn’t the libraries being used have come pre-installed with ESP-IDF?

Anyways, I am able to see sdkconfig.h in my file structure (6th item in the list below), so the library is definitely installed.

Hey @Ishan286537,

The original error message seems to indicate that it expects the file in /main. I have had this issue (or very similar) when working in STM32CubeIDE, and fixing it typically involved adding another default directory to “search” for includes.

AFAIK you can change include directories within “CMakeLists.txt”, can you send us a snippet of the code within the file?

It seems strange that the default installation does not have these include directories setup, so perhaps this is not the root cause of the issue.

Hi Zach,

I tried doing that before posting here on the forum, and then the program throws an error on the next line, for #include “freertos/FreeRTOS.h”, and when I pasted that file too in the “main” folder, it threw an error for another #include inside the freeRTOS.h file, and the chain kept going on, until I decided to post here since there’s no way this is what I should be doing.

There are 2 CMakeLists.txt in my folder:

Here is the code for the one outside of main:

# 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.16)

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

This is the code for the one inside main:

idf_component_register(SRCS "hello_world_main.c"
                    INCLUDE_DIRS "")

From reading online, it seems this problem could be fixed through a lot of

idf_component_register(  REQUIRES )

lines in the CMakeLists.txt inside main.

Unfortunately, I haven’t had much experience with ESP-IDF, so I would struggle to get exact code written out for you. To be honest, that doesn’t sound like an ideal solution anyway.

In my personal experience, this type of problem when using STM32CubeIDE has been due to incorrect project configuration, and has been fixed by reconfiguring the project.

It may be worth trying to set up the project again. If you didn’t use a guide the first time around, try using one for the reattempt. I wouldn’t be surprised if there’s some random setting that configures includes properly.

Never mind, I figured out I was being stupid :confused:

When I clicked “run”, visual studio code was just trying to run the code like it was a normal C++ file, which was causing errors.

What I am supposed to do is build the project and then flash it.

Nonetheless, thanks everyone for the help!

Hey Ishan,

Glad you got it working! Good luck with your project.

1 Like

Thanks!

wait, can you explain is step by step? First time using VS + C + ESP-IDF so I’m extremely lost. I have the same problem.

Hey @G289527, welcome to the forums!

Visual Studio is just an IDE, the program itself, by default, is used to write code and execute it on your computer.

VSCode Extensions are used to add functionality to the basic Visual Studio Setup.

ESP-IDF is a VSCode Extension that allows VSCode to flash a file to an external ESP device, and interface with the hardware directly. This allows VSCode to operate in a very similar way to other microcontroller IDE’s, such as Thonny, Espressif IDE, or Arduino IDE.

@Ishan286537 was accidentally using the “Run” button, which simply runs the code as a normal file on the PC, like VS would normally do. The intended method when using ESP-IDF is to use the “Build” button to build the code locally, and then the “Flash” button to upload the built code to your ESP device.

Both of these buttons should be found on the ESP-IDF toolbar at the bottom of VSCode.

Hope this helps!