New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"multiple definition of `Blynk'" error from multi-file project #312

Closed
jrobert-gh opened this Issue Apr 28, 2017 · 9 comments

Comments

Projects
None yet
6 participants
@jrobert-gh
Copy link

jrobert-gh commented Apr 28, 2017

Blynk library version: [0.4.4]
IDE: [I used Arduino for this MCVExample. I am an Eclipse user; Eclipse gets same result.]
IDE version: [1.6.12]
Board type: [Mega 2560 for this demo; same result with Uno]
Additional modules: [None]

Scenario, steps to reproduce

[What you are trying to achieve and you can't?]
In a multi-file project, I want to communicate with Blynk from more than one of the files.

  • My main file, a .ino file, calls Blynk.run() in the loop function.
  • Module1 wants to write to one or more Blynk virtual pins. To to do so, it has to #include the same Blynk .h files as the main file did (for the declaration of .virtualWrite()).

Expected Result

[What are you expecting to happen as the consequence of above reproduction steps?]

  • No compile errors.
  • loop(), in the main file, can call Blynk.run()
  • Code in other modules can call Blynk library functions, Blynk.virtualWrite(), in my case.

Actual Result

[What actually happens after the reproduction steps? Include the error output or a link to a gist if possible.]

The project draws the following errors from ld. A Minimum, Complete, and Verifiable Example is attached as a .zip file.
BlynkDemoMultiDef-MCVE.zip

Note that some commenters have offered a work-around that takes advantage of the IDE's non-compliance to the language specs. Specifically, by removing the #includes from modules other than the main one and depending on the IDE's non-compliant concatenation of files. It is still a work-around; in any compliant environment, removal of the #includes results in Undefined errors.

/var/folders/z1/lg_25rqj0_g_mhx8_mtrmbt80000gp/T/arduino_build_772884/sketch/BlynkDemoMultiDef.ino.cpp.o (symbol from plugin): In function blynk_dbg_print(char const*, ...)': (.text+0x0): multiple definition of Blynk'
/var/folders/z1/lg_25rqj0_g_mhx8_mtrmbt80000gp/T/arduino_build_772884/sketch/BlynkDemoMod1.cpp.o (symbol from plugin):(.text+0x0): first defined here
/Applications/Arduino.app/Contents/Java/hardware/tools/avr/bin/../lib/gcc/avr/4.9.2/../../../../avr/bin/ld: Disabling relaxation: it will not work with multiple definitions
collect2: error: ld returned 1 exit status
Using library BlynkESP8266_Lib in folder: /Users/jrobert/Projects/ArduinoWksp/libraries/BlynkESP8266_Lib (legacy)
Using library Blynk at version 0.4.4 in folder: /Users/jrobert/Projects/ArduinoWksp/libraries/Blynk
exit status 1
Error compiling for board Arduino/Genuino Mega or Mega 2560.

@Gun-neR

This comment has been minimized.

Copy link

Gun-neR commented Apr 29, 2017

I honestly do not understand how you got where you did, but this is the correct, and fully compilable, way to have a tabbed sketch of your example.

BlynkDemoMultiDef.zip

@WhizzCat

This comment has been minimized.

Copy link

WhizzCat commented Apr 29, 2017

I came to the same conclusion as gunner. I've tried using the CPP file, but the Arduino IDE does not support that in a sketch. It only has that for libraries somehow. It's just not fully C/C++ compliant unfortunately because the Arduino programming language is based on C/C++, it's not the same :)

@vshymanskyy

This comment has been minimized.

Copy link
Collaborator

vshymanskyy commented May 2, 2017

Confirmed, It's still usable with multi-tab projects.

@vshymanskyy vshymanskyy closed this May 2, 2017

@jrobert-gh

This comment has been minimized.

Copy link

jrobert-gh commented May 2, 2017

I have to take issue with that conclusion. It is usable in multi-tab ArduinoIDE projects because of a compensating bug or non-conformity in that one IDE. It does not bind correctly in a normal C/C++ environment - those with separate compilation of each module - because including the necessary Blynk declarations in more than one module results in more than one definition - memory assignment - of an object named Blynk.

I'm an Eclipse user. Eclipse behaves in the standard way. Are you suggesting we (whomever do not use the ArduinoIDE) are outcasts at Blynk?

@vshymanskyy

This comment has been minimized.

Copy link
Collaborator

vshymanskyy commented May 2, 2017

@jrobert-gh , it's OK - it should be super simple for you to modify the library for your needs.
If you come up with any widely-applicable ideas, your PR would be more than welcome

@majodi

This comment has been minimized.

Copy link

majodi commented Aug 10, 2018

I use PlatformIO with Visual Code and face the same issue. I just started with Blynk today and tried to implement it in a larger project. I didn't dive into it yet but why is Blynk only for Arduino IDE (as the benchmark for this)? I did throw Blynk in my main module just for testing and I really like it. Also very clean docs etc. Would be nice if other professional cpp environments were taken into account. But from what I understand here is that this is something we should do on our own. It should indeed be super simple...

@vshymanskyy

This comment has been minimized.

Copy link
Collaborator

vshymanskyy commented Aug 14, 2018

@majodi there's nothing restricting you from using it in other IDEs. You may adjust library headers to match your needs.

@majodi

This comment has been minimized.

Copy link

majodi commented Aug 15, 2018

@vshymanskyy, as I said, I understand that we should adjust things ourself. No problem, I could get rid of the Blynk definition in the header file and define the object where ever I want. But I would rather keep my hands of the library and let PlatformIO do updates when new versions are available without redoing my change(s). Plus I don't want to search for other possible references to the hardcoded "Blynk" object definition.
So for now I just put everything related to Blynk in a separate .h/.cpp file with wrapper functions so that I can have a clean implementation and work with Blynk just the same way as with every other library.

@DaleSchultz

This comment has been minimized.

Copy link

DaleSchultz commented Dec 31, 2018

Here is a minimal implementation of a demo class file. It seems this is not to do with multiple tabs, but rather a class implementation.
I can find no way to call Blynk form the class file.
@vshymanskyy when you say adjust headers, can you give an example that can make this work?

Main sketch, Morse.ino, Morse.cpp and Morse.h
Morse.zip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment