Go Down

Topic: Problem with conditional #include (Read 2 times) previous topic - next topic

Nick Gammon

Here's the problem ...

The compile isn't done in your sketch directory. All the files in the sketch are copied to a temporary directory, along with any files mentioned in #include in the main sketch.

This is why you have to put (say) "#include <Wire.h>" in the main sketch, even if only a library uses it.

Now since the IDE doesn't honour preprocessor directives (and to be honest, it would probably be hard to) it copies all the files you mention to this temporary directory, and compiles them.

Hence the error message:

Code: [Select]

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


You'll notice that is a linker error message.

I don't know there is a way around it. I was tempted to suggest you put the conditional includes into a .cpp file (where they would be honoured) but then neither of the included files would be copied, and you would get missing symbol messages.

Nick Gammon

Maybe if you somehow made your own .h file (which gets copied into the temporary directory) and inside that you have the conditional directives which then include the files you really want, with a full pathname.

aarondc

#22
Jun 01, 2013, 05:13 am Last Edit: Jun 01, 2013, 05:16 am by aarondc Reason: 1
I was getting frustrated with exactly the same phenomenon, having seen the conditional Arduino vs WString include code somewhere, but having conditional includes not work with my own sketches.

I have been wondering how hard it would be to turn OFF the IDE "help" that happens, like creating prototypes, etc. A single option checkbox we could uncheck that would allow conditional #includes to work, mostly.

I am going down the

#include <one.h>
//#include <two.h>

route myself.
Windows serial port monitor: Tellurium | Arduino serial port debugging library: DBG | Cusom LCD char generator | Technical questions will only be answered in forum threads

WizenedEE


I was getting frustrated with exactly the same phenomenon, having seen the conditional Arduino vs WString include code somewhere, but having conditional includes not work with my own sketches.

I have been wondering how hard it would be to turn OFF the IDE "help" that happens, like creating prototypes, etc. A single option checkbox we could uncheck that would allow conditional #includes to work, mostly.
It would require more than an off switch to fix this problem. It's not that the IDE's helping is messing it up, it's that it's not being smart enough with the helping. If you turned it off then no -I or -l switches would be passed to gcc at all. Generally in IDEs there's separate configurations for the command line switches.

Nick Gammon


I have been wondering how hard it would be to turn OFF the IDE "help" that happens, like creating prototypes, etc. A single option checkbox we could uncheck that would allow conditional #includes to work, mostly.


If you make your own .cpp and .h files as part of the project, those files do not seem to be subjected to the IDE "help". For example, this file, which deliberately omits the function prototype:

Code: [Select]

void test ()
  {
  bar ();
  }
 
void bar ()
  {
  }


... gives me a compile error:

Code: [Select]

foo.cpp: In function 'void test()':
foo.cpp:3: error: 'bar' was not declared in this scope

Go Up