0

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

5
  • Possible duplicate of How to create global variables/includes/functions for use in multiple source files
    – Robert Andrzejuk
    Commented Nov 28, 2018 at 8:55
  • Like that answer already says:> If you include BlynkSimpleEsp8266.h in one cpp file only, you will get only one instance of Blynk.
    – Robert Andrzejuk
    Commented Nov 28, 2018 at 8:55
  • Whole magic of "possible duplicate" is the extern keyword in header file and instance must be defined in .cpp file (without extern). The extern means this variable is placed somewhere else.
    – KIIV
    Commented Nov 28, 2018 at 9:34
  • Please read my question - I need to include the lib in multiple modules/cpp files! And editing header file is out of scope...
    – partinis
    Commented Nov 28, 2018 at 17:27
  • You want to include header, that CAN NOT be included in more than one cpp file, in more than one cpp file without any change in that header file. You may think we don't read your question, but it's just because there is no other way than fix to the library using extern.
    – KIIV
    Commented Nov 28, 2018 at 22:38

1 Answer 1

4

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);
2
  • 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
    Commented 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 libraries
    – Juraj
    Commented Nov 30, 2018 at 17:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.