#include does not work as expected

For this project, I have a number of very, very similar sketches. Basically, they share everything except a #define for a module name. So I moved the code into a separate file to include it into my sketches, which then should look like:

#define MODULE_NAME "Module1"
#include "../Master.cpp"

This does not work - the sketch does not find the "Master.cpp" file wich definitely is in the parent directory.

If I softlink the Master.cpp into the sketch directory, it tries to compile both the sketch and the Master.cpp file, and fails (Obviously, an .ino file includes something that I cannot see as a user).

If I softlink the file into the sketch directory as "Master.h", and use

#define MODULE_NAME "Module1"
#include "Master.h"

it works.

So while I have a working solution, why does #include "../Master.cpp" not work?

I don't know the way how Arduino IDE handles .cpp files with #include in the different directory, but how about using .hpp instead of .cpp.

When I want to share some C or C++ source code among multiple projects, I use .hpp as follows:

Arduino
├── Project_A
│   └── Project_A.ino
├── Project_B
│   └── Project_B.ino
└── src
    ├── code_1.hpp
    ├── code_2.hpp
    └── code_3.hpp

where in Project_A.ino and Project_B.ino I use #include directive something like this:

#include "../src/code_1.hpp"
#include "../src/code_2.hpp"

In fact, code_A.hpp and code_B.hpp are C or C++ source code.

I just tried #include "../Master.hpp", #include "../Common/Master.h", and #include "../Common/Master.hpp", and none of them work. And yes, the files are there, they are acccessible, valid, etc. Always "Compilation error: (Filename incl. relative path as in the #include statement) No such file or directory".

Could you share a picture or something to show us were is located the file you wanna use and were is the project saved? Maybe the paths to the files are enough for us to tell

I tried something similar and it appeared to work.

What version of IDE, which OS, and have you got a complete minimal example with which we could reproduce the error exactly.

Possibly because the IDE copies the files in the sketch directory to a temporary directory before compiling the sketch from the temporary directory.

The directory structure looks like this at the moment. I killed all the ".hpp" and the "Common" subdirectory that did not work.

/home/ct/Arduino/Moon
├──LandingPad
│  ├──LandingPad.ino
│  ├──LandingPad.h -> /home/ct/Arduino/Moon/LandingPad.h
│  └──Network.h -> /home/ct/Arduino/Moon/Network.h
├──LandingPad.h
└──Network.h

I'm using arduino-ide_2.3.6_Linux_64bit.AppImage with the "--no-sandbox" parameter.

The LandingPad.ino is just

/*
Landing Pad 1
*/
#include "Network.h"
#define MODULE_NAME "LandingPad1"
#define MODULE_PREFIX "Moon/" MODULE_NAME "/"
#include "LandingPad.h"

This works, but any attempt of a relative path fails.

Create a folder in your sketch directory named src. In that folder put any files or folders containing files to be #included then you can do this

#include "src/LandingPad/LandingPad.h"
#include "src/LandingPad/Network.h"

Yes, it fails, because Arduino IDE does not compile your code. It copy it somewhere to to temp directory and compile that copy (as can be seen, if you turn compiler messages to be visible).
And if the IDE did not copy all related files there too, then the related files are missing in the compile time.

Ether put all your files into subdirectories of the *.ino file, or make them a "library" for Arduino and use them as "library", else use some sane building system, not "Arduino IDE".
Arduino IDE use gcc for compiling and for example GitHub - sudar/Arduino-Makefile: Makefile for Arduino sketches. It defines the workflows for compiling code, flashing it to Arduino and even communicating through Serial. use gcc too, but with normal Makefiles and normal make, so all relative paths normally works

(Just for test I moved all *.h from root of project 3 level of directories down and used in all related includes ../../../ as prefix and it normally compiled without problem.)

That would not solve the issue. What currently is the "LandingPad" directory and "LandingPad.ino" should be the "LandingPad1" directory with a "LandingPad1.ino", next to a "LandingPad2" directory with a "Landingpad2.ino", etc. They all share "Network.h" (with the SSID, the password, the MQTT server and port), and the "LandingPad.h" (which is the brain of the operation). The "LandingPad2.ino" would be nearly identical with the "LandingPad1.ino" except for the definition of "MODULE_NAME".

About using a "sane building systems instead of Arduino IDE" - I tried to build this with the RPi Pico SDK, but it was a bigger build system catastrophe than this here. Apart from their horrible MQTT implementation...

At this point, I can say that the soft link variety works for me. I was just wondering why relative paths outside the Sketch directory don't work as expected. If the IDE copies stuff around without thinking, this would explain the issue.

I think, that making the scr directory somewhere and making link named "src" in your project directory (where the link itself will be with full path, not relative) may work.

/home/ct/Arduino/Moon/LandingPad1/src -> /home/ct/Libraries/central/Moon/src
/home/ct/Arduino/Moon/LandingPad2/src -> /home/ct/Libraries/central/Moon/src
/home/ct/Arduino/Moon/LandingPad3/src -> /home/ct/Libraries/central/Moon/src

As for using build scripts, I'll be using makefiles on the long run with "arduino-cli compile --export-binaries" (and all the other options needed for it to actually build something) and then create the UF2 files from there into a separate directory for installation. For experimenting with the individual boards, the IDE is more than sufficient, and amazingly simple, if not too simple in some places (Like WTF does it not save project related preferences in the sketch directory like Board, Port, Programmer, etc)

I don't plan to add extra layers of complication to the whole thing. I now know that #include "../" does not work, and that I have to softlink the modules I need into the sketches. The main goal - that I can reuse and recycle the code has been reached.

Hi @treczoks_job.

As was explained previously:

and here:

I don't think that "without thinking" is an accurate description. You are simply trying to do something that the IDE is not designed for, and which there is no reason for it to be designed for.

Arduino libraries are the mechanism by which code is shared between multiple sketch projects. If you use the established mechanism, you will find that Arduino IDE works just fine. If you try to invent your own non-standard equivalent of that mechanism, then you should not be surprised to find that it doesn't work.

I use C compilers for about 30 years now, and this is the first time ever where I encounter an IDE where this simple, standard kind of behavior does not work. I never had reason to suspect that the IDE copies my sources to somewhere else before compiling it. Now that I know what to expect I can work with it, but it is not something to be expected - of all the tens of different C IDEs over a dozen or more platforms, the Arduino IDE is the first showing this rather odd and unexpected behavior.