How to create examples from "../.platformio\packages\framework-espidf\examples"

I wanted to try one of the installed esp-idf examples (…/.platformio\packages\framework-espidf\examples).
Naive as I was, I created an empty esp-idf project and copied the example project to the respective folders in the empty pio project incl. my template platformio.ini - but of cause this didn’t work. :face_exhaling:
I have now used too much time trying to overcome the ‘Error: Couldn’t find the main target of the project!’. What is the right way to use/tryout the examples from: “…/.platformio\packages\framework-espidf\examples” in pio/VSCODE?


platform = espressif32
board = nodemcu-32s
board_build.partitions = ESP32_OTA_4M.csv
; board_build.embed_files = src/cert/server_cert.pem
framework = espidf
monitor_speed = 115200
monitor_filters = esp32_exception_decoder
debug_tool = olimex-arm-usb-ocd-h
debug_speed = 8000
build_flags = -DCORE_DEBUG_LEVEL=5


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


# The following five 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)
# (Not part of the boilerplate)
# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection.
set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common)

This is a caveat explained in Use empty ASM and CXX flags if config empty by maxgerhardt · Pull Request #823 · platformio/platform-espressif32 · GitHub, caused by the examples having

When it should be, with PlatformIO’s build logic,

list(APPEND EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common)

then it should work.

Actually all new versions should have a more explicit error message after this commit

If you don’t have that error message, that suggests you’re running an old version of platform-espressif32. CLIpio pkg update -g -p espressif32 to update.

Thanks for the reply (you seem to be my “go-to-guy” - positively meant of cause)! :clap:
I have actually been quite thorough in keeping my dev environment up to date. I did as you suggest and got this:

PS C:\Users\niels\Documents\PlatformIO\Projects\Varmeflytter> pio pkg update -g -p espressif32
Platform Manager: espressif32@5.1.1 is already up-to-date
Tool Manager: framework-espidf@3.40401.0 is already up-to-date
Tool Manager: tool-cmake@3.16.4 is already up-to-date
Tool Manager: tool-esptoolpy@1.30300.0 is already up-to-date
Tool Manager: tool-idf@1.0.1 is already up-to-date
Tool Manager: tool-mconf@1.4060000.20190628 is already up-to-date
Tool Manager: tool-mkfatfs@2.0.1 is already up-to-date
Tool Manager: tool-mklittlefs@1.203.210628 is already up-to-date
Tool Manager: tool-mkspiffs@2.230.0 is already up-to-date
Tool Manager: tool-ninja@1.9.0 is already up-to-date
Tool Manager: tool-openocd-esp32@2.1100.20220706 is already up-to-date
Tool Manager: toolchain-esp32ulp@1.22851.191205 is already up-to-date
Tool Manager: toolchain-xtensa-esp32@8.4.0+2021r2-patch3 is already up-to-date
PS C:\Users\niels\Documents\PlatformIO\Projects\Varmeflytter>

…Then there seems to be a bug in triggering that message. Does the fix in the CMakeLists.txt work regardless?

Yes, this did it! :clap: :clap:

So, one need to modify the ./CMakeLists.txt in order to get the examples to work with platformio or is it a reported issue (sorry, but I didn’t read/understand the complete thread that you referenced)?

Added a new source file to the project in “./src/”. How du you “tell” CMake (i guess) to add this file to the project? Apparently it can’t “see” the new function reference:

Linking .pio\build\nodemcu-32s\firmware.elf
c:/users/niels/.platformio/packages/toolchain-xtensa-esp32/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld.exe: .pio\build\nodemcu-32s\src\tcp_client.o:(.literal.app_main+0x3c): undefined reference to `GetTempTask'
collect2.exe: error: ld returned 1 exit status
*** [.pio\build\nodemcu-32s\firmware.elf] Error 1

If you want to make your life easy, instead of adding each new source file there, you can use a glob expression like *.c to just add any new file to the build, like an example shows.

idf_component_register(SRCS ${MY_SOURCES}
                    INCLUDE_DIRS ".")

That is great, just what I was looking for and I like easy - thanks! :clap: