CMakeLists.txt Questions - Errors & Components

ignisuti
Posts: 17
Joined: Wed Nov 15, 2023 1:29 pm

CMakeLists.txt Questions - Errors & Components

Postby ignisuti » Sun Nov 17, 2024 6:29 am

I'm new to the ESP-IDF and trying to teach myself about CMake.

Question 1:
I'm creating a new project using the example "nvs_rw_value" as a template. I've started porting some of that code to a folder located here: main/Middleware/Tools/NONVOL.c.
When I build, I get an error saying "nvs_flash.h: No such file or directory"
There was no problem with including nvs_flash.h with the fresh example project that had this code was in the main directory.

Here is my top-level CMakeLists.txt file:

Code: Select all

cmake_minimum_required(VERSION 3.16)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(project)
enable_language(C)

set(SOURCE_FILES
	main/APP.c
	main/APP.h
	main/APP__CONFIG.h
	main/Middleware/Tools/DEBUG.c)
	
add_executable(${PROJECT_NAME} ${SOURCE_FILES})

target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/)
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/Middleware/Tools)
I thought the "include($ENV{IDF_PATH}/tools/cmake/project.cmake)" portion of this file would allow the compiler to know where to find nvs_flash.h...?

Question 2:
Why do I need a separate CMakeLists.txt for each subdirectory (I guess these are called "Components")?
What do I put in these files? Do I just list all my .C files for that subdirectory?

ignisuti
Posts: 17
Joined: Wed Nov 15, 2023 1:29 pm

Re: CMakeLists.txt Questions - Errors & Components

Postby ignisuti » Sun Nov 17, 2024 6:44 am

Also I should add that when I modify the CMakeLists.txt files and try to rebuild the project I get this error: "'Building Active Configuration' has encountered a problem". Cleaning the project doesn't help.

What works most of the time is closing the Espressif Eclipse IDE and re-opening it.

Occasionally, that doesn't work either, and then I have to delete & re-import the project to get it working again.

ignisuti
Posts: 17
Joined: Wed Nov 15, 2023 1:29 pm

Re: CMakeLists.txt Questions - Errors & Components

Postby ignisuti » Tue Nov 19, 2024 4:40 am

I'm still stuck on this and could use some assistance.
I've read through the Espressif Build System guide and tons of other resources online including paying for a subscription to UDemy which has some great videos on the ESP32. But, I still can't figure out why this isn't working!

Here's my file structure:
Main >
-APP.c
-APP.h
-APP__CONFIG.h

Main / Middleware >
-NONVOL.c
-NONVOL.h
-TYPES.h

test_project / CMakeLists.txt
# The following 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.16)

set (EXTRA_COMPONENT_DIRS "main" "main/Middleware")
#list(APPEND EXTRA_COMPONENT_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/../Middleware")
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
#include_directories("${CMAKE_CURRENT_SOURCE_DIR}/Middleware")

project(test_project)
enable_language(C)

set(SOURCE_FILES
main/APP.c
main/Middleware/NONVOL.c)
add_executable(${PROJECT_NAME} ${SOURCE_FILES})

test_project / main / CMakeLists.txt
idf_component_register(SRCS "APP.c"
INCLUDE_DIRS ".")

test_project / main / Middleware / CMakeLists.txt
idf_component_register(SRCS "NONVOL.c"
INCLUDE_DIRS ".")

Start of APP.c
/*--------------------------------------------------------------------
INCLUDES
--------------------------------------------------------------------*/
// Standard library header files
#include <stdio.h>
#include <inttypes.h>

// ESP-IDE Header files
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "nvs_flash.h"
#include "nvs.h"

// Middleware header files
#include "main/Middleware/TYPES.h"

// Application header files
#include "APP.h"

Start of NONVOL.c
/*--------------------------------------------------------------------
INCLUDES
--------------------------------------------------------------------*/
#include "main/APP__CONFIG.h"
#include "main/Middleware/NONVOL.h"

Errors:
FAILED: CMakeFiles/test_project.dir/main/Middleware/NONVOL.c.obj
D:/PlaneAC/04_Firmware/Firmware_TEST/main/Middleware/NONVOL.c:12:10: fatal error: main/APP__CONFIG.h: No such file or directory
12 | #include "main/APP__CONFIG.h"

FAILED: CMakeFiles/test_project.dir/main/APP.c.obj
D:/PlaneAC/04_Firmware/Firmware_TEST/main/APP.c:17:10: fatal error: freertos/FreeRTOS.h: No such file or directory
17 | #include "freertos/FreeRTOS.h"

ignisuti
Posts: 17
Joined: Wed Nov 15, 2023 1:29 pm

Re: CMakeLists.txt Questions - Errors & Components

Postby ignisuti » Tue Nov 19, 2024 4:39 pm

Okay, I made some guesses and updated CMakeLists.txt files as follows.

test_project / main / CMakeLists.txt
idf_component_register(SRCS "APP.c"
INCLUDE_DIRS "." "Middleware")

test_project / main / Middleware / CMakeLists.txt
idf_component_register(SRCS "NONVOL.c"
INCLUDE_DIRS "." "..")

That appears to have fixed issues with the code I've added, but it appears that the compiler still does not know how to find the ESP-IDF components included from NONVOL.h.

So, I think we can summarize my question now to the following:
Why are would #include "nvs.h" be acceptable if NONVOL.h were located in the main folder, but not be acceptable when NONVOL.h is located in the main/Middleware folder?

Also... it looks like the components direction is only 300MB. Why not just copy these files to my project? Is that something some developers choose to do? If not, why is it a bad idea? I think doing it this way would allow Eclipse to link/index to the files more easily.
My next step will be to compile that into a library and then include the library so full clean rebuilds are quicker.

chegewara
Posts: 2375
Joined: Wed Jun 14, 2017 9:00 pm

Re: CMakeLists.txt Questions - Errors & Components

Postby chegewara » Wed Nov 20, 2024 5:49 am

ignisuti wrote:
Tue Nov 19, 2024 4:39 pm

test_project / main / Middleware / CMakeLists.txt
idf_component_register(SRCS "NONVOL.c"
INCLUDE_DIRS "." "..")
This file makes no sense, as this is subfolder, not component.

Code: Select all

test_project / main / CMakeLists.txt
idf_component_register(SRCS "APP.c" "full/path/to/file.c" "Middleware/Tools/NONVOL.c"
INCLUDE_DIRS "." "Middleware")

ignisuti
Posts: 17
Joined: Wed Nov 15, 2023 1:29 pm

Re: CMakeLists.txt Questions - Errors & Components

Postby ignisuti » Wed Nov 20, 2024 6:12 am

Yes, I am very confused. I thought "component" meant "subfolder"....?

chegewara
Posts: 2375
Joined: Wed Jun 14, 2017 9:00 pm

Re: CMakeLists.txt Questions - Errors & Components

Postby chegewara » Wed Nov 20, 2024 6:18 am

There is few ways to add components in esp-idf, but most basic is to create folder "components" in your projects and put your components into it. Like here:

https://github.com/espressif/esp-iot-br ... components

All folders inside are components.

ESP_igrr
Posts: 2072
Joined: Tue Dec 01, 2015 8:37 am

Re: CMakeLists.txt Questions - Errors & Components

Postby ESP_igrr » Wed Nov 20, 2024 10:30 am

Hi ignisuti,

'main' component has a special behavior that by default, it adds all other components as dependencies. This is the reason why you can use nvs_flash component from "main" without explicitly adding nvs_flash as a dependency. Once you start creating additional components, you have to explicitly specify dependencies of these components using "REQUIRES" and "PRIV_REQUIRES" keywords of idf_component_register.

Here is the relevant part of the documentation: https://docs.espressif.com/projects/esp ... quirements

Here is a video where I tried to explain the concept of component dependencies: https://youtu.be/7utLBxSOXlQ?si=GQu5A21hYqrZuPTR&t=1278

ignisuti
Posts: 17
Joined: Wed Nov 15, 2023 1:29 pm

Re: CMakeLists.txt Questions - Errors & Components

Postby ignisuti » Wed Nov 20, 2024 7:37 pm

Thank you for your responses. I think I have a better understanding now, but will continue to ask questions as I build my project.

In the example CMakeLists.txt file below, can you please tell me where CONFIG_BRIDGE_EXTERNAL_NETIF_STATION and CONFIG_BRIDGE_DATA_FORWARDING_NETIF_SOFTAP would come from?

Code: Select all

set(srcs "src/bridge_common.c")
set(requires "esp_eth" "esp_netif" "esp_wifi" "esp_timer" "nvs_flash" "esp_modem")
set(include_dirs "include")

if (CONFIG_BRIDGE_EXTERNAL_NETIF_STATION OR CONFIG_BRIDGE_DATA_FORWARDING_NETIF_SOFTAP)
    list(APPEND srcs "src/bridge_wifi.c")
endif()

ignisuti
Posts: 17
Joined: Wed Nov 15, 2023 1:29 pm

Re: CMakeLists.txt Questions - Errors & Components

Postby ignisuti » Thu Nov 21, 2024 9:05 pm

I found part of my answer...
The values like CONFIG_BRIDGE_EXTERNAL_NETIF_STATION come from /build/Confifg.sdkconfig.h

But, this is an automatically generated file. So, how do these values get in this file? I assume it's from sdkconfig, but I searched through there and don't see these values. I wonder if maybe the values do come from sdkconfig, but do not line up word-for-word. Is that the case? If so, how do you find the names of the values that I would want to use?

Who is online

Users browsing this forum: No registered users and 39 guests