Setting up common source files in Espressif-IDE

ben3welec
Posts: 7
Joined: Tue Jul 14, 2020 1:06 pm

Setting up common source files in Espressif-IDE

Postby ben3welec » Fri Mar 18, 2022 8:43 pm

Hi All,

I hope this ins't a duplicate topic (I've done a fair amount of googling and tinkering), but I cannot seem to come up with the right way to have a set of common source files shared between two projects in the Espressif IDE (Version: 2.4.0 / Build id: 20220128-0745)

What I want is something like this:

Project1/
|---main/
| |----app1_main.c
|...

Project2/
|---main/
| |----app2_main.c
|...

SharedSource
|---main/
| |----shared_code.h
| |----shared_code.c
|...

Where I would like to be able to #include shared_code.h into app1_main.c and app2_main.c. What is the correct way to do this so that I don't have to keep individual copies of shared_code.h/c and then remember to copy them back and forth whenever I need to make a change?

Thanks much!

ESP_kondalkolipaka
Posts: 178
Joined: Wed Jul 17, 2019 5:26 pm

Re: Setting up common source files in Espressif-IDE

Postby ESP_kondalkolipaka » Fri Mar 25, 2022 10:02 am

Hello,

Could you take a look at this thread in the CMake forum on sharing the code between projects.

To link the projects in the eclipse, you could use Project references concept so that you will be able to access the shared code across the different projects.

To do this, go to Project 1 > right-click on the project > Go to Properties > Navigate Project references > Check shared code project here

After this, you should be able to access the functions from the shared project in project 1.

I hope that helps.

ben3welec
Posts: 7
Joined: Tue Jul 14, 2020 1:06 pm

Re: Setting up common source files in Espressif-IDE

Postby ben3welec » Mon Mar 28, 2022 6:44 pm

Hi ESP_kondalkolipaka,

Thanks so much for your response. Unfortunately I couldn't seem to make much of the cmake thread you referenced, and I also wasn't able to get far with the "Shared Projects" concept - I wasn't really sure how to fit that in with my paradigm as the shared files are not really a project unto themselves and as I've set them up, cannot be built without files in the projects that reference them (Not something I shared earlier as I didn't fully appreciate that detail myself). BUT, your comments, along with reading https://docs.espressif.com/projects/esp ... ystem.html about 3 times) did lead me along enough that I could muddle my way to a seemly half working solution by altering the make files using EXTRA_COMPONENT_DIRS command.

The way I've currently setup the thing is (based on my previously described file setup):

Project1/CMakeLists.txt (the makefile in the project's root directory):

[Codebox]
# 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.5)

include($ENV{IDF_PATH}/tools/cmake/project.cmake)
set(EXTRA_COMPONENT_DIRS "../SharedSource") #<========== Line I added
project(Project1)
[/Codebox]

This seems to point IDF to the location of the shared files. I then also added a makefile to the SharedSource directory like this:

[Codebox]
idf_component_register(SRCS "shared_code.c"
INCLUDE_DIRS "."
REQUIRES main)
[/Codebox]

where the important takeaway seems to be adding "REQUIRES main" at the end of idf_component_register. This is probably because of my shared code having dependencies back to code in the Project1 / Project2.

I call this a half-working solution because, while the code builds and loads into my esp32 and operates as expected, The Eclipse (or espressif IDE) indexer can't seem to figure out what is going on and in particular puts error markers at my #includes of esp32 related header files. At this point, this is a price that I'm willing to pay to not have to try to manually keep my shared code synced up across multiple projects. If you have any insight into how to fix this, however, I'd greatly appreciate hearing it. Thanks so much!

Who is online

Users browsing this forum: No registered users and 25 guests