Passing #define between tabs


Is there a way of passing a #define between tabs in the arduino ide.

The example,

In my main i use "#define verbose", then below this I include my other tabs (which contain classes), however this definition doesn't get passed to them (I'm assuming because the compiler works on the .cpp's separately before then looking at pulling it all together?).

It's a long shot but if anyone knows if you can, or another way around that would be great.


#defines and #includes "happen" at exactly the location in the file where they occur. Hence, if you have

// your sketch
#define verbose
#include "stuff.h"

'verbose' will be defined in "stuff.h", but not in "stuff.cpp", thus:

// your sketch
#define verbose
#include "stuff.h"

// file stuff.h
// 'verbose' is defined here

//file stuff.cpp
#include "stuff.h"
// 'verbose' is not defined her

simply because #include means, well, a 1:1 text include of the referenced file contents, and stuff.cpp does not get included in your sketch.

You could create a top-level #include file with all #defines that need to be known in your different files, and then #include them accordingly:

// your sketch
#include "config.h"
#include "stuff.h"

// file stuff.h
#include "config.h"

// file stuff.cpp
#include "config.h"
#include "stuff.h"

// file config.h
#define verbose

Ah yes good idea, I'll give that a go. Cheers very much!

I'm using ideas for the debugging method however I'm having problems including the functionality in other tabs/classes.

// sketch.pde

#include "config.h"
#include "debug.h"
#include <Wire.h>
#include <Servo.h>
#include "ADC.h"

// config.h

#define DEBUG  // Prints everything to screen for debugging
#define data     // Forces getV and getI to pump out random gobbleijuke

#include <WProgram.h>

// conditional debugging

#ifdef DEBUG 

  #define BEGIN_DEBUG   Serial.begin(115200);
  #define TRACE(x)      pUSBdebug.print   (x)
  #define TRACE2(x,y)   pUSBdebug.print   (x,y)
  #define TRACELN(x)    pUSBdebug.println (x)
  #define TRACELN2(x,y) pUSBdebug.println (x,y)
  #define BEGIN_DEBUG   ((void) 0)
  #define TRACE(x)      ((void) 0)
  #define TRACE2(x,y)   ((void) 0)
  #define TRACELN(x)    ((void) 0)
  #define TRACELN2(x,y) ((void) 0)
#endif // DEBUG


class tUSBdebug : 
public Print
  virtual void write (const byte c)  { 
  }  // end of tUSBdebug::write
}; // end of tUSBdebug

// an instance of the USBdebug object
tUSBdebug USBdebug;

// ADC.h

#ifndef ADC_H
#define ADC_H
#include "config.h"

class Adc{
  void helloWorld();

// ADC.cpp

#include "config.h"
#include "ADC.h"

void Adc::helloWorld(){
  TRACE("Hello World!");  // 0 to 1023

The error I get, with TRACE("Hello World!") being called in ADC.cpp is:

ADC.cpp: In member function 'int Adc::getAdc(int)':
ADC.cpp:36: error: 'USBdebug' was not declared in this scope

However, if I try and #include "DEBUG.h" it repeatedly tries to define the class USBdebug and I get:

code.cpp.o:(.bss.USBdebug+0x0): multiple definition of 'USBdebug'
ADC.cpp.o:(.bss.USBdebug+0x0): first defined here

Any ideas for a solution, or another way of doing this?

Many thanks

Look at the difference between ADC.h and DEBUG.h.

ADC.h has a multiple include guard (the #ifndef, #define, and #endif stuff). DEBUG.h does not.

In the future, please post different files in different windows, to make it easier to see where the files actually start and end.

Yeah thanks for the spot. Unfortunately that doesnt help though.

The thing is quite complex...

I need all the .cpp's, or classes to include the DEBUG.h functionality. However, the debug class needs to be initiated before the .ccp's are compiled so they can use that functionality. But the way the compiler works buggers this up because it initiates the debug class in each .cpp, creates the .o files then realises it has multiple instances of debug.

There must be a way of doing this, however I suspect I'm going about it in the wrong way. It would work if it was in one almighty .pde file but that's daft lol.


There must be a way of doing this, however I suspect I'm going about it in the wrong way.

I think you are. In general, a header file should not contain executable code.

tUSBdebug USBdebug;

is executable code.

Move that code to the source file.

Yep I agree.

When I do that here though, the class isnt initiated when ADC.cpp is compiled, and if I initiate it in ADC.cpp and all the other objects it causes multiple instances. Unless I gave them each a different name and chews up the memory :frowning:

Ha, sorry. Ive just had a beer & a rest and realised I'm creating a class for a class, which is pointless and messing the whole thing up. Got rid of the problematic header, changed a few names and it now compiles.

Moral of the story, have beer earlier! lol.