How to add all include folders and source files of project in CMakeList.txt

ashigupta9
Posts: 13
Joined: Thu Nov 28, 2019 9:48 am

How to add all include folders and source files of project in CMakeList.txt

Postby ashigupta9 » Fri Mar 19, 2021 3:23 pm

Hello,

I am using ESP-IDV v4.2 on Eclipse along with ESP-IDF plugin and I am able to compile ESP-IDF example codes and testing the same on ESP32-Wrover-B.

I want to include all source files i.e. "*.cpp" and include files "*.h" of complete project in CMakeList.txt.
I have around hundred of .cpp and .h files and it is not possible to make each and every file entry like this

//------------------ CMakeLists.txt
set( srcs "app_main.cpp"
"Main.cpp"
"test.cpp" )

set (include_dirs "Test_Code"
"Config")

df_component_register( SRCS "${srcs}"
INCLUDE_DIRS "${include_dirs}"
)

My project directory hierarchy goes like this

MyProject ( Folder )
-- Maths ( folder )
--- Sum (Folder)
----- Sum.cpp
----- Sum.h
--- Sub (Folder)
----- Sub.cpp
----- Sub.h
-- Science (Folder)
-- Database (Folder)
---- Database.cpp
---- Database.h
-- Algorithm
---- Algorithm.cpp
---- Algorithm.h
-- app_main.cpp ( contain app_main() function )
CMakelist.txt ( Includes path to all .cpp and .h include directories )
component.mk ( contain just COMPONENT_ADD_INCLUDEDIRS := . )

I am using only once CMakelist.txt that is compiling all files.

Please let me know if anyone has tried any command to add all source files and header files of project that is more easier than adding them manually.

Thanks in advance.

ashigupta9
Posts: 13
Joined: Thu Nov 28, 2019 9:48 am

Re: How to add all include folders and source files of project in CMakeList.txt

Postby ashigupta9 » Fri Jun 25, 2021 6:20 pm

I am able to find solution for this problem . Below is the CMakeLists.txt format i am using

set( srcs "xyz.cpp")
set (include_dirs "include") // PAth to header file folder
idf_component_register( SRCS "${srcs}" INCLUDE_DIRS "${include_dirs} )"

Note: I don't wanted to use "Component" folder name in my project hierarchy.

zliudr
Posts: 357
Joined: Thu Oct 03, 2019 5:15 am

Re: How to add all include folders and source files of project in CMakeList.txt

Postby zliudr » Fri Jun 25, 2021 8:30 pm

Could you enlighten me with how this works? Is "xyz.cpp" literal if yes then how does that help indicate all cpp files? Could someone just "ls *.cpp" your folder and direct to a file to save all the file name typing?

I'm just a beginner regarding CMakeList and IDF components. So if you have so many files, does that mean you have lots of classes etc. By IDF convention are they better off being organized in "loosely interdependent" components instead of all placed in main? Are there anything there that might be useful beyond your current project?

I made a few components and within each I registered every file in the component's own CMakeList, probably not the best but that's all I could do without a systematic understanding of CMakeList syntax and IDF's extension to it.

ashigupta9
Posts: 13
Joined: Thu Nov 28, 2019 9:48 am

Re: How to add all include folders and source files of project in CMakeList.txt

Postby ashigupta9 » Thu Aug 05, 2021 5:14 pm

zliudr wrote:zliudr
sry for the late response. I am registering all my folders in project level CMakelists.txt first. Below is the format of the same

##########################################################################
# CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.5)

set( EXTRA_COMPONENT_DIRS "Code/Config" # Code is a folder that contains another folder called Config and Test
"Code/Test" ) # Config and Test has CMakelists.txt

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

I am registering all .cpp files in my folder in cmake. Below is my CMakelists.txt format

###########################################################
#Set source files
set( srcs "app_main.cpp"
"xyz.cpp" ) # Add all your .c or .cpp files in folder here

#Set include directories
set (include_dirs " ") # Add all your include file path here

idf_component_register( SRCS "${srcs}"
INCLUDE_DIRS "${include_dirs}"

###############################################################

I have created CMakelists.txt for all my folders along with component.mk file .
Content in component.mk file

########################################
COMPONENT_ADD_INCLUDEDIRS := .
########################################

hope this helps.

Who is online

Users browsing this forum: No registered users and 119 guests