Page 1 of 1

Adding folder s to ESP-IDF template project

Posted: Thu Aug 30, 2018 7:27 am
by dmdamen
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!

Re: Adding folder s to ESP-IDF template project

Posted: Thu Aug 30, 2018 8:03 am
by jcsbanks
viewtopic.php?f=13&t=6766

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

Re: Adding folder s to ESP-IDF template project

Posted: Thu Aug 30, 2018 8:58 am
by ESP_Angus
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.

Re: Adding folder s to ESP-IDF template project

Posted: Thu Aug 30, 2018 9:51 am
by dmdamen
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"

Re: Adding folder s to ESP-IDF template project

Posted: Thu Aug 30, 2018 10:03 am
by ESP_Angus
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.

Re: Adding folder s to ESP-IDF template project

Posted: Thu Aug 30, 2018 10:15 am
by dmdamen
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

Re: Adding folder s to ESP-IDF template project

Posted: Tue Sep 04, 2018 5:41 pm
by dmdamen
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

Re: Adding folder s to ESP-IDF template project

Posted: Wed Sep 05, 2018 2:04 am
by ESP_Angus
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

Re: Adding folder s to ESP-IDF template project

Posted: Wed Jan 02, 2019 9:05 am
by william.ferguson.au
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.