3

I started my project with a simple "blink" example and used it as a template to write my code. This example used only one source file blink.c. Eventually, I want to a use multi source files project and can't figure out how to configure CMakeLists.txt in order to compile the project.

My CMakeLists.txt is:

cmake_minimum_required(VERSION 3.5)

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

project(blink)

I want to add for example init.c. I tried different ways, but with no success.

None of idf_component_register() / register_component() worked for me.

Any idea how to correctly configure the project?

Dmitry Kezin
  • 105
  • 1
  • 3
  • 10

2 Answers2

4

Right, the CMake project hierarchy in ESP IDF is a bit tricky. You are looking at the wrong CMakeLists.txt file. Instead of the one in root directory, open the one in blink/main/CMakeLists.txt. This file lists the source files for the "main" component, which is the one you want to use. It would look like this:

idf_component_register(SRCS "blink.c" "init.c"
                    INCLUDE_DIRS ".")

Make sure your init.c file is in the same directory as this CMakeLists.txt and blink.c.

I also recommend taking a look at the Espressif Build System documentation, it's quite useful.

Tarmo
  • 3,728
  • 1
  • 8
  • 25
1

You should edit the CMakeLists.txt located in your main folder inside your project folder. In addition, you need to put the directory that contains the header files into INCLUDE_DIRS parameter.

For example, if you have this file structure in your project (you're putting init.h inside include folder) as shown below:

blink/
├── main/
│   ├── include/
│   │   └── init.h
│   ├── blink.c
│   ├── CMakeLists.txt
│   ├── init.c
│   └── ...
├── CMakeLists.txt
└── ...

The content in your main/CMakeLists.txt should be:

idf_component_register(SRCS "blink.c" "init.c"
                    INCLUDE_DIRS "." "include")