Blynk multiple definitions

Hi,

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?

Thanks,
Asim

I hacked my way around this issue with this ugly solution… =/ I would love to hear a more elegant idea about doing this though.

  1. In the header BlynkSimpleEsp8266.h, replace
    BlynkWifi Blynk(_blynkTransport);
    with
    extern BlynkWifi Blynk;

  2. In the main .ino file, right below the #include <BlynkSimpleEsp8266.h>, add
    BlynkWifi Blynk(_blynkTransport);

  3. Shamelessly include the BlynkSimpleEsp8266 header in as many places as you want with no remorse.

I hope there’s a better solution though. If not, this might help someone else out too. =)

Asim

1 Like

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…

Not entirely sure if your issue is the same or not, but hopefully this info helps in your determination on how to use it, or work around it.

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.

1 Like

Hi Gunner,

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. =)

1 Like