VSCode + ESP-IDF + Component Dev Env

YovIru
Posts: 2
Joined: Wed Dec 18, 2024 11:30 am

VSCode + ESP-IDF + Component Dev Env

Postby YovIru » Wed Dec 18, 2024 11:49 am

Hello!

Can please someone share best practice to develop components as standalone projects to share among multiple projects?

My current approach works (compiles und runs) but some troubles with IntelliSense stuff and some warnings.

I configured multi-root environment: main component and my custom shared components in one ide window.
  1. project-root/
  2. ├── project-1/
  3. │   ├── CMakeLists.txt             # Main project CMake configuration
  4. │   ├── main/                      # Application's main component
  5. │   │   ├── CMakeLists.txt         # CMake for main component
  6. │   │   ├── main.c                 # Example source file
  7. │   │   └── (other source files)
  8. │   ├── build/                     # Generated build files (after `idf.py build`)
  9. │   ├── sdkconfig                  # Configuration for ESP-IDF project
  10. │   └── project-1.code-workspace   # VSCode workspace file
  11. ├── my_shared_components/
  12. │   ├── components/
  13. │   │   ├── component-1/           # Custom component 1
  14. │   │   │   ├── CMakeLists.txt     # CMake for component-1
  15. │   │   │   ├── include/           # Public headers for component-1
  16. │   │   │   │   └── component1.h
  17. │   │   │   └── src/               # Source files for component-1
  18. │   │   │       └── component1.c
  19. │   │   ├── component-2/           # Custom component 2
  20. │   │   │   ├── CMakeLists.txt     # CMake for component-2
  21. │   │   │   ├── include/           # Public headers for component-2
  22. │   │   │   │   └── component2.h
  23. │   │   │   └── src/               # Source files for component-2
  24. │   │   │       └── component2.c
  25. │   └── (other custom components)
  26.  
project-1.code-workspace looks as follows:
  1. {
  2.     "folders": [
  3.         {
  4.             "path": "."
  5.         },
  6.         {
  7.             "path": "../my_shared_components"
  8.         }
  9.     ],
  10.     "settings": {
  11.     }
  12. }
Compiling works. I can use my custom components in my main component without problems.

But I have nasty warnings while editing shared component's code like this:

Code: Select all

#include errors detected. Consider updating your compile_commands.json or includePath. Squiggles are disabled for this translation unit...

Code: Select all

cannot open source file "portmacro.h"

Code: Select all

ESP-IDF Hint: esp_mac.h header file is not included by esp_system.h anymore. It shall then be manually included with #include "esp_mac.h"

Code: Select all

cannot open source file "esp_log.h" (dependency of "component1.h")
I there any other recommended approach?

Thanks

ESP_bignacio
Posts: 232
Joined: Wed May 02, 2018 12:12 pm

Re: VSCode + ESP-IDF + Component Dev Env

Postby ESP_bignacio » Wed Dec 25, 2024 1:34 am

What are you using for Language syntax ? Microsoft C/C++ extension ? What is the content of your .vscode/c_cpp_properties.json configuration ?

Does your main/CMakeLists.txt including SRCS the list of files used by your components ?

uC_Jon
Posts: 22
Joined: Fri Jan 05, 2024 7:03 pm

Re: VSCode + ESP-IDF + Component Dev Env

Postby uC_Jon » Mon Jan 06, 2025 2:02 pm

I'm currently playing around with component development.

Within my .code-workspace file I have the following:

Code: Select all

"folders": [
		{
			"name": "test-apps/i2c-mcp9808",
			"path": "./test-apps/i2c-mcp9808"
		},
		{
			"name": "ComponentTests",
			"path": "./components/i2c-mcp9808/test"
		},
		{
			"name": "examples/i2c-mcp9808",
			"path": "./components/i2c-mcp9808/examples/i2c-mcp9808/"
		},
		{
			"name": "components/i2c-mcp9808",
			"path": "./components/i2c-mcp9808"
		},
		{
			"name": "docs",
			"path": "./docs"
		},
		{
			"name": "/",
			"path": "."
		}
	],
Then within each directory that is an "application" I have a .vscode/c_cpp_properties.json file that has the following:

Code: Select all

{
    "configurations": [
        {
            "name": "ESP-IDF",
            "compilerPath": "${config:idf.toolsPath}/tools/riscv32-esp-elf/esp-14.2.0_20241119/riscv32-esp-elf/bin/riscv32-esp-elf-gcc",
            "compileCommands": "${config:idf.buildPath}/compile_commands.json",
            "includePath": [
                "${config:idf.espIdfPath}/components/**",
                "${config:idf.espIdfPathWin}/components/**",
                "${workspaceFolder}/**"
            ],
            "browse": {
                "path": [
                    "${config:idf.espIdfPath}/components",
                    "${config:idf.espIdfPathWin}/components",
                    "${workspaceFolder}"
                ],
                "limitSymbolsToIncludedHeaders": true
            }
        }
    ],
    "version": 4
}


That way all the "applications" (the examples and the test-apps) all point to their own folder where the application projects are built.

Within the "component" directory I had to make a choice, do I want to look at the built "example" application or the built "test-apps" application.

In the end I decided the examples application would be sufficient for code completion etc. But only because the example calls all the functions of the component. If the example had been a limited subset of the component functions with the testing calling all the functions then I'd have used that application instead.

So in .vscode/c_cpp_properties.json within the component directory I point to the examples application build directory (note the :examples/i2c-mcp9808 after the ${workspaceFolder ).

Code: Select all

{
    "configurations": [
        {
            "name": "ESP-IDF",
            "compileCommands": "${workspaceFolder:examples/i2c-mcp9808}/build/compile_commands.json"
        }
    ],
    "version": 4
}
Its not perfect because you must have a build created so new files/code/headers may not be included in the compile_commands.json file but it does the job. I think its a limitation of the vscode "c" prompting/code completion and makefiles and gubbins so its outside the realms of esp-idf. I'm quite new, but the makefiles seem to be quite "empty" in comparison to others I've seen so I'm not sure if the alternative way (based on makefiles not the compile commands) would work. I gave up looking in to it as making the components point to the application build worked for me.

Edited to add. I have used the component yaml system with paths coded in them so they are real components very similar to the ones on the espressif component website. Then in projects (not the component development) that might use my component my yaml points to the github repository for the components. In that way each component (temp sensors/external rtc battery clock with memory store) I'm developing is developed entirely stand alone and not contained within my larger project that mashes them all together (pulling from multiple repositories using idf component yamls) for a wall mounted central heating switch/display.

YovIru
Posts: 2
Joined: Wed Dec 18, 2024 11:30 am

Re: VSCode + ESP-IDF + Component Dev Env

Postby YovIru » Wed Jan 08, 2025 7:33 am

ESP_bignacio wrote:
Wed Dec 25, 2024 1:34 am
What are you using for Language syntax ? Microsoft C/C++ extension ? What is the content of your .vscode/c_cpp_properties.json configuration ?
For now I have only one ".vscode/c_cpp_properties.json" under "project-1". The folder with components doesn't have ".vscode" at all.

Code: Select all

{
    "configurations": [
        {
            "name": "ESP-IDF Win32",
            "cStandard": "c11",
            "cppStandard": "c++23",
            "forcedInclude": [
                "${config:idf.buildPath}/config/sdkconfig.h"
            ],
            "compilerPath": "${config:idf.toolsPathWin}\\tools\\xtensa-esp-elf\\esp-13.2.0_20240530\\xtensa-esp-elf\\bin\\xtensa-esp32-elf-gcc.exe",
            "compileCommands": "${config:idf.buildPath}/compile_commands.json",
            "includePath": [
                "${config:idf.espIdfPathWin}/components/**",
                "${workspaceFolder}/**"
            ],
            "browse": {
                "path": [
                    "${config:idf.espIdfPathWin}/components",
                    "${workspaceFolder}"
                ],
                "limitSymbolsToIncludedHeaders": true
            },
            "intelliSenseMode": "windows-gcc-x86"
        }
    ],
    "version": 4
}
ESP_bignacio wrote:
Wed Dec 25, 2024 1:34 am
Does your main/CMakeLists.txt including SRCS the list of files used by your components ?
No. Main component's CMakeLists.txt looks like this:

Code: Select all

idf_component_register(SRCS "main.cpp"
                    INCLUDE_DIRS ".")
Project's CMakeLists.txt - one level above - looks like this:

Code: Select all

cmake_minimum_required(VERSION 3.16)
set(EXTRA_COMPONENT_DIRS "../my_shared_components/components")
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(project-1)

Who is online

Users browsing this forum: No registered users and 14 guests