How to setup a different path for librabies in #include

Suppose I say I have 3 customs boards that I wanted to use. Each having different SPI libraries and they have to be configured into a single file. So what my idea was to have different sub-folders in my libraries folder and in that sub-folder I'd have the libraries. Now how do I tell the IDE which path to take when a particular board is selected and it uses SPI library. Because I think that, it would create an error when my custom_board_1 uses the SPI libraries which have been configured for custom_board_2.

You can use a full path or a relative path like

include "\a\b\c\d.h"

Yeah but I wanted to make permanent changes. Each board uses different tools. So I was thinking if somehow I can change the INCLUDE variable defined in gcc or any other compiler , I can add a different path. So I want to know how to add a different path to the main compiler (let's take avr-gcc for example)

Since each board uses different tools they should each have their own hardware package. You can bundle libraries with a hardware package in the libraries subfolder of the package. That folder will only be added to the include path when one of the boards of that package is selected. This is how all hardware packages already handle the SPI library since it is hardware specific.

Thanks, but just out of curiosity can you help me with one last thing? Say I wanted to change the default path which the Arduino sets for the gcc compiler when #include is used to search for SPI library. I know by default it goes to the library directory but I wanted to know how is that path defined and how can I see it. Also, what would I have to do if I wanted to change it.

If you do F*ile > Preferences > Show verbose output during compilation (check) > OK* then examine the output after a compilation you can get an idea of the process and see all the generated commands.

What happens with IDE versions 1.6.6 and newer is the Arduino IDE passes some paths based on the IDE installation location to the arduino builder tool: That lets arduino-builder know the location of:

  • Arduino IDE's bundled hardware packages folder
  • Boards Manager hardware packages folder
  • sketchbook hardware packages folder
  • Arduino IDE's "built-in" libraries folder
  • sketchbook libraries folder
  • FQBN of the selected board (e.g. arduino:avr:uno)

arduino-builder preprocesses the sketch and uses the above information to determine the necessary -I flags to pass to the compiler via the {includes} property used in the recipes in platform.txt.

Prior to Arduino IDE 1.6.6 there was a similar process but it was all mashed into the Arduino IDE code, rather than being broken out into the separate arduino-builder tool.

So a lot of this is coded into the Arduino IDE and arduino-builder and can't be modified externally. However, in your platform.txt you can modify the recipes as you like so if you wanted to add other include paths it should be easy enough by just adding extra -I flags where appropriate. Unless you have good reason to do otherwise, I think it's a good idea to stick with the conventions that have been established and used by all hardware packages.

Okay, I lost you at -I flags. What are -I flags, I'm assuming that they provide the path of the library? Let's say I wanted to edit my cpp recipe , given below

recipe.cpp.o.pattern="{compiler.path}{compiler.cpp.cmd}" {compiler.cpp.flags} -mmcu={build.mcu} -DF_CPU={build.f_cpu} -DARDUINO={runtime.ide.version} -DARDUINO_{build.board} -DARDUINO_ARCH_{build.arch} {compiler.cpp.extra_flags} {build.extra_flags} {includes} "{source_file}" -o "{object_file}"

and my path is let's assume D:/extra/libraries/custom_board_1

How do I add this path to the recipe?

If I'm not wrong I think the path is being taken from "{includes}" variable

Yeah, you're right, it would be a lot easier to follow the convention but learning about this has made me understand about the beauty of Arduino in much more detail.

ozil_11: What are -I flags

ozil_11: and my path is let's assume D:/extra/libraries/custom_board_1

How do I add this path to the recipe?

recipe.cpp.o.pattern="{compiler.path}{compiler.cpp.cmd}" {compiler.cpp.flags} -mmcu={build.mcu} -DF_CPU={build.f_cpu} -DARDUINO={runtime.ide.version} -DARDUINO_{build.board} -DARDUINO_ARCH_{build.arch} {compiler.cpp.extra_flags} {build.extra_flags} {includes} "-ID:/extra/libraries/custom_board_1" "{source_file}" -o "{object_file}"

Now if you wanted to make the path relative to the hardware package location you could do something like this:

recipe.cpp.o.pattern="{compiler.path}{compiler.cpp.cmd}" {compiler.cpp.flags} -mmcu={build.mcu} -DF_CPU={build.f_cpu} -DARDUINO={runtime.ide.version} -DARDUINO_{build.board} -DARDUINO_ARCH_{build.arch} {compiler.cpp.extra_flags} {build.extra_flags} {includes} "-I{runtime.hardware.path}/extra/libraries/custom_board_1" "{source_file}" -o "{object_file}"

Thanks a lot .... Best teacher ever !!!!!!! Also I've had this doubt for a long time, is Pert the same person or it's just a username every Arduino support members uses.

It's just me. My first name is Per and my last name starts with a "T". I'm not formally affiliated with the Arduino organization. I'm just a volunteer trying to help out in my spare time as my abilities allow

Wooow that is soo awesome. Massive respect. Anyways another quick question. What I understood till now is that it has a default library path along with what we defined. Now is there anyway I can remove the default path and set my custom path for libraries as default ????

You could do it by leaving {includes} out of the recipes in platform.txt.