1

I, once again have difficulties including libraries from code files other than my .ino file.

I have several .cpp and .h files in my project to keep it sorted and clean. However one of my header files starting like this:

#ifndef DPLANDEF
#define DPLANDEF
#include <Arduino.h>
#include <EEPROM.h>
#include "syssettings.h"

yields the following errors:

In file included from /var/folders/jl/nv1qvh6n569cxq9xxfd6dx980000gn/T/build4605486877500371361.tmp/dplan.cpp:2:0: /var/folders/jl/nv1qvh6n569cxq9xxfd6dx980000gn/T/build4605486877500371361.tmp/dplan.h:5:20: fatal error: EEPROM.h: No such file or directory #include ^ compilation terminated. Fehler beim Kompilieren.

EEPROM.h should be known to the IDE, because I can find it in the libraries menu, too. If it was a custom library, I might have botched something during installation, but this is a standard library. It should work out of the box. What's the problem here?

Ariser
  • 567
  • 1
  • 7
  • 25
  • See [Classes and objects: how many and which file types I actually need to use them?](http://arduino.stackexchange.com/questions/13178/classes-and-objects-how-many-and-which-file-types-i-actually-need-to-use-them) - that mentions the issue of putting your includes in the main .ino file. – Nick Gammon Aug 29 '15 at 22:50
  • @NickGammon Interesting roundup of the whole thing. bookmarked now. – Ariser Aug 30 '15 at 00:34

1 Answers1

2

The problem here is that it's a header file. The IDE doesn't treat header files like it does INO files - it doesn't parse them for included libraries, etc.

In order for it to work you must also include EEPROM.h in your main INO file so that the IDE knows that you want to include it in the rest of your files.

Majenko
  • 103,876
  • 5
  • 75
  • 133
  • Thanks a lot. In the past I fiddled cluelessly around until it finally somehow worked out. After adding all libs to my INO header file, all libraries are detected, but I have a new problem. The IDE somehow managed to circumvent the include guard definition of PinChangeInt.h (custom library, which I tested already successfully). Linker is now complaining about lots of double definitions. Sigh. I guess time to move to a "real" IDE!? – Ariser Aug 29 '15 at 22:20
  • @Ariser Include guards only work within the same *compilation unit*. If you include the file in separate cpp or ino files it will exist multiple times, and any variables or functions defined in there will be conflicting. You should never have functions or variables in headers - put them (once) in cpp or ino files and put *extern* references to them in the header files. The header files then act like a "sharing" point for the variables and functions in the other cpp and ino files. – Majenko Aug 29 '15 at 22:43
  • `Sigh. I guess time to move to a "real" IDE!?` - you would have that problem anywhere. As Majenko said, .h files are for declarations. To **define** something it should be done once, in a .c or .cpp file. You won't get double definitions on the class declaration. Your PinChangeInt library should also have a PinChangeInt.cpp file which is compiled once. – Nick Gammon Aug 29 '15 at 22:52
  • @Majenko I'm familiar with separation of declaration and definition. But I didn't suspect that this library was broken. I should have been alerted by the fact, that it was i linker and not a compiler error. Looking closely, this library lacks a source file completely. What a mess, I'm going to rewrite it... – Ariser Aug 30 '15 at 00:31
  • @NickGammon: More like passing to a real build process. The Arduino builds stuff in a way that is meant to help newbies, but it's disconcerting, to say the least, to those who are used to more traditional Unix ways of building code. Make, to mention one. – Igor Stoppa Aug 30 '15 at 18:06
  • @Ariser: you could have a look at this: http://www.instructables.com/id/How-to-get-started-with-Eclipse-and-AVR/?ALLSTEPS I'm not too fond of Eclipse, but it beats to a pulp the Arduino IDE hands down any day. And if you have an AVR Dragon, you can also ditch the serial prints and debug like it's supposed to be done on embedded devices: http://www.larsen-b.com/Article/315.html – Igor Stoppa Aug 30 '15 at 18:13
  • 2
    @IgorStoppa - yes it is intended for newbies, and you must surely admit that Make is not for the faint-hearted. The process is described in [a reply I made](http://arduino.stackexchange.com/questions/13178/classes-and-objects-how-many-and-which-file-types-i-actually-need-to-use-them). I note that Ariser now says that the library is a mess, which presumably lets the build process of the hook a bit. The IDE can't help it if people put all their functions into .h files. – Nick Gammon Aug 30 '15 at 20:34
  • 1
    @NickGammon: the build process is still a mess as it does (black) magic with headers and I consider it as an encouragement to people doing even more evil things with headers. – Igor Stoppa Aug 30 '15 at 20:39