Header/CPP files with Arduino

Hi,

Its been a bit since I've done some code (yikes) and I'm having a few issues incorporating multiple files in my arduino project.

I have a main file which begins:

//#include "GlobalVars.h"
#include "InitFile.cpp"
#include "SPI.h"

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial1.begin(9600); //XBEE
  Serial2.begin(9600);  
  InitIO();

And I have a header file that begins like:

#ifndef GlobalVars_h
#define GlobalVars_h

#include "Arduino.h"
#include "SPI.h"


int LEDState1, LEDState2, LEDState3, LEDState4;

signed long ENC1COUNT;
signed long ENC2COUNT;
signed long ENC3COUNT;
signed long ENC4COUNT;
signed long ENC5COUNT;
signed long ENC6COUNT;
signed long ENC7COUNT;
signed long ENC8COUNT;
signed long ENC9COUNT;
signed long ENC10COUNT;

and I have a cpp file which begins like:

#include "GlobalVars.h"

ENC1COUNT = 0;
ENC2COUNT = 0;
ENC3COUNT = 0;
ENC4COUNT = 0;
ENC5COUNT = 0;
ENC6COUNT = 0;
ENC7COUNT = 0;
ENC8COUNT = 0;
ENC9COUNT = 0;
ENC10COUNT = 0;

The header has variables which are needed by the main body of code and the function file.

I'm getting the error (on InitFile.cpp):

"ENC1COUNT does not name a type" etc. etc. for all the variables.

If I initialize in the header file-I cannot get compilation either.

What am I setting up wrong with this? How should I be setting this type of thing up with the include files?

Thanks.

Just as an update:

My file with #include "GlobalVars.h" gives the same errors whether or not the header file include is commented out. But throws no "file not found" errors. Just errors like the file is not there.

Also tried upgrading my Arduino version with no luck.

edit: Oh, and I have the tabs open so all the files are part of the same project

edit: .h vars seem to be recognized when used in the .ino-just not the .cpp ... no errors if I comment everything out and put a ENC1COUNT = 0 in the main loop.

If you want to initialize a global I think you have to do that in the place it is declared.

You don’t have to initialize globals to zero because that is done automatically.

If you want to initialize the globals but not in the header file that declares them you could put the assignments in a function called from setup().

I was playing with this, and your's is the only way I could get it to work, John.

As soon as I removed the *.cpp file from the sketch folder, the errors went away and I could initialise "ENC1COUNT etc inside the setup() function with no errors. I could find no way to get the version with the cpp file to work.

Even adding a folder to my libraries folder, with the cpp and h files, then including the h file wouldn't work, but as soon as I deleted the cpp file, it worked.

ie. They need to be initialised inside a function.

I just learned something too - this is my first day with an Arduino. :)

Interesting. Well-I’ve moved over the initialization to the header and will deal with changing variables inside functions in the cpp-hopefully that will work. I’m still not compiling correctly-but it seems a slightly different issue:

edit: No-unfortunately (in addition to the errors below) still getting the multiple definitions of for the variables initialized in the header file and used inside of functions in the cpp file. Darn-I really would like to have multiple cpp files.

edit: fixed some of the stuff below by deleting my #include “InitFile.cpp”. Apparently ,arduino already knows to do the two together. Still no luck with the multiple defs.

Main:

#include "Arduino.h"
#include "SPI.h"
#include "GlobalVars.h"
#include "InitFile.cpp"


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial1.begin(9600); //XBEE
  Serial2.begin(9600);  
  
  //InitIO();

  initEncoders();
  
  //clearEncoderCounts();
   
  previousmillis = millis();

}

header:

#ifndef GlobalVars_h
#define GlobalVars_h

#include "Arduino.h"

const int LEDPin1 = 41;
const int LEDPin2 = 43;
const int LEDPin3 = 45;
const int LEDPin4 = 47;

const int SSpin1 = 31;
const int SSpin2 = 46;
const int SSpin3 = 27;
const int SSpin4 = 42;
const int SSpin5 = 33;

const int SSpin6 = 53;
const int SSpin7 = 48;
const int SSpin8 = 25;
const int SSpin9 = 29;
const int SSpin10 = 38;
int LEDState1, LEDState2, LEDState3, LEDState4;

signed long ENC1COUNT = 0;
signed long ENC2COUNT = 0;
signed long ENC3COUNT = 0;
signed long ENC4COUNT = 0;
signed long ENC5COUNT = 0;
signed long ENC6COUNT = 0;
signed long ENC7COUNT = 0;
signed long ENC8COUNT = 0;
signed long ENC9COUNT = 0;
signed long ENC10COUNT = 0;

cpp file:

#include "GlobalVars.h"
#include "Arduino.h"
#include <SPI.h>


//*****************************INITIALIZE ENCODERS**************************************************************************
void initEncoders( void ) 
{
  //Set up SPI
  pinMode(SSpin1, OUTPUT);
  pinMode(SSpin2, OUTPUT);
  pinMode(SSpin3, OUTPUT);
  pinMode(SSpin4, OUTPUT);
  pinMode(SSpin5, OUTPUT);
  pinMode(SSpin6, OUTPUT);
  pinMode(SSpin7, OUTPUT);
  pinMode(SSpin8, OUTPUT);
  pinMode(SSpin9, OUTPUT);
  pinMode(SSpin10, OUTPUT);
  
  digitalWrite(SSpin1,HIGH);
  digitalWrite(SSpin2,HIGH);
  digitalWrite(SSpin3,HIGH);
  digitalWrite(SSpin4,HIGH);
  digitalWrite(SSpin5,HIGH);
  
  digitalWrite(SSpin6,HIGH);
  digitalWrite(SSpin7,HIGH);
  digitalWrite(SSpin8,HIGH);
  digitalWrite(SSpin9,HIGH);
  digitalWrite(SSpin10,HIGH);
  
  SPI.begin();

Errors:

C:\Users\__\AppData\Local\Temp\build9113198105209894032.tmp\main.cpp.o: In function `initEncoders()':
C:\Users\__\AppData\Local\Temp\build9113198105209894032.tmp/InitFile.cpp:10: multiple definition of `initEncoders()'
C:\Users\__\AppData\Local\Temp\build9113198105209894032.tmp\InitFile.cpp.o:C:\Users\__\AppData\Local\Temp\build9113198105209894032.tmp/InitFile.cpp:10: first defined here
c:/program files (x86)/arduino/hardware/tools/avr/bin/../lib/gcc/avr/4.8.1/../../../../avr/bin/ld.exe: Disabling relaxation: it will not work with multiple definitions
C:\Users\__\AppData\Local\Temp\build9113198105209894032.tmp\main.cpp.o: In function `initEncoders()':
C:\Users\__\AppData\Local\Temp\build9113198105209894032.tmp/InitFile.cpp:10: multiple definition of `previousmillis'
C:\Users\__\AppData\Local\Temp\build9113198105209894032.tmp\InitFile.cpp.o:C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\SPI/SPI.h:209: first defined here

To respond to the suggestions of putting the inits in a function:

tried an initialize with an InitVars ( void ) function in the cpp and changed those to be just defined in the header-still get the multiple definitions error-which does not make sense to me.

Header:

// ...
extern const int var1;
extern int var2;
// ...

tells that these variables are defined somewhere.

Actual definitions in a cpp file:

// ...
const int var1 = value1;
int var2 = value2;
// ...

HABalloonGirl: To respond to the suggestions of putting the inits in a function:

tried an initialize with an InitVars ( void ) function in the cpp and changed those to be just defined in the header-still get the multiple definitions error-which does not make sense to me.

When I did that, I deleted the cpp file altogether. Declared in the header, initialised in the main file. The reply above this sounds like the right way to do what you want.

Ah. Awesomeness-thank you. Yeah-that does it. Thanks all!

(I also discovered that if you do not need any c++ things, you can just make the cpp an ino and the code compiles as is)

HABalloonGirl: Ah. Awesomeness-thank you. Yeah-that does it. Thanks all!

(I also discovered that if you do not need any c++ things, you can just make the cpp an ino and the code compiles as is)

I've found that all of the C++ stuff that I've tried has worked fine too, in an ino file.

I just did a test with a class declaration in a header and the implementation at the bottom of the main ino file and it compiles fine.

This is how I got it to compile:
MAIN.ino

 #include "Arduino.h"
#include "SPI.h"
#include "GlobalVars.h"
extern void initEncoders(void);
//#include "InitFile.cpp"

unsigned long previousmillis;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial1.begin(9600); //XBEE
  Serial2.begin(9600);

  //InitIO();

  initEncoders();

  //clearEncoderCounts();

  previousmillis = millis();
}

void loop() {}

InitFile.cpp

#include "GlobalVars.h"
#include "Arduino.h"
#include <SPI.h>

signed long ENC1COUNT = 0;
signed long ENC2COUNT = 0;
signed long ENC3COUNT = 0;
signed long ENC4COUNT = 0;
signed long ENC5COUNT = 0;
signed long ENC6COUNT = 0;
signed long ENC7COUNT = 0;
signed long ENC8COUNT = 0;
signed long ENC9COUNT = 0;
signed long ENC10COUNT = 0;

//*****************************INITIALIZE ENCODERS**************************************************************************
void initEncoders( void ) {

  pinMode(SSpin1, OUTPUT);
  pinMode(SSpin2, OUTPUT);
  pinMode(SSpin3, OUTPUT);
  pinMode(SSpin4, OUTPUT);
  pinMode(SSpin5, OUTPUT);
  pinMode(SSpin6, OUTPUT);
  pinMode(SSpin7, OUTPUT);
  pinMode(SSpin8, OUTPUT);
  pinMode(SSpin9, OUTPUT);
  pinMode(SSpin10, OUTPUT);

  digitalWrite(SSpin1, HIGH);
  digitalWrite(SSpin2, HIGH);
  digitalWrite(SSpin3, HIGH);
  digitalWrite(SSpin4, HIGH);
  digitalWrite(SSpin5, HIGH);

  digitalWrite(SSpin6, HIGH);
  digitalWrite(SSpin7, HIGH);
  digitalWrite(SSpin8, HIGH);
  digitalWrite(SSpin9, HIGH);
  digitalWrite(SSpin10, HIGH);
  
  //Set up SPI
  SPI.begin();
}

GlobalVars.h

#ifndef GlobalVars_h
#define GlobalVars_h

#include "Arduino.h"

const int LEDPin1 = 41;
const int LEDPin2 = 43;
const int LEDPin3 = 45;
const int LEDPin4 = 47;

const int SSpin1 = 31;
const int SSpin2 = 46;
const int SSpin3 = 27;
const int SSpin4 = 42;
const int SSpin5 = 33;

const int SSpin6 = 53;
const int SSpin7 = 48;
const int SSpin8 = 25;
const int SSpin9 = 29;
const int SSpin10 = 38;

extern int LEDState1, LEDState2, LEDState3, LEDState4;

extern signed long ENC1COUNT;
extern signed long ENC2COUNT;
extern signed long ENC3COUNT;
extern signed long ENC4COUNT;
extern signed long ENC5COUNT;
extern signed long ENC6COUNT;
extern signed long ENC7COUNT;
extern signed long ENC8COUNT;
extern signed long ENC9COUNT;
extern signed long ENC10COUNT;
#endif

johnwasser:
This is how I got it to compile:
MAIN.ino

 #include "Arduino.h"

#include “SPI.h”
#include “GlobalVars.h”
extern void initEncoders(void);
//#include “InitFile.cpp”

unsigned long previousmillis;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial1.begin(9600); //XBEE
  Serial2.begin(9600);

//InitIO();

initEncoders();

//clearEncoderCounts();

previousmillis = millis();
}

void loop() {}




InitFile.cpp


#include “GlobalVars.h”
#include “Arduino.h”
#include <SPI.h>

signed long ENC1COUNT = 0;
signed long ENC2COUNT = 0;
signed long ENC3COUNT = 0;
signed long ENC4COUNT = 0;
signed long ENC5COUNT = 0;
signed long ENC6COUNT = 0;
signed long ENC7COUNT = 0;
signed long ENC8COUNT = 0;
signed long ENC9COUNT = 0;
signed long ENC10COUNT = 0;

//INITIALIZE ENCODERS*********************************************
void initEncoders( void ) {

pinMode(SSpin1, OUTPUT);
  pinMode(SSpin2, OUTPUT);
  pinMode(SSpin3, OUTPUT);
  pinMode(SSpin4, OUTPUT);
  pinMode(SSpin5, OUTPUT);
  pinMode(SSpin6, OUTPUT);
  pinMode(SSpin7, OUTPUT);
  pinMode(SSpin8, OUTPUT);
  pinMode(SSpin9, OUTPUT);
  pinMode(SSpin10, OUTPUT);

digitalWrite(SSpin1, HIGH);
  digitalWrite(SSpin2, HIGH);
  digitalWrite(SSpin3, HIGH);
  digitalWrite(SSpin4, HIGH);
  digitalWrite(SSpin5, HIGH);

digitalWrite(SSpin6, HIGH);
  digitalWrite(SSpin7, HIGH);
  digitalWrite(SSpin8, HIGH);
  digitalWrite(SSpin9, HIGH);
  digitalWrite(SSpin10, HIGH);
 
  //Set up SPI
  SPI.begin();
}




GlobalVars.h


#ifndef GlobalVars_h
#define GlobalVars_h

#include “Arduino.h”

const int LEDPin1 = 41;
const int LEDPin2 = 43;
const int LEDPin3 = 45;
const int LEDPin4 = 47;

const int SSpin1 = 31;
const int SSpin2 = 46;
const int SSpin3 = 27;
const int SSpin4 = 42;
const int SSpin5 = 33;

const int SSpin6 = 53;
const int SSpin7 = 48;
const int SSpin8 = 25;
const int SSpin9 = 29;
const int SSpin10 = 38;

extern int LEDState1, LEDState2, LEDState3, LEDState4;

extern signed long ENC1COUNT;
extern signed long ENC2COUNT;
extern signed long ENC3COUNT;
extern signed long ENC4COUNT;
extern signed long ENC5COUNT;
extern signed long ENC6COUNT;
extern signed long ENC7COUNT;
extern signed long ENC8COUNT;
extern signed long ENC9COUNT;
extern signed long ENC10COUNT;
#endif

It compiles well for me too, John. I copied this for reference, so that when I do need multiple files, once I’ve learned a bit more, I’ll know how to do it.
Thanks.