Pages: [1]   Go Down
Author Topic: #define of a #include  (Read 853 times)
0 Members and 1 Guest are viewing this topic.
Denver
Offline Offline
God Member
*****
Karma: 19
Posts: 778
Inactive - PM
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Just a simple question for those who know more.

Given this code
Code:
// This construct doesn't work. Wire.h will be included no matter what.
// So 1244 bytes are always added unless the #include is commented out.

#define ADD_WIRE   false

#if (ADD_WIRE)
#include <Wire.h> // <<< can only comment out to not include
#endif

void setup(){
}

void loop(){
}

I was surprised to find that Wire.h is always included regardless of the setting of the #define.
If it's not the right way to do this, is there a way that works?
Logged

"Data is not information, information is not knowledge, knowledge is not understanding, understanding is not wisdom."
~ Clifford Stoll

Offline Offline
Edison Member
*
Karma: 18
Posts: 1170
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Look at #ifdef

preprossesor math is really limited.
Logged

Denver
Offline Offline
God Member
*****
Karma: 19
Posts: 778
Inactive - PM
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Like this?
Code:
//#define ADD_WIRE

#ifdef ADD_WIRE
#include <Wire.h> // <<< can only comment out to not include
#endif
Same thing.
Logged

"Data is not information, information is not knowledge, knowledge is not understanding, understanding is not wisdom."
~ Clifford Stoll

Offline Offline
Edison Member
*
Karma: 18
Posts: 1170
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Are all the references to the wire library #ifdef'd out? The IDE might be smart enough to add in Wire.h if it sees any reference to the Wire library. Also, try a #undefine right before your #ifdef.

What we need is a clean button.

Try a new test script that has never had wire.h defined.
Logged

Denver
Offline Offline
God Member
*****
Karma: 19
Posts: 778
Inactive - PM
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The sketch I posted is the entire sketch.
Logged

"Data is not information, information is not knowledge, knowledge is not understanding, understanding is not wisdom."
~ Clifford Stoll

Offline Offline
Edison Member
*
Karma: 18
Posts: 1170
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am saying start a new one that has never had wire.h included.

Then add the code that should prevent wire.h from being included.
Then add the #define to include wire.h
Then try to uninclude wire.h

The ide might be doing some stupid magic behind teh scenes and refuses to let go of wire.h.
Logged

Offline Offline
Edison Member
*
Karma: 18
Posts: 1170
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It looks like processing breaks the standard preprocessor. Note that the only directive mentioned in the reference is #define.

I could not even include Wire.h in a separate include file, it had to go into the main file.

I think the only thing you can do is comment it out.
Logged

Offline Offline
Edison Member
*
Karma: 18
Posts: 1170
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

A little forum search led me to this:
http://www.a-control.de/arduino-fehler/?lang=en

Does that help?
Logged

Denver
Offline Offline
God Member
*****
Karma: 19
Posts: 778
Inactive - PM
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks Keith, I appreciate your help.

I tried adding
Code:
/*
 * BOF preprocessor bug prevent
 * insert me on top of your arduino-code
 */
#define nop() __asm volatile ("nop")
#if 1
nop();
#endif
/*
 * EOF preprocessor bug prevent
*/

before the sketch I first posted with the same results - 1710 bytes and 466 bytes if the #include is commented out.
Unless I missed something in that example, that should be the workaround.

It appears that the precompiler makes one pass and seems to handle #includes before #defines.
Logged

"Data is not information, information is not knowledge, knowledge is not understanding, understanding is not wisdom."
~ Clifford Stoll

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46063
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
It appears that the precompiler makes one pass and seems to handle #includes before #defines.
Are you looking at the .ino file or the .cpp file that the IDE constructs from the .ino file? The IDE does some "unusual" things to convert a .ino file to a .cpp file.
Logged

UK
Offline Offline
Shannon Member
****
Karma: 184
Posts: 11173
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Same thing.

When I compile this sketch with 1.0.4, it fails with:
Quote
sketch_apr24a.ino: In function 'void setup()':
sketch_apr24a:7: error: 'Wire' was not declared in this scope

This indicates that Wire.h was not included.

Code:
#ifdef ADD_WIRE
#include <Wire.h>
#endif

void setup()
{
  Wire.begin();
}

void loop()
{
}

This sketch compiles without error:
Code:
#define ADD_WIRE
#ifdef ADD_WIRE
#include <Wire.h>
#endif

void setup()
{
  Wire.begin();
}

void loop()
{
}

That's what you're trying to achieve, isn't it?
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Denver
Offline Offline
God Member
*****
Karma: 19
Posts: 778
Inactive - PM
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Not exactly.
I'm trying to save the 1244 bytes that Wire.h adds to the sketch even when it's not defined.
The fact that it's not "compiled in enough" to satisfy Wire.begin is interesting, buy if you try the sketch as I have it in the first post,
compile it and note record the sketch size,
and then comment out the #include and note the sketch size again, you will see a 1244 byte difference.

I'm using this construct in another sketch that is close to the wall, and I'd like to avoid this overhead by setting a #define.
That's what I'm trying to achieve but it's looking unlikely.

Logged

"Data is not information, information is not knowledge, knowledge is not understanding, understanding is not wisdom."
~ Clifford Stoll

UK
Offline Offline
Shannon Member
****
Karma: 184
Posts: 11173
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The code I posted did not compile in Wire.h at all. However, I think what you're complaining about is actually the linker. For reasons which seem bizarre to me, the Arduino approach is to copy the sketch file and all associated code tabs, plus all the libraries and bits of the Arduino runtime framework which it references, into a temporary directory - and then compile and link everything. What's happening here is that the Arduino IDE is noticing the #include <wire.h> in your sketch, decided you are using the associated library, and including that library in the build. In theory the linker is smart enough to only link in the library functions that your code actually refers to, but in practice it seems to be pulling in quite a lot of junk in your case.

As far as I can see, the only options you have to avoid that are to comment out the #include to stop the IDE screwing up, or move to a grown up IDE.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Edison Member
*
Karma: 18
Posts: 1170
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You might be able to use a pre-preprocessor like M4 to add and remove comment tokens as necessary.
Logged

Denver
Offline Offline
God Member
*****
Karma: 19
Posts: 778
Inactive - PM
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Peter, that seems to describe what is happening. For me, the question is answered and I guess I'll have to just comment out that #include in addition to setting the #define false. Thanks for everyone's help.
Logged

"Data is not information, information is not knowledge, knowledge is not understanding, understanding is not wisdom."
~ Clifford Stoll

Pages: [1]   Go Up
Jump to: