Create a Project in VSCode - mriksman/esp-idf-homekit GitHub Wiki

Create a Project

To begin, you just need the following files;

.vscode
  c_cpp_properties.json
  settings.json
  tasks.json
main
  app_main.c
  CMakeLists.txt
CMakeLists.txt

CMakeLists.txt

 cmake_minimum_required(VERSION 3.5)
 include($ENV{IDF_PATH}/tools/cmake/project.cmake)
 project(whatever_name)

main\app_main.c

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
void app_main(void) {
}

main\CMakeLists.txt

idf_component_register(
 SRC_DIRS "."
 INCLUDE_DIRS "."
)

VSCode Settings

You need to have esp-idf tools and xtensa-esp32-elf bin folders in the PATH environment variable. But as this can change depending on whether you are working on ESP8266 or ESP32, create a settings.json file. Note you cannot use this with a Workspace; open the project folder directly. The first time the Terminal loads, it may ask you if you want to add the environment variables.

.vscode\settings.json

{
   "terminal.integrated.env.windows\": {
      "PATH": "${env:PATH}C:\\path\\to\\esp32\\esp-idf\\tools;C:\\path\\to\\esp32\\xtensa-esp32-elf\\bin;"   
   }
}

For IntelliSense to work, you can list all the folders that will have the includes, or, you can simply use the compile_commands.json that is created after a build; the caveat being IntelliSense won't work until after a build attempt. I had issues with standard libraries (stdio.h) and even the sdkconfig.h not showing, so have added the fallback paths to includePath.

.vscode\c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Win32",
            "cStandard": "c11",
            "cppStandard": "c++11",
            "compilerPath": "",
            "includePath": [
                "${workspaceFolder}/**",
                "C:\\path\\to\\esp32\\**"
            ],
            "compileCommands": "${workspaceFolder}/build/compile_commands.json"
        }
    ],
    "version": 4
}

After performing an idf.py menuconfig and setting the configuration, sdkconfig will be created. When the build process is performed, sdkconfig.h will be created and placed in the build folder.

idf.py menuconfig doesn't work in VS Code Terminal -- the arrow keys don't work. A Pull Request has been made, but for now, run idf menuconfig in a separate PowerShell window. To do this, you can create a Task that opens a separate window; see Menuconfig task below.

To access the Task list, press CTRL + ALT + T.

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Build - Build project",
            "command": "idf.py build",
            "type": "shell",
            "problemMatcher": {
                "owner": "cpp",
                "fileLocation": "absolute",
                "pattern": {
                    "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                    "file": 1,
                    "line": 2,
                    "column": 3,
                    "severity": 4,
                    "message": 5
                },
            },
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        {
            "label": "Flash - Flash the device",
            "type": "shell",
            "dependsOn": "Build - Build project",
            "command": "idf.py flash",
            "problemMatcher": []
        },
        {
            "label": "Monitor - Flash and Monitor",
            "type": "shell",
            "dependsOn": "Flash - Flash the device",
            "command": "idf.py monitor",
            "problemMatcher": []
        },
        {
            "label": "Clean - Clean the project",
            "command": "idf.py fullclean",
            "type": "shell",
            "problemMatcher": []
        },
        {
            "label": "Menuconfig",
            "type": "shell",
            "windows": {
                "command": "Start-Process",
                "args": [
                    "Powershell",
                    "-Command idf.py menuconfig"
                ]
            },
            "presentation": {
                "reveal": "never"
            },
            "problemMatcher": []
        }
    ]
}

The problemMatcher will show up problems in the Problems tab.

Main Source Files

The build system provides special treatment to the main component. It is a component that gets automatically added to the build provided that it is in the expected location, ${PROJECT_PATH}/main. All other components in the build are also added as its dependencies. Renaming the main component causes the loss of these behind-the-scenes heavy lifting, requiring the user to specify the location of the newly renamed component and manually specifying its dependencies. Specifically, the steps to renaming main are as follows:

  1. Rename main directory.
  2. Set EXTRA_COMPONENT_DIRS in the project CMakeLists.txt to include the renamed main directory.
  3. Specify the dependencies in the renamed component's CMakeLists.txt file via COMPONENT_REQUIRES or COMPONENT_PRIV_REQUIRES.

Components Directory for Library

No need to set implicitly include library directory, if it is named components. The variable COMPONENT_DIRS defaults to ${IDF_PATH}/components${PROJECT_PATH}/components, and EXTRA_COMPONENT_DIRS. Override this variable if you don't want to search for components in these places. You can use set(EXTRA_COMPONENT_DIRS directory) to add additional directories.