Adding folder s to ESP-IDF template project

User avatar
dmdamen
Posts: 15
Joined: Sun Aug 19, 2018 2:47 pm

Adding folder s to ESP-IDF template project

Postby dmdamen » Thu Aug 30, 2018 7:27 am

I am able to write and compile C++ code for the ESP32 using Eclipse on Linux with the ESP-IDF template. However, I would like to store my classes in seperate directories. For some reason I cannot get this to work. Eclipse keeps complaining about not being able to find "myclass.h"

My folder structure looks like this:

Code: Select all

- MyProject
+ Build Targets
+ Binaries
+ Archives
+ Includes
+ build
- main
    - main.cpp
    - myclass.cpp
    - myclass.h
    - component.mk
- CMakeList.txt
- Makefile
- README.rst
- sdkconfig
The structure shown above works but I would like to move my class to a different directory. Someting like this:

Code: Select all

- MyProject
+ Build Targets
+ Binaries
+ Archives
+ Includes
+ build
- classes
    - myclass
        - myclass.h
        - myclass.cpp
- main
    - main.cpp
    - myclass.cpp
    - myclass.h
    - component.mk
- CMakeList.txt
- Makefile
- README.rst
- sdkconfig
How do I configure this? Do I need to edit the cmake files? Do I add this to Eclipse? (I tried to do the latter already)

T.i.a!

jcsbanks
Posts: 305
Joined: Tue Mar 28, 2017 8:03 pm

Re: Adding folder s to ESP-IDF template project

Postby jcsbanks » Thu Aug 30, 2018 8:03 am

viewtopic.php?f=13&t=6766

I am not using Eclipse or Linux but the above might help? A colleague just loves subdirectories :)

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: Adding folder s to ESP-IDF template project

Postby ESP_Angus » Thu Aug 30, 2018 8:58 am

Hi dmdamen,

I would suggest structuring your project in the way shown in the build system docs:
https://docs.espressif.com/projects/esp ... le-project

(This link is for the CMake preview branch docs, in particular.)

In brief: rename "classes" directory to "components" and create a "component CMakeLists.txt" file for the "myclass" component.

If you don't want to do this for some reason, you can also append this line to the end of your project CMakeLists.txt file:

Code: Select all

target_include_directories(myproject.elf PRIVATE ../classes/myclass)
Where "myproject" is the name of your project as given in the same file. That way the source files in "main" will add the include directory in "myclass".

I would strongly recommend the first suggestion (creating a component) instead of this,though - components are more modular and will scale up better as your project grows. The documentation explains them in detail.

User avatar
dmdamen
Posts: 15
Joined: Sun Aug 19, 2018 2:47 pm

Re: Adding folder s to ESP-IDF template project

Postby dmdamen » Thu Aug 30, 2018 9:51 am

Thanks for the help so far.

I renamed to folder to components and I created the structure as described in the documentation. I need to include the component like this from my main.cpp to make it compile.

Code: Select all

#include "../components/myclass/include/myclass.h"
I'd like to be able to specify it like this:

Code: Select all

#include "myclass.h"

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: Adding folder s to ESP-IDF template project

Postby ESP_Angus » Thu Aug 30, 2018 10:03 am

What's the contents of your component CMakeLists.txt file? You'll need a line like this:

Code: Select all

set(COMPONENT_ADD_INCLUDEDIRS ".")
before the "register_component()" line, so that the build system knows that you want this directory added to the include path.

User avatar
dmdamen
Posts: 15
Joined: Sun Aug 19, 2018 2:47 pm

Re: Adding folder s to ESP-IDF template project

Postby dmdamen » Thu Aug 30, 2018 10:15 am

I took the example from the documentation.

Code: Select all

set(COMPONENT_SRCDIRS ".")
set(COMPONENT_ADD_INCLUDEDIRS "include")
register_component()
My project now looks like this:

Code: Select all

- components
    - myclass
        - include
            myclass.h
        myclass,cpp
        CMakeLists.txt
- main
    main.cpp

User avatar
dmdamen
Posts: 15
Joined: Sun Aug 19, 2018 2:47 pm

Re: Adding folder s to ESP-IDF template project

Postby dmdamen » Tue Sep 04, 2018 5:41 pm

I think I found it. The component folder also needs to have an empty component.mk file. The build system will not recognize the components if this file is not present. You can check the component with the folowing command:

Code: Select all

make list-components

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: Adding folder s to ESP-IDF template project

Postby ESP_Angus » Wed Sep 05, 2018 2:04 am

Hi dmdamen,

Glad you got it working. I think I misunderstood something when I replied to you the first time.

Because your initial project contained a "CMakeLists.txt" file, I assumed you were using the new CMake-based build system which is currently in preview release in the branch "feature/cmake". So I linked you to more CMake-based documentation. I didn't notice there was also a Makefile in your project. I should have clarified the details with you before replying.

For the CMake-based build system (in the feature/cmake branch), both components and the main project must contain a CMakeLists.txt file.

For the GNU Make based build system (the current build system used in the master branch and all stable releases), components contain a component.mk file and the project contains a Makefile.

The documentation for the default GNU Make based build system is here:
https://docs.espressif.com/projects/esp ... ystem.html

Angus

william.ferguson.au
Posts: 107
Joined: Wed Jan 02, 2019 8:55 am

Re: Adding folder s to ESP-IDF template project

Postby william.ferguson.au » Wed Jan 02, 2019 9:05 am

ESP_Angus, I think there is a lot of confusion being caused by the esp-idf-template and the examples all containing config for both Make and for CMake. For those of us coming from other languages and environments it is not obvious that they are separate and distinct types of config.

It would be really helpful if the online docs (https://docs.espressif.com/projects/esp ... ild-system) were explicit about the options and syntax of what is available and expected within the Makefile and component.mk files.

For example, I have still been unable to configure my project to have a sibling folder to "main" that contains source and headers.

Who is online

Users browsing this forum: No registered users and 306 guests