We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,535 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Modify a variable in an "includes.h" file

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?

2
Contributors
4
Replies
16 Hours
Discussion Span
4 Months Ago
Last Updated
7
Views
Question
Answered
RockJake28
Light Poster
27 posts since Dec 2012
Reputation Points: 0
Solved Threads: 4
Skill Endorsements: 0

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.

deceptikon
Challenge Accepted
Administrator
3,425 posts since Jan 2012
Reputation Points: 822
Solved Threads: 473
Skill Endorsements: 56

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.

RockJake28
Light Poster
27 posts since Dec 2012
Reputation Points: 0
Solved Threads: 4
Skill Endorsements: 0

I added a file food.c containing just:

#include "includes.h"

bool toMakeFood;

Problem solved
Thanks deceptikon!

RockJake28
Light Poster
27 posts since Dec 2012
Reputation Points: 0
Solved Threads: 4
Skill Endorsements: 0
Question Answered as of 4 Months Ago by deceptikon

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. ;)

deceptikon
Challenge Accepted
Administrator
3,425 posts since Jan 2012
Reputation Points: 822
Solved Threads: 473
Skill Endorsements: 56

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.2974 seconds using 2.7MB