Go Down

Topic: how to include header file from previous folder? (Read 6426 times) previous topic - next topic

EvylSeme

Hello everyone,

What i want to achieve is rather simple.

I build a program for my Arduino for which i use alongside the .ino file also .cpp and .h files.
For these .cpp and .h files I build test cases with ArduinoUnit library.

In order to work neatly (the way i learned it at school) I have this folder structure:
(note: the names have been changed for this example)

MyArduino/Com.cpp
MyArduino/Com.h
MyArduino/Inter.cpp
MyArduino/Inter.h
MyArduino/MyArduino.ino
MyArduino/Testing/Testing.ino


In Testing.ino I want to include Com.h and Inter.h.
I tried to achieve this like so:
#include "../Com.h"
#include "../Inter.h"

I tried several other ways like using '\' instead of '/'
or "../../Com.h" or "./../Com.h"

Arduino doesn't seem to like them. He keeps complaining "no such file or directory".
I also checked for spelling, it is correct.

Anybody any ideas? They are very welcome.

PaulS

Quote
Anybody any ideas?
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?

CrossRoads

PaulS, is this the location rule to follow? I think this has worked for me for .h libraries:
"filename" has to be in same folder as the .ino,
<filename> has to be the /libraries (/library ?) folder in the File:Preferences:sketchbook location

Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

PaulS

Quote
is this the location rule to follow?
Yes. Relative paths are not supported. I want OP to understand why, by doing a little detective work.

EvylSeme

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?
I did as you suggested. Verbose mode on, it lead me to something like:
/AppData/Local/Temp/build<a lot of characters>/sketch/Testing.ino.cpp

Neither in the Sketch nor build folder are the included library.

I'm not sure what you wish to achieve with this, seeing as arduino refuses to compile because it can't find the header files.

(Also, it's prefered not to put the .h and .cpp files in myDocument/Arduino/library/ (for < > usage).

UKHeliBob

Quote
it's prefered not to put the .h and .cpp files in myDocument/Arduino/library/ (for < > usage
I am not sure what you are saying here.  Is that your preference or did you mean something else.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

EvylSeme

I am not sure what you are saying here.  Is that your preference or did you mean something else.
Sorry for being unclear.

Arduino has to possibility to put the .h and .cpp files in the mydocuments/arduino/library/ so i can use it in code as #include <Com.h>

I do NOT prefer this.

PaulS

Quote
I do NOT prefer this.
Your preferences do not matter.

The reason for having you go look at the something like/AppData/Local/Temp/build<a lot of characters>/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 <header.h>, 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.

Robin2

The basic problem is that the Arduino IDE copies your source code files to another directory before it tries to compile them and relative file references don't work in that situation - they are relative from the wrong place.

Absolute file references should work - but they can be a PITA for other reasons.

To solve a somewhat different problem I created a short Python program that copies my program files to a directory called ArduinoTemp and it renames my project file to ArduinoTemp.ino (so it matches the directory name to keep the IDE happy). Then it calls the IDE to verify or upload as required. This allows me to use a common .h file with two separate Arduino programs - one for a receiver and the other for the transmitter.

I suspect this general approach could be used to meet your need.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up