Include header file outside of sketch

Is it possible somehow to include a header file outside of the root directory of the sketch?

I'd like to use a "global" header file for multiple sketches that defines the communication protocol for them. As far as I know the IDE makes an out-of-source build and it does not copy headers outside of the sketch directory.

How about creating a link to the file and have the alias in the project directory. Maybe time to look at using a better IDE as well :)


Paul

Is it possible somehow to include a header file outside of the root directory of the sketch?

Yes, of course. That is how libraries work.

Create a folder in the sketches folder libraries folder with the same name as the .h file and put the file in there. Stop and start the IDE and now you can #include the .h file

rockwallaby: How about creating a link to the file and have the alias in the project directory.

This seems to be a reasonable idea. The only downfall of this is that the IDE doesn't detect file changes so I have to be careful when the file is loaded into multiple IDE instances at the same time.

rockwallaby: Maybe time to look at using a better IDE as well :)

Any suggestion? I thought about using a more powerful one, but the current one is so comfortable with its build process and firmware upload method. I didn't have enough time yet to create my own build process for Arduino's ARM. :-)

You can also provide an absolute or relative path to the include file. If all your sketches are in your username\Arduino directory, you can put your "global" includes in Arduino\Global, then use either:

include "../Global/MyGlobalInclude.h"

or

include "c:/users/username/documents/Arduino/Global/MyGlobalInclude.h"

Regards, Ray L.

UKHeliBob:
Yes, of course. That is how libraries work.

Create a folder in the sketches folder libraries folder with the same name as the .h file and put the file in there. Stop and start the IDE and now you can #include the .h file

I’d like to keep this header file in my GIT repository to be able to synchronize is between different workstations. The libraries folder is outside of my source repository as far as I know, so this makes it a bit complicated.

RayLivingston:
You can also provide an absolute or relative path to the include file. If all your sketches are in your username\Arduino directory, you can put your “global” includes in Arduino\Global, then use either:

#include “…/Global/MyGlobalInclude.h”

or

#include “c:/users/username/documents/Arduino/Global/MyGlobalInclude.h”

The relative path method won’t work because for example under Linux the sources of the sketch are copied to /tmp/buildXXX for building. Anything outside of the sketch folder will be omitted and the build process will miss it.

The absolute path is not an option for me as I would like to use multiple workstations with different locations.

giszo: The relative path method won't work because for example under Linux the sources of the sketch are copied to /tmp/buildXXX for building. Anything outside of the sketch folder will be omitted and the build process will miss it.

The absolute path is not an option for me as I would like to use multiple workstations with different locations.

The IDE should copy the include file to the build directory regardless of where it is #included from. Simply put it in the same location on all machines (/global, /usr/include/global?, etc.), and either a relative or absolute path should work just fine.

Regards, Ray L.

I'd like to keep this header file in my GIT repository to be able to synchronize is between different workstations. The libraries folder is outside of my source repository as far as I know, so this makes it a bit complicated.

You might have mentioned that in your first post. You could move your sketches folder, and hence its libraries folder, somewhere inside the GIT source repository I suppose.

RayLivingston: The IDE should copy the include file to the build directory regardless of where it is #included from. Simply put it in the same location on all machines (/global, /usr/include/global?, etc.), and either a relative or absolute path should work just fine.

Tried with a relative location this way under Windows:

#include "../protocol/protocol.h"

... and I got the following compilation error:

controller.ino:11:34: fatal error: ../protocol/protocol.h: No such file or directory compilation terminated. Error compiling.

UKHeliBob: You could move your sketches folder, and hence its libraries folder, somewhere inside the GIT source repository I suppose.

Do you mean that I could have a non-global libraries folder as well in my repository beside the global one in Arduino's folder? Is there any information about doing this somewhere? Where to put that directory, etc ...

Do you mean that I could have a non-global libraries folder as well in my repository beside the global one in Arduino's folder?

No, I meant that you could have the one and only libraries folder intended to hold user contributed libraries in your repository.

If you save your programs in the default directory then that too would be in the repository. How do you synchronise source code between different workstations at the moment ?

UKHeliBob: No, I meant that you could have the one and only libraries folder intended to hold user contributed libraries in your repository.

Okay, I see.

UKHeliBob: If you save your programs in the default directory then that too would be in the repository. How do you synchronise source code between different workstations at the moment ?

At the moment I use the GIT repository for keeping my sources at a global place. I pull the repository to the workstation where I want to work and push my modifications to the central repository, like the usual workflow of version control systems. The repository contains two sketches (both of them containing multiple files), electronic designs (schematic, PCB), documentations and such.

If I understand you correctly I should clone my repository to the default sketch location (it's Documents/Arduino under Windows) and I could use the libraries directory there to have a common place for global includes?