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?
-
See 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– Nick Gammon ♦2015年08月29日 22:50:14 +00:00Commented Aug 29, 2015 at 22:50
-
@NickGammon Interesting roundup of the whole thing. bookmarked now.Ariser– Ariser2015年08月30日 00:34:44 +00:00Commented Aug 30, 2015 at 0:34
1 Answer 1
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.
-
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– Ariser2015年08月29日 22:20:01 +00:00Commented Aug 29, 2015 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– Majenko2015年08月29日 22:43:03 +00:00Commented Aug 29, 2015 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.2015年08月29日 22:52:48 +00:00Commented Aug 29, 2015 at 22:52 -
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. 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.2015年08月30日 20:34:42 +00:00Commented Aug 30, 2015 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– Igor Stoppa2015年08月30日 20:39:49 +00:00Commented Aug 30, 2015 at 20:39