1

I'm working on a program for an ESP8266 and it's getting a little cluttered to have everything in one source file, so I decided to break it up, but I'm unsure of how to go about properly structuring the different files.

My current structure is a main.cpp file that contains my void loop() and void setup(), with no includes. Then, I have a separate file named effects.cpp with functions that will be different LED effects, and a corresponding effects.h file. Again no includes. Finally, I have a globals.h file that contains the following:

#ifndef GLOBAL_H
  #define GLOBAL_H

  #include <ESP8266WiFi.h>
  #include <DNSServer.h>
  #include <WiFiManager.h>
  #include <FastLED.h>
  #include <BlynkSimpleEsp8266.h> //library for blynk functions

  #define NUM_LEDS 150 //Number of LEDs on the strip
  #define DATA_PIN D5
  #define CLOCK_PIN D6
  #define LED_TYPE APA102 //Change this to the chipset of the LED strip you're using
  #define BRIGHTNESS 84

  extern char auth[]; //stores API token for Blynk
  extern int pinValue; //stores state of button from Blynk app
  CRGB leds[NUM_LEDS];
#endif

So then I added an #include <globals.h> to both main.cpp and effects.cpp. My understanding was that with this structure, both of those files would be able to refer to the same libraries and the same variables as declared in globals.h. This seems to be working for the most part, but I keep getting errors saying that "Blynk" has multiple definitions (first occurrence in effects.cpp, second in main.cpp). I get the same error for "leds".

I haven't defined "Blynk" anywhere in my code, it's an external library, so I'm not sure what the issue is. The code exactly as it is works fine if it's all in one file. I also can't use extern before CRGB leds[NUM_LEDS]; in my globals.h file because CRGB is not a recognized variable type.

Can anyone point me in the right direction, what am I doing wrong?

5
  • #include <BlynkSimpleEsp8266.h> //library for blynk functionsI suspect that someone may have foolishly placed a Blynk in that header. Don't have the header so I can't confirm, but that's the first place I'd look. Jan 23, 2018 at 0:55
  • The header can be found here: github.com/blynkkk/blynk-library/blob/master/src/… but I don't see a stray "Blynk" in that header or the other headers it references. Should I just be looking for a variable named "Blynk"?
    – vashp2029
    Jan 23, 2018 at 1:05
  • Are you by any chance using #include to include .cpp files?
    – user2100815
    Jan 23, 2018 at 1:08
  • 1
    Ideally you should reduce this to a minimal complete example. If you start to remove your defines and the includes you will start to see where your problem is.
    – Tas
    Jan 23, 2018 at 1:09
  • @NeilButterworth I'm not including any .cpp files, only .h files, the problem is that the BlynkSimpleEsp8266.h file contains a declaration for "Blynk" in it already, as @DavidHoadley mentioned below. I just don't understand why this would be an issue when I separate out my code but work fine when everything is within one file.
    – vashp2029
    Jan 23, 2018 at 1:45

2 Answers 2

2

The header file you include (BlynkSimpleEsp8266.h) contains the following at lines 90 to 92

static WiFiClient _blynkWifiClient;
static BlynkArduinoClient _blynkTransport(_blynkWifiClient);
BlynkWifi Blynk(_blynkTransport);

all of which look sus to me. The last one looks like the one which gives you your error message. These three lines create objects in every cpp file that includes this header. They should not be in a header file. Move them in to one of your cpp files.

5
  • Hmm, any idea why would this be problematic only when I have my files separated as I described, but work fine if I throw all the code into the main.cpp file?
    – vashp2029
    Jan 23, 2018 at 1:46
  • 1
    Yes. If you include BlynkSimpleEsp8266.h in one cpp file only, you will get only one instance of Blynk. But if you include it in more than one file, you will get an instance of Blynk in each one. And they all have the same name. This is why the writer of the header file has made an error. You declare classes, types, functions and the like in headers, but you do not create objects or define variables. Those are to be done in c or cpp files. To define an object or variable in one C or cpp file, and use it in another, put an "extern" declaration of it in a header, not an actual definition. Jan 23, 2018 at 6:34
  • @vashp2029 Just padding out David's comment a little here. The first two lines are fine because they are static and will be bound to the translation unit that includes them. The third might be an attempt at a function prototype, but... it winds up giving you a violation of the One Definition Rule as soon as two cpp files include the header. SergeyA gives a detailed write-up here: stackoverflow.com/questions/34614523/… Jan 23, 2018 at 19:07
  • Note: you are walking yourself into the same error with CRGB leds[NUM_LEDS]; You can save yourself some debugging in the near future by reading How to correctly use the extern keyword in C (C and C++ are close enough here) and When to use extern in C++. A more complete discussion can be found here: en.cppreference.com/w/c/language/storage_duration Jan 23, 2018 at 19:14
  • Thank you so much guys, this really helped me out! I ended up making a globals.cpp and used that for includes rather than a header file, and I alos fixed the CRGB leds issue by defining it only in main.cpp. As it turns out, that's the only place I needed it. Thank you!
    – vashp2029
    Jan 23, 2018 at 20:21
0

Each cpp file (compilation unit) is compiled separately to an object file and then linked. The definitions are not remembered across compilation unit boundaries. So it can be a linker multiple definition error.

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.