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?

Recommended Answers

All 4 Replies

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.

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.

I added a file food.c containing just:

#include "includes.h"

bool toMakeFood;

Problem solved
Thanks 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. ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.