Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ESP-IDF project with multiple source files

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?

like image 200
Dmitry Kezin Avatar asked Oct 15 '25 18:10

Dmitry Kezin


2 Answers

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.

like image 145
Tarmo Avatar answered Oct 18 '25 09:10

Tarmo


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")
like image 28
Mokhamad Arfan Wicaksono Avatar answered Oct 18 '25 11:10

Mokhamad Arfan Wicaksono