Grand Blanc, MI, USA
Online
Faraday Member
Karma: 43
Posts: 2518
"We're a proud service of the Lost Electricity Reclamation Agency"
|
 |
« on: November 25, 2012, 08:33:09 pm » |
I have two interchangeable libraries that implicitly define the same object (RTC) and are therefore mutually exclusive. I want to include one or the other, based on a #defined value. The code below seems to act as though both libraries are being included. If I explicitly comment one library or the other out with "//" then I can get it to compile. This seems simple, I must be missing something fundamental. #define RTC_TYPE 1
#include <Time.h> //http://www.arduino.cc/playground/Code/Time #include <Wire.h> //http://arduino.cc/en/Reference/Wire
#if RTC_TYPE == 1 #include <DS1307RTC.h> //http://www.arduino.cc/playground/Code/Time #else #include <MCP79412RTC.h> //https://github.com/JChristensen/MCP79412RTC #endif
void setup(void) { setSyncProvider(RTC.get); }
void loop(void) { }
Compiler output:
MCP79412RTC\MCP79412RTC.cpp.o:C:\Documents and Settings\Jack\My Documents\arduino\sketchbook\libraries\MCP79412RTC/MCP79412RTC.cpp:551: multiple definition of `RTC' DS1307RTC\DS1307RTC.cpp.o:C:\Documents and Settings\Jack\My Documents\arduino\sketchbook\libraries\DS1307RTC/DS1307RTC.cpp:112: first defined here
|
|
|
|
|
Logged
|
|
|
|
|
Indiana, US
Offline
Full Member
Karma: 12
Posts: 161
|
 |
« Reply #1 on: November 25, 2012, 08:50:02 pm » |
At a quick glance it looks ok. Unless one library includes the other in error (doubt it). Try this to see what the preprocessing is doing. Change the #define to see what happens... (I would, but I'm not near my compiler) #if RTC_TYPE == 1 #include <DS1307RTC.h> //http://www.arduino.cc/playground/Code/Time #error DS1307RTC #else #include <MCP79412RTC.h> //https://github.com/JChristensen/MCP79412RTC #error DS1307RTC #endif
|
|
|
|
|
Logged
|
There are 10 types of people in the world, those that understand binary, and those that don't.
|
|
|
|
Grand Blanc, MI, USA
Online
Faraday Member
Karma: 43
Posts: 2518
"We're a proud service of the Lost Electricity Reclamation Agency"
|
 |
« Reply #2 on: November 25, 2012, 09:44:05 pm » |
@patduino, I get one #error or the other, not both. Double-checked my library just to verify that it doesn't include the other, and it doesn't. At a quick glance it looks ok. Unless one library includes the other in error (doubt it). Try this to see what the preprocessing is doing. Change the #define to see what happens... (I would, but I'm not near my compiler) #if RTC_TYPE == 1 #include <DS1307RTC.h> //http://www.arduino.cc/playground/Code/Time #error DS1307RTC #else #include <MCP79412RTC.h> //https://github.com/JChristensen/MCP79412RTC #error MCP79412RTC #endif
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Dallas
Online
Shannon Member
Karma: 120
Posts: 10181
|
 |
« Reply #3 on: November 25, 2012, 09:44:56 pm » |
Linker error?
|
|
|
|
|
Logged
|
|
|
|
|
Grand Blanc, MI, USA
Online
Faraday Member
Karma: 43
Posts: 2518
"We're a proud service of the Lost Electricity Reclamation Agency"
|
 |
« Reply #4 on: November 25, 2012, 09:49:54 pm » |
I was thinking compiler... MCP79412RTC\MCP79412RTC.cpp.o:C:\Documents and Settings\Jack\My Documents\arduino\sketchbook\libraries\MCP79412RTC/MCP79412RTC.cpp:551: multiple definition of `RTC' DS1307RTC\DS1307RTC.cpp.o:C:\Documents and Settings\Jack\My Documents\arduino\sketchbook\libraries\DS1307RTC/DS1307RTC.cpp:112: first defined here
Linker error?
|
|
|
|
|
Logged
|
|
|
|
|
Indiana, US
Offline
Full Member
Karma: 12
Posts: 161
|
 |
« Reply #5 on: November 25, 2012, 09:51:06 pm » |
I get one #error or the other, not both. That implies that the preprocessor is handling the conditionals correctly. Hmmmmm. Do you have code somewhere else, like in another tab, that might be compiling too?
|
|
|
|
|
Logged
|
There are 10 types of people in the world, those that understand binary, and those that don't.
|
|
|
|
Grand Blanc, MI, USA
Online
Faraday Member
Karma: 43
Posts: 2518
"We're a proud service of the Lost Electricity Reclamation Agency"
|
 |
« Reply #6 on: November 25, 2012, 09:53:44 pm » |
I get one #error or the other, not both. That implies that the preprocessor is handling the conditionals correctly. Hmmmmm. Do you have code somewhere else, like in another tab, that might be compiling too? Nope, the code I posted is everything, single tab. The problem originally occurred in a larger project with multiple tabs, but I like to pare things down to the minimum that still exhibits the issue. Better for the forum I figure too.
|
|
|
|
|
Logged
|
|
|
|
|
Grand Blanc, MI, USA
Online
Faraday Member
Karma: 43
Posts: 2518
"We're a proud service of the Lost Electricity Reclamation Agency"
|
 |
« Reply #7 on: November 25, 2012, 09:59:59 pm » |
More info:
1. Checked the build folder, it contains sub-folders for both libraries with object files.
2. Tried it on another PC (new build) ... same results.
|
|
|
|
|
Logged
|
|
|
|
|
Grand Blanc, MI, USA
Online
Faraday Member
Karma: 43
Posts: 2518
"We're a proud service of the Lost Electricity Reclamation Agency"
|
 |
« Reply #8 on: November 25, 2012, 10:04:29 pm » |
Verbose compiler output attached.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
God Member
Karma: 14
Posts: 909
|
 |
« Reply #9 on: November 25, 2012, 10:05:23 pm » |
This looks like the infamous IDE trick of blowing #ifdef away. Try putting char junk; at the front of the file. Pete
|
|
|
|
|
Logged
|
|
|
|
|
Grand Blanc, MI, USA
Online
Faraday Member
Karma: 43
Posts: 2518
"We're a proud service of the Lost Electricity Reclamation Agency"
|
 |
« Reply #10 on: November 25, 2012, 10:13:33 pm » |
This looks like the infamous IDE trick of blowing #ifdef away. Try putting char junk; at the front of the file. Pete The library (.h) file?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
God Member
Karma: 14
Posts: 909
|
 |
« Reply #11 on: November 25, 2012, 10:40:04 pm » |
In your .ino file - it should work as long as it is in front of #if RTC_TYPE == 1 Pete
|
|
|
|
|
Logged
|
|
|
|
|
Grand Blanc, MI, USA
Online
Faraday Member
Karma: 43
Posts: 2518
"We're a proud service of the Lost Electricity Reclamation Agency"
|
 |
« Reply #12 on: November 25, 2012, 10:44:34 pm » |
In your .ino file - it should work as long as it is in front of #if RTC_TYPE == 1 Pete Hmmm, no change: char junk; #define RTC_TYPE 1
#include <Time.h> //http://www.arduino.cc/playground/Code/Time #include <Wire.h> //http://arduino.cc/en/Reference/Wire
#if RTC_TYPE == 1 #include <DS1307RTC.h> //http://www.arduino.cc/playground/Code/Time #else #include <MCP79412RTC.h> //https://github.com/JChristensen/MCP79412RTC #endif
void setup(void) { setSyncProvider(RTC.get); }
void loop(void) { } MCP79412RTC\MCP79412RTC.cpp.o:C:\Documents and Settings\Jack\My Documents\arduino\sketchbook\libraries\MCP79412RTC/MCP79412RTC.cpp:552: multiple definition of `RTC' DS1307RTC\DS1307RTC.cpp.o:C:\Documents and Settings\Jack\My Documents\arduino\sketchbook\libraries\DS1307RTC/DS1307RTC.cpp:112: first defined here
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Sr. Member
Karma: 11
Posts: 393
|
 |
« Reply #13 on: November 25, 2012, 10:58:49 pm » |
This is a well documented and well known failing of the Arduino IDE preprocessor. It doesn't respect (or simply ignores) the preprocessor conditionals, and will aggressively match any line starting with #include for a library inclusion.
It's been reported as a problem for years. Like many other problems with the IDE preprocessor, Team Arduino don't think it's a big deal, apparently, so it's not going to be fixed.
My solution is to simply not use the IDE to avoid the numerous bugs and problems in the IDE preprocessor altogether. I use makefiles that do the build. I need to declare my own prototypes, and put a list of user libraries I'm using in a makefile script for that project. Done. No IDE preprocessor. Real C/C++ preprocessor directives all work as expected again. Life is better that way.
|
|
|
|
|
Logged
|
|
|
|
|
Grand Blanc, MI, USA
Online
Faraday Member
Karma: 43
Posts: 2518
"We're a proud service of the Lost Electricity Reclamation Agency"
|
 |
« Reply #14 on: November 25, 2012, 11:08:01 pm » |
This is a well documented and well known failing of the Arduino IDE preprocessor. It doesn't respect (or simply ignores) the preprocessor conditionals, and will aggressively match any line starting with #include for a library inclusion.
It's been reported as a problem for years. Like many other problems with the IDE preprocessor, Team Arduino don't think it's a big deal, apparently, so it's not going to be fixed.
Understood, thanks. Guess I'd missed that one. My solution is to simply not use the IDE to avoid the numerous bugs and problems in the IDE preprocessor altogether. I use makefiles that do the build. I need to declare my own prototypes, and put a list of user libraries I'm using in a makefile script for that project. Done. No IDE preprocessor. Real C/C++ preprocessor directives all work as expected again. Life is better that way.
I've read various threads on this approach, and made a couple half-hearted attempts but didn't stick to it long enough to get it working. I'd probably be happier working that way, too. Declaring prototypes is another thing I don't appreciate the IDE doing for me; it doesn't always get it right.
|
|
|
|
|
Logged
|
|
|
|
|
|