Adding Source Folders to a C++ Project

SparkyNZ
Posts: 48
Joined: Thu Jan 04, 2024 9:01 pm

Adding Source Folders to a C++ Project

Postby SparkyNZ » Thu Jan 04, 2024 9:40 pm

I'm quite new to IDF and CMake in general. I'm trying to add the FabGL source files to a dummy (blink example) C++ project. My folder structure looks like this:

Code: Select all

paul_fabgl
|   CMakeLists.txt
|   ...
|   
+---fabgl_src
|   |   canvas.cpp
|   |   canvas.h
|   |   CMakeLists.txt
|   |   ...
|           
+---main
|       blink_example_main.c
|       CMakeLists.txt
|       idf_component.yml
|       Kconfig.projbuild
My top level CMakeLists.txt file is like this:

Code: Select all

cmake_minimum_required(VERSION 3.16)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
include(${CMAKE_CURRENT_SOURCE_DIR}/fabgl_src/CMakeLists.txt)
project(paul_fabgl)
Is this the correct way to include a subfolder of source files?

The CMakeLists.txt file within fabgl_src looks like this:

Code: Select all

idf_component_register(SRC_DIRS "."
                                comdrivers
                                devdrivers
                                dispdrivers
                                emudevs
                                network
                       INCLUDE_DIRS "."
                                fonts
                                images
                                comdrivers
                                devdrivers
                                dispdrivers
                                emudevs
                                network
                       REQUIRES fatfs
                                spiffs
                                esp_adc_cal
                                ulp
                                nvs_flash
                                soc)
Within VSCode, when I save this file, it tries to configure the project and I get the following error:

Code: Select all

[cmake] CMake Error at C:/esp32/frameworks/esp-idf-v5.1.2/tools/cmake/component.cmake:439 (message):
[cmake]   Called idf_component_register from a non-component directory.
[cmake] Call Stack (most recent call first):
[cmake]   fabgl_src/CMakeLists.txt:1 (idf_component_register)
Should I be using idf_component_register to include the FabGl source folders, should I be including them in a different way, or do I need to do something else?

I don't understand what idf_component_register is actually for - all I want to do is compile a bunch of source as one monolithic project for now.

SparkyNZ
Posts: 48
Joined: Thu Jan 04, 2024 9:01 pm

Re: Adding Source Folders to a C++ Project

Postby SparkyNZ » Thu Jan 04, 2024 10:23 pm

How exactly do you add source files in a folder to an IDF C++ project? I've seen examples like this for general CMake:

Code: Select all

file(GLOB all_SRCS
        "${PROJECT_SOURCE_DIR}/include/*.h"
        "${PROJECT_SOURCE_DIR}/include/*.hpp"
        "${PROJECT_SOURCE_DIR}/src/*.cpp"
        "${PROJECT_SOURCE_DIR}/src/*.c"
        )

add_executable(MyTarget ${all_SRCS})
Does the same apply?

SparkyNZ
Posts: 48
Joined: Thu Jan 04, 2024 9:01 pm

Re: Adding Source Folders to a C++ Project

Postby SparkyNZ » Thu Jan 04, 2024 10:47 pm

Are the below CMake commands actually used in an IDF project?

add_executable()
add_library()


Looking at the CMakeLists.txt from the hello_world example:

Code: Select all

cmake_minimum_required(VERSION 3.16)

include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(hello_world)
..there is no add_executable() here. Should there be?

If you were to take the hello_world project and add a subfolder with some additional source files - would you add those files as a library?

I don't understand if CMake is able to extend the top CMakeLists.txt so that it builds top source files plus source files in a specified sub folder (monolithic inclusion of .cpp files), or whether source files in sub folders must be libraries?

Is the IDF environment doing anything different than what would be expected in any other C++ project environment that you'd use to build a typical C++ program?

MicroController
Posts: 1706
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Adding Source Folders to a C++ Project

Postby MicroController » Fri Jan 05, 2024 12:49 pm

https://docs.espressif.com/projects/esp ... ystem.html

With the ESP-IDF, static libraries are rarely used (explicitly). Instead, IDF introduces the concept of 'components' (see https://docs.espressif.com/projects/esp ... ists-files)
Your application (main) is also a component. Each component has its own CMakeLists.txt and uses idf_component_register(...) to specify 1. which source files are to be compiled as part of the component and 2. (recursively) which other components are required to be built before the current component can be built because this component depends on them.

You can choose to either
a) add any (third-party) source files to the SRCS in your application's CMakeLists.txt, or
b) make the third-party code a custom component, by creating a dedicated CMakeLists.txt with idf_component_register(...) in it, and declare that your application REQUIRES this component.

SparkyNZ
Posts: 48
Joined: Thu Jan 04, 2024 9:01 pm

Re: Adding Source Folders to a C++ Project

Postby SparkyNZ » Fri Jan 05, 2024 8:12 pm

@MicroController thanks for that. I'll go read about the Build System overall. I did get some help regarding vanilla CMake too so that should help me in both IDF and non-IDF environments. It's good to see the Build System section mentions FreeRTOS as an example because that's something else I need to include in my project.

MicroController
Posts: 1706
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Adding Source Folders to a C++ Project

Postby MicroController » Sat Jan 06, 2024 12:20 am

SparkyNZ wrote:
Fri Jan 05, 2024 8:12 pm
It's good to see the Build System section mentions FreeRTOS as an example because that's something else I need to include in my project.
Don't bother ;-) FreeRTOS and a bunch of other basic components are always implicitly included in every IDF build.

SparkyNZ
Posts: 48
Joined: Thu Jan 04, 2024 9:01 pm

Re: Adding Source Folders to a C++ Project

Postby SparkyNZ » Sat Jan 06, 2024 12:25 am

Yeah.. I noticed the FreeRTOS in the output window, thanks. :D I think the FabGL source was built with an older IDF distro.. so it's going to take a bit of fiddling to get it to compile.

MicroController
Posts: 1706
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Adding Source Folders to a C++ Project

Postby MicroController » Sat Jan 06, 2024 1:03 am

Seeing that FabGL is already an IDF component, you may want to just try using it as such, i.e. in your project directory, create a directory named 'components' (next to 'main'). In this components directory, checkout/put the whole FabGL into its own subdirectory, so that you end up with something like myproject/components/fab_gl/CMakeLists.txt.
Then add the name of the FabGL subdirectory you created (e.g. "fab_gl") to the REQUIRES in your main's idf_component_register(...).
Hit "build" and keep your fingers crossed...

SparkyNZ
Posts: 48
Joined: Thu Jan 04, 2024 9:01 pm

Re: Adding Source Folders to a C++ Project

Postby SparkyNZ » Sat Jan 06, 2024 1:16 am

Yeah I did that in the end. It was a lot easier than I thought. It configures and builds somewhat.. I just have to work through the deprecations and changes between IDF versions etc.. and fill in my gaps of CMake and IDF knowledge.

mtraven
Posts: 28
Joined: Thu Jul 07, 2022 3:34 am

Re: Adding Source Folders to a C++ Project

Postby mtraven » Mon Mar 18, 2024 3:27 am

were you able to get fabgl working on an IDF build? Was that inside platformio, or just a raw c++ project?

I've been scratching my head trying to get fabgl working with either platforio-IDF or normal ESP-IDF....nothings working for me.

Who is online

Users browsing this forum: mln2007 and 93 guests