When I #include the Blynk libraries from more than 1 module, I get multiple definitions at compile time. How can I get the Blynk declarations into a second or third module, without multiple definitions?
What do you mean by module??
You should only need to declare Blynk once based on your device/connection type.
e.g.
#include <BlynkSimpleStream.h> // for USB
#include <BlynkSimpleEsp8266.h> // for ESP
#include <BlynkSimpleEthernet.h> // for Ethernet
But NOT more than one at a time.
A module (in software) is a. cpp or other language code-file possibly paired with a definitions file (.h). My project is large enough that I’ve divided it into several modules. The .ino file is the main program with various related functions grouped into their own files. Each of them gets compiled by itself, then they are linked together into the complete hex file that gets uploaded to the Arduino.
It is the linking process that draws the error message; it finds two objects with the same name where there should only be one.
Well, I can’t say that I am completely following you on the separate “module” explanation… is that similar to multiple tabs in the IDE?
Either way, it is probably beyond my current programming knowledge. Can you provide more details… and you may have to show the code for others to grasp what you are trying.
When posting code, please format it as shown here.
Yes, when there is more than one file in a project, each one is shown in a separate tab. In my project, the .ino file’s ‘loop()’ function looks like
void loop(void)
{
Timer.run(); // update timers
Fsm.run(); // dispatch events
Blynk.run(); // update Blynk
}
, plus it opens the connection to the Blynk server.
Another module (file) needs to call Blynk to update a variables it manages:
static SMRESULT_T afUpdTemps(SMEVENT_T event)
{
char fxbuf[9+1];
q12p4toa(fxbuf, TemprSm[0]); // conv value to string
Blynk.virtualWrite(VTemp1, fxbuf); send to Blynk
return( Success );
}
The trouble comes when two files both try to
#include <BlynkSimpleShieldEsp8266.h>
The declarations in the #included .h file are needed in each source file to compile that its references to the Blynk Library, but it appears that the included file(s) also define a Blynk instance, leading to the multiple definitions. If that is the correct interpretation, the library should let the project define the Blynk instance(s) it needs.
OK, I have adjusted your title to better reflect the issue. There are some good programmers out there who might know what to do.
I think in Arduino the Tabs option is a really dumb implementation from their side. It just concatenates all files. You can use the IFDEF statement I think to check if a module is already defined or included.
I’ve updated post #3 to go into more detail and tweaked the title.
I’m not looking for a work-around for this; I already have that. I’m hoping the library will get fixed to prevent the issue in future. I probably should have posted this in Issues and Errors - my bad.
For future readers: One work-around is to assign one, and only one, of the modules as the Blynk interface. Only that module #includes the Blynk library or libraries you need, and it contains the only functions that directly talk or listen to Blynk, including helper-functions that you write specifically to communicate with Blynk on behalf of code in your other modules. It is awkward, because you lose some of the flexibility inherent in the design of Blynk’s library entries, unless you re-invent it your helper functions.
It all ends up on the same forum… but this doesn’t sound like a Blynk specific issue. Do all other libraries allow multiple repeat #includes across tabs without the IFDEF option as @Lichtsignaal suggested?
it is not dumb, if you know how it works and what it does (indeed, it just concatenates all files, in a specific order).
i use it all the time if a project is larger than 1-2 pages.
personally, i have a:
- main file (global variables, object inits, main loop comes here)
- a “header” file (here goes all the includes and defines)
- than separate file for every function (even for void setup)
i simply can not imagine how to handle a larger project without tabs in arduino ide…
with tabs, one can organize the code really well, and results much cleaner interface.
of course, you can not compare the arduino ide with jetbrain ides, but until someone will came up with a usable + easily configurable + smart ide for arduino, this is the best we can use.
for some time i used clion with arduino plugin, but never worked flawlessly, and it was a pain in the ass to set it up…
@wanek Good reference article Barely a few paragraphs in and I already found mention of this OP issue:
Essentially, you should only keep all variable definitions, setup() and loop() in your main .pde file, and disperse all your functions into their separate .pde files…
personally for me, that article was one of the biggest help in code organisation with arduino ide in larger projects. it is strange in the beginning, but after you got used to, it is gold.
@JRobert I have tested the Arduino IDE multi-tab setup with my large Arduino Mega 2560 Blynk Testbed Project (24.3KB and 8 tabs full of various Blynkified sensors, relay, servo and LED functions - and growing ).
No “workarounds” required. I just simply cut and pasted any standard loops and Blynk Function loops into their associated tabs. All #defines
, setup()
and void loop()
stay in the primary tab.
Aside from minimising the vertical scrolling necessary to move through my sketch, everything works normally and compiles fine. No changes required to how I programed anything.
So basically, as long as you are using the IDE tabs correctly and DON’T duplicate any of your #defines
, variable declarations, etc. across the tabs (there should be absolutely no need to anyhow), you will be fine.
When I work in the IDE (which is not often) I create a Tab for every function I make. This makes things pretty much neat and orderly.
All the setup and run-once stuff I put in the main INO file. I usually add a last Tab with a text file used for documentation called zz_Documentation.ino or something, so it ends up in the back
I appreciate everyone’s willingness to help, and you’ve found some useful guidelines for creating multi-file projects. However those do not address the issue I raised and it is not yet solved. Will whomever marked it so please un-mark it.
I have submitted a full issue report on GitHub (Issue #312), including a minimum, complete, and verifiable example, and an analysis of what I believe causes it.
I "fixed’ your example the correct multi-tabbed way and posted it back on Github. Compiles just fine now.
PS, delete that BlynkDemoMod1.cpp
file before you open the rest into the IDE or you will be right back where you started. If you open the .ino file in the IDE without deleting that .cpp file, then delete it after the fact, you will have to close and reopen the IDE before the rest will compile.