My project consists of a main ino file and a number of .h and .cpp files. This setup works fine with Arduino IDE and it compiles the complete sketch just fine.
However, when I include the Blynk header BlynkSimpleEsp8266.h in more than one file, the compiler throws an error about Blynk being redefined in more than one place. I searched the forum about this and found a post that mentions the same issue. The solution mentioned is to include the header only in the main .ino file. I strongly disagree with this approach, as this means I would literally have to copy paste all .h and .cpp files into the main .ino file.
For instance, I have a couple of sub-modules in the code. One DEBUG module outputs data on some specific virtual pins. I have encapsulated this code in a separate .cpp file and included BLYNK_READ_XX handler in there. Same for other modules. But if I can’t include BlynkSimpleEsp8266.h in multiple places, the symbol Blynk and all BLYNK_XYZ macros will be undefined.
Is there any way to be able to define include these headers in more than one file?
I recall another topic with a big “don’t want to do it this way” run around… but end result is to use the one main .ino file with all the #include headers and then using IDE tabs (sorry I don’t know all the proper technical terms) with their associated files, like this…
I’m the OP of the topic @Gunner is referring to. It is not “fixed” fixed - and it may be that in C++ it can’t be done the way @AweSIM and I expected to do it. I am able work around it by limiting the ESP8266 communication to one module and providing more flexible wrapper-functions that may be declared and called from anywhere else in the project. It’s one layer of code away from the preferred solution but it works.
This was indeed the topic I was referring to. I came across it while searching for a way around. Usually, when working in C++ or C, you tend to keep your declarations in header files and definition code in CPP files. That way, you’re free to include the headers anywhere as many times as you want.
However, in case of the Blynk library, you’ve got all the class declarations and definitions right inside the header files. So if you include the header more than once, you’ll get multiple defined symbols error. The workaround I posted was ugly (but works nonetheless) and since I am able to keep my “modules” separate without cluttering the main INO file, I am satisfied. =)