3

I have a project that is part of a larger repository, and shares header files with non-Arduino C programs.

I know that it's possible to use these headers by copying them to the libraries folder, or the sketch folder, but I would rather use them with a relative path (which seems to not work), so I can avoid copying files around for every repository clone or update.

Is there a solution that doesn't involve moving/copying the header file?

EDIT: Although there is no full solution for this question, I have since found that using the PlatformIO IDE instead solves the problem.

  • symlinks maybe? – Edgar Bonet Apr 21 at 10:46
  • I tried that using cygwin on windows. The IDE didn't like it. – Photon Apr 21 at 11:33
  • I use links. I link a folder into libraries folder. mklink on Windows – Juraj Apr 21 at 16:09
3

You can use header files with an absolute name, like:

#include "c:\Temp\x.h"

Another way that might help is to make the include path like:

#include "q:\x.h"

And use Windows to map driver letter Q to the path you need.

  • I guess that's better than nothing, but requires the repository to be cloned to a specific location, or changes to the code each time. – Photon Apr 21 at 11:32
  • @Photon ... I tried using a define inside the include path to make that path variable, but that does not seem to work. I added another solution (mapping a drive letter). Than you need only one change. – Michel Keijzers Apr 21 at 11:38
  • 1
    Thanks. I'll mark it as an answer, because it provides the best that can be done under the IDE limitations. – Photon Apr 21 at 12:39
  • Thanks; and yes, it seems like a limitation of the IDE. – Michel Keijzers Apr 21 at 12:47
2

I tried to do this, and looked into it, a few years ago, but it doesn't appear to be possible to do what you want to do. This guy was having the same issue, #include statement with relative path.

Seems like either Michel's suggestion or copying them (which is a pain) will work, from Can I include a header file that is not a library?:

If the include file is part of a single Sketch, instructions are here... http://www.arduino.cc/en/Hacking/BuildProcess [new link]

If the include file is meant to be shared by multiple Sketches then...

  • Close the Arduino IDE
  • Navigate to the {Arduino}\hardware\libraries directory
  • Create a subdirectory. I suggest something like MyCommon.
  • In the new subdirectory, create a header file. I suggest something like MyCommon.h.
  • Open the new header file, edit it as you wish, and save it
  • Open the Arduino IDE
  • Create or open a Sketch
  • Add a #include to the top of the Sketch that references your new include file

This is why relative paths are not supported, from Re: how to include header file from previous folder?

Enable verbose mode when compiling. Navigate to the Testing.cpp file that is created from the Testing .ino file. Go up one level. Do you see the files you are trying to include?

and from Re: how to include header file from previous folder? (emphasis is mine)

The reason for having you go look at the something like/AppData/Local/Temp/build/sketch folder was to make you think about what the build process is doing, and maybe think that you could google Arduino + Build to get more details.

The IDE copies your sketch, and stuff #included by your sketch to a build directory.

If you #include , the file header.h, and everything else in the directory it is in, will be copied to the build directory, too.

If you #include "header.h", the file header.h, and everything else in the directory it is in, will be copied to the build directory, too.

The difference between <> and "" is where the header.h file is looked for. "" looks in the current directory. <> looks in the library directories.

If the token between the <> or the "" is not found in the appropriate folder, you are not told that; nothing gets copied, though.

But, that explains why relative names are useless. A sketch directory might contain a file called header.h. It will not contain a file called ..\header.h, because ..\header is NOT a valid file name.

Note that I did NOT say that ..\header.h is not a valid NAME. I said that it is not a valid FILE NAME.

Only file names between the <> or "" are possible, as far as the IDE is concerned.

If you do not like that, you are free to not use the IDE.

Finally from Re: How to specify a path to a specific header file?

The Arduino system has some severe limitations in its ability to work sensibly with the file system - can be a real PITA.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.