Can Arduino IDE compile files in sub directories?

Can Arduino IDE compile files in sub directories?

I get this error when I attempt to include a file in a sub directory:

directories2:1: error: dir2\file2.h: No such file or directory
directories2.ino: In function 'void loop()':
directories2:12: error: 'func2' was not declared in this scope

This is the directory structure:

directoreis2 (folder)
	directoreis2.ino
	dir2 (folder)
		file2.h

I am compiling with Arduino IDE 1.0.5-r2 in Windows 7.

directoreis2.ino:

#include "dir2\file2.h"
//#include "dir2/file2.h"	same result with forward slash

void setup()
{
	Serial.begin(115200);
	Serial.println("in setup");
}

void loop()
{
	Serial.println("in loop");
	func2();
	while(true) { } 
}

file2.h:

#include <Arduino.h>

void func2()
{
	Serial.println("in func2");
}

Thank you.

If you create a library you can have a sub-directory called 'utility', but its seems not for a sketch, you'll have to keep all the sketch files together.

Thanks pYro_65.

I guess memory is so small that flat file structures are acceptable.

I guess memory is so small that flat file structures are acceptable.

Even if is weren't, dir2 is a directory in a directory. How is the preprocessor supposed to know what directory it is in?

You can use relative paths, like ".\dir2\file3.h" or absolute paths like "E:\here\that.h", but you can't expect the compiler to know "relative to" unless you define what "relative to" means (the . does that in the first example).

Hi PaulS,
I tried this but still get the error:

subdirectories:5: error: .\dir2\file2.h: No such file or directory
subdirectories.ino: In function 'void loop()':
subdirectories:17: error: 'func2' was not declared in this scope

subdirectories.ino:

#include ".\dir2\file2.h"		//error: .\dir2\file2.h: No such file or directory
//#include "./dir2/file2.h"		//same result with forward slash

void setup()
{
	Serial.begin(115200);
	Serial.println("in setup");
}

void loop()
{
	Serial.println("in loop");
	func2();
	while(true) { } 
}

.\dir2\file2.h:

#include <Arduino.h>

void func2()
{
	Serial.println("in func2");
}

I tried this but still get the error

Do you have any idea what . is when the code is compiled? It is NOT the directory that the sketch is in. If you can not be absolutely certain what . is, do not go that route. And, you can't, because the IDE creates a new build directory every time it builds.

PaulS,

What is '.' when the code is compiled? And where can I read more about it.

Thanks.

You can't usefully put anything in a subdirectory to a sketch. All files in the sketch folder are copied to a Temp folder for compiling, but nothing in sub-folders.

You could put an include file in another directory and use an absolute path, but I would really not recommend it.

I spent some time trying to find ways round the "flat" restriction in the Arduino IDE, but decided don't fight it, live with it :slight_smile:

An alternative is to use a different IDE , like Eclipse, if you don't mind losing Arduino IDE compatibility.

Thanks for the advice bobcousins.

If your project is big enough to want subdirs, then code it as a library or
libraries.. Its probably made of several parts anyway, one or more of
which you may want to reuse in future sketches.

Great idea MarkT! :slight_smile: