'Unable to compile ESP-IDF example project

I'm trying to compile my first ESP32 example project. I set up Visual Studio Code with all the latest tools. (Python, toolschain, etc). I'm not sure what exactly I need as I'm new in this environment, so maybe I missed something.

I followed the user guide on how to create and compile the first project, I used the Blink project from the example folder.

Here is what I get.

> Executing task: cmake -G Ninja .. <

-- Project is not inside a git repository, or git repository has no commits; will not use 'git describe' to determine PROJECT_VER.
-- Building ESP-IDF components for target esp32s2
-- Checking Python dependencies...
Python requirements from C:\Users\dmitryke\esp\esp-idf\requirements.txt are satisfied.
-- Project sdkconfig file C:/ESP32_projects/blink/sdkconfig
Loading defaults file C:/ESP32_projects/blink/sdkconfig.defaults...
-- Components:
-- Component paths:
-- Configuring done
-- Generating done
-- Build files have been written to C:/ESP32_projects/blink/build

The terminal will be reused by tasks, press any key to close it.

> Executing task: cmake --build . <

[1/1] Linking C executable blink.elf
FAILED: blink.elf 
cmd.exe /C "cd . && C:\Users\dmitryke\.espressif\tools\xtensa-esp32s2-elf\esp-2020r3-8.4.0\xtensa-esp32s2-elf\bin\xtensa-esp32s2-elf-gcc.exe -mlongcalls   CMakeFiles/blink.elf.dir/project_elf_src.c.obj  -o blink.elf  -Wl,--cref -Wl,--Map=C:/ESP32_projects/blink/build/blink.map  -fno-rtti  -fno-lto && cd ."
c:/users/dmitryke/.espressif/tools/xtensa-esp32s2-elf/esp-2020r3-8.4.0/xtensa-esp32s2-elf/bin/../lib/gcc/xtensa-esp32s2-elf/8.4.0/../../../../xtensa-esp32s2-elf/bin/ld.exe: c:/users/dmitryke/.espressif/tools/xtensa-esp32s2-elf/esp-2020r3-8.4.0/xtensa-esp32s2-elf/bin/../lib/gcc/xtensa-esp32s2-elf/8.4.0/../../../../xtensa-esp32s2-elf/lib/no-rtti/crt0.o:(.literal+0x0): undefined reference to `main'
collect2.exe: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
The terminal process "C:\windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command cmake --build ." terminated with exit code: 1.

Terminal will be reused by tasks, press any key to close it.

I removed everything from the blink.c to avoid code errors.

blink.c

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "sdkconfig.h"

/* Can use project configuration menu (idf.py menuconfig) to choose the GPIO to blink,
   or you can edit the following line and set a number here.
*/
#define BLINK_GPIO 0x01

void app_main(void)
{
    while(1) {    }
}

Another issue, there is an option to run idf.py menuconfig. Sometimes it works and sometimes it says:

C:\ESP32_projects\blink>idf.py menuconfig
'idf.py' is not recognized as an internal or external command,
operable program or batch file.

Any help will be appreciated!



Solution 1:[1]

All ESP IDF tasks (cmake, idf.py, etc) must be run inside a prepared environment. I would start by losing the VSC (because it's just another potential failure point) and using the basic ESP IDF command prompt. Try running idf.py menuconfig, idf.py build and idf.py -p COMx flash for configuring, building and flashing your test project.

Once you've verified that the ESP IDF environment and your project works, go back to VSC and see what's wrong there.

As a working example, this is how I compile the same sample project on my machine (Linux, ESP IDF v4.2.1) from scratch:

$ cp -r ~/esp-idf/examples/get-started/blink/ .
$ cd blink
$ idf.py build
Running cmake in directory /home/tarmo/tmp/espidftest/blink/build                                                                                                                                                  
Executing "cmake -G Ninja -DPYTHON_DEPS_CHECKED=1 -DESP_PLATFORM=1 -DCCACHE_ENABLE=0 /home/tarmo/tmp/espidftest/blink"... 
-- Found Git: /usr/bin/git (found version "2.30.2")                                                                                                                                                                
-- IDF_TARGET not set, using default target: esp32                                                                                                                                                                 
-- The C compiler identification is GNU 8.4.0                                                                                                                                                                      
-- The CXX compiler identification is GNU 8.4.0   

[SNIP]

esptool.py v3.0                                                                                                                                                                                                    
Generated /home/tarmo/tmp/espidftest/blink/build/bootloader/bootloader.bin                                                                                                                                         
[934/934] Generating binary image from built executable                                                                                                                                                            
esptool.py v3.0                                                                                                                                                                                                    
Generated /home/tarmo/tmp/espidftest/blink/build/blink.bin                                                                                                                                                         
                                                                                                                                                                                                                   
Project build complete. To flash, run this command:                                                                                                                                                                
/home/tarmo/espressif/python_env/idf4.2_py3.9_env/bin/python ../esp-idf/components/esptool_py/esptool/esptool.py -p (PORT) -b 921600 --before default_reset
 --after hard_reset --chip esp32  write_flash --flash_mode dio --flash_size detect --flash_freq 40m 0x1000 build/bootloader/bootloader.bin 0x8000 build/partition_table/partition-table.bin 0x10000 build/blink.bin
or run 'idf.py -p (PORT) flash'
$ ls -lha
drwxr-xr-x 8 tarmo tarmo 4,0K juuni  4 12:26 build
-rw-r--r-- 1 tarmo tarmo  234 juuni  4 12:25 CMakeLists.txt
-rw-r--r-- 1 tarmo tarmo 1,5K juuni  4 12:25 example_test.py
drwxr-xr-x 3 tarmo tarmo 4,0K juuni  4 12:26 main
-rw-r--r-- 1 tarmo tarmo  177 juuni  4 12:25 Makefile
-rw-r--r-- 1 tarmo tarmo  157 juuni  4 12:25 README.md
-rw-r--r-- 1 tarmo tarmo  33K juuni  4 12:25 sdkconfig
-rw-r--r-- 1 tarmo tarmo    3 juuni  4 12:25 sdkconfig.defaults

Solution 2:[2]

The command window is less concerning, i got it working. My main problem is main not being linked.

Solution 3:[3]

I had the same problem with building the ESP-project in VS Code, but found a solution: The project needs to be created in a folder with a different name than the example name.

I tried the blink project and it works when creating it in c:\esp-idf\ rather than in c:\esp-idf\blink. The latter will make a folder named blink within the blink folder.

Solution 4:[4]

You can add this code to your main(), checking your esp-idf environment first:

    gpio_reset_pin(BLINK_GPIO);
    gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);
    while(1) {
        printf("Turning off the LED\n");
        gpio_set_level(BLINK_GPIO, 0);
        vTaskDelay(1000 / portTICK_PERIOD_MS);
        printf("Turning on the LED\n");
        gpio_set_level(BLINK_GPIO, 1);
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }

Don't forget to close the previous terminal and open it again.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Dmitry Kezin
Solution 3 EvenLund
Solution 4 Yorvin