By including
#include <BlynkSimpleEsp8266.h>
Blynk library (https://github.com/blynkkk/blynk-library) from more than 1 module/cpp file, "Multiple definition of `Blynk’" error is thrown at compile time. The header file contains following:
static WiFiClient _blynkWifiClient;
static BlynkArduinoClient _blynkTransport(_blynkWifiClient);
BlynkWifi Blynk(_blynkTransport);
How can I get the Blynk declarations into a second or third module, without multiple definitions error? Editing the header/library file/s is not an option!
By the question in How to create global variables/includes/functions for use in multiple source files it is still not clear what a working solution is...
1 Answer 1
The Blynk "simple" header files are not ready to be included into multiple cpp files. They are meant to be included only in the main ino file. The reason is that the variable allocations are in headers file, which is bad practice for C and C++. The h files should contain only declarations. The right way would have
extern BlynkWifi Blynk;
and the lines which are now in the h file should go in a cpp file. Then every cpp file that includes the .h will know about the variables but the variables would be allocated only once.
The fix is to change the library or to copy the BlynkSimpleEsp8266.h include to your project as for example MyBlynk.h and change the Blynk variable in MyBlynk.h to extern
and add a MyBlynk.cpp file with variables allocation
#include "MyBlynk.h"
WiFiClient _blynkWifiClient;
BlynkArduinoClient _blynkTransport(_blynkWifiClient);
BlynkWifi Blynk(_blynkTransport);
-
As good as Blynk is - and it is; I use it for many of my projects - and even though it is actively being maintained and improved, this issue has remained uncorrected. It is one of the few blemishes in an otherwise incredibly useful project.JRobert– JRobert2018年11月30日 17:03:18 +00:00Commented Nov 30, 2018 at 17:03
-
it is a compromise. that is why it is called "simple". they can't create cpp for this, because the builder would try to compile all the cpp for different networking libraries and board architectures. they could make ifdefs for architectures but not for libraries2018年11月30日 17:06:04 +00:00Commented Nov 30, 2018 at 17:06
extern
keyword in header file and instance must be defined in .cpp file (without extern). The extern means this variable is placed somewhere else.