Difficulties with project files in hierarchical structure

I have a project which for practical reasons I have added a sub-directory to. It looks something like this:

> Sketch
    Sketch.ino
    Module.h
    Module.cpp
  > Resources
       Rsc1.h
       Rsc1.cpp

I am having a couple of problems with this arrangement:

  1. it is not possible to navigate to the Resources directory within IDE2.x.x using the built-in navigator. Navigation stops at the Sketch directory and does not expand any further down to view Resources. This means I have to use an external editor to work with files in the sub-directory.

  2. The compiler apparently finds the Rsc1.h file even if a relative path to thw main directory is used, but fails to include the functions in Rsc1.cpp. The linker then complains about undefined references to all the functions found in Rsc1.cpp.

There was a similar post some 3 years ago which I imagine related to IDE 1.8.x:

It would seem that the same appears to still apply IDE 2.3.x?

The workaround seems to be to either stick with a flat directory structure which is becoming a bit unwieldy. Another option is to use full paths as mentioned in the linked discussion which I have tried, but with no change in result. I guess the other option is to stick with the flat project structure or else use another IDE.

I was hoping that this problem had been resolved in IDE2? Perhaps I am missing something but a single #include for the .h file was sufficient to include both .h and .cpp into the project for all the other .h/.cpp pairs that I have in the Sketch directory, i.e at the same level as the .ino file.

If I deliberately mis-spell the file path then the IDE tells me that it can't find the file, so it does appear to be finding it, just not importing the functions for some reason.

You have to rename the top-level subdirectory to src (instead of Resources or anything else). Only then will the compiler go in there to build all the .cpp, and in any nested subdirectories. (When actually compiling a file, the preprocessor will use the paths you specify.)

In the Settings/Preferences dialog, turn on "Show files inside sketches"

2 Likes

Is the issue resolved if you move Rsc1.cpp and Rsc1.h to the main sketch directory?

Yes that solves problem (1). Thank you.

Was reasonably confident, but tested to be sure and yes, it is.

Ooh that is very interesting! After doing the test b707 suggested, I created a src directory and moved the files into it and changed the #include path to src/Rsc1.h and that worked!

I then also created a sub-directory under src, e.g. src/Resources, moved the files into that and updated the #include path to src/Resources/Rsc1.h and that worked as well. As you said, src is a mandatory name for the first sub-directory level.

Thank you. Problem number (2) solved!

Thank you for the quick responses. :slight_smile: