1,105,281 Community Members

Modify a variable in an "includes.h" file

Member Avatar
RockJake28
Light Poster
27 posts since Dec 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
0
 

I have tons of files in this project as I am programming an ARM board. Making a tilt snake game where the snake is controlled by an accelerometer.
I am using a timer interupt (handled by timer0.c) to toggle a boolean variable extern bool toMakeFood; stored in "includes.h" that tells "main.c" if food should be displayed or not.
The problem I am having is that timer0.c throws a "no definition for toMakeFood" error while main.c has no problem. They both have the line #include "include.h"

Heres the code although I have ommited irrelevant parts as like I said, this is a big(ish) project.

main.c

#include "includes.h"

//more code here, can give you everything if you really want

void makeFood() {

  if (toMakeFood == true) {

    displayFood(); //not actually implemented, just used to demo this parts use

  }

}

timer0.c

#include "includes.h"

//more code here

void Timer0IntrHandler (void)
{

/* clear interrupt */
  T0IR_bit.MR0INT = 1;
  VICADDRESS = 0;

  if (count % 2 == 0) {
    toMakeFood = true;
    count++;
  }
  else {
    toMakeFood = false;
    count++;
  }


}

//more code here

includes.h

//loads of includes here

extern bool toMakeFood;

//loads of includes here

main.c has no problem and compiles
timer.0 causes the error shown above

Any ideas as to why?

Member Avatar
deceptikon
Eternally Awesome
4,675 posts since Jan 2012
Reputation Points: 1,341 [?]
Q&As Helped to Solve: 681 [?]
Skill Endorsements: 104 [?]
Administrator
Featured
 
1
 

toMakeFood doesn't exist because everywhere you have a declaration of it, you're saying "defined elsewhere" due to the extern keyword. Add an includes.c file with this:

bool toMakeFood = false;

And don't forget to link that file in with the rest. The header will provide multiple delcarations while the .c file will provide a single definition. On a side note, I strongly suspect you're compiling as C because bool and true/false require the inclusion of the stdbool.h header.

Member Avatar
RockJake28
Light Poster
27 posts since Dec 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
0
 

I am using C. I've included stdbool.h, its in the "includes.h" file.
What you're saying makes sense, but I dont understand why main.c is accepting the existance of toMakeFood and timer0.c is not.

Member Avatar
RockJake28
Light Poster
27 posts since Dec 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
0
 

I added a file food.c containing just:

#include "includes.h"

bool toMakeFood;

Problem solved
Thanks deceptikon!

Question Answered as of 1 Year Ago by deceptikon
Member Avatar
deceptikon
Eternally Awesome
4,675 posts since Jan 2012
Reputation Points: 1,341 [?]
Q&As Helped to Solve: 681 [?]
Skill Endorsements: 104 [?]
Administrator
Featured
 
0
 

I am using C. I've included stdbool.h, its in the "includes.h" file.

Booya. It's nice to see someone taking advantage of more recent revisions of the C standard.

but I dont understand why main.c is accepting the existance of toMakeFood and timer0.c is not.

That's simply where the lack of a definition is reported. If you remove timer0.c entirely from the project then you'll still get the error because toMakeFood really doesn't exist. You've only provided a name and a type in includes.h, not an actual object that can be worked with.

And the reason why you shouldn't have a definition in includes.h is because then there would be multiple definitions of an object with the same name, which would still result in a linker error. ;)

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: