Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Multipule definition of timers

This thread is locked; no one can reply to it. rss feed Print
Multipule definition of timers
sicgamemaker
Member #1,365
June 2001
avatar

i have 2 .cpp (engine.cpp, main.cpp) files in myproject that use these timers and when i include the .h file, i get redefinition errors for the timers

how do i get these timers to (or at least the variables) to be used in both cpp files?

this is the basic layout
----------------
in main.cpp
----------------
#include timers.h
#include engine.h

code

--------------
in engine.cpp
--------------
#include engine.h
code

--------------------
in engine.h
--------------------
#include timers.h
code

--------------
and in timers.h
----------------
#ifndef _TIMERS_
#define _TIMERS_
volatile double SECONDS=0;//seconds - keeps track of time elapsed in game
void SECONDS_TIMER()
{
SECONDS=SECONDS+.001;
}
END_OF_FUNCTION(SECONDS_TIMER);
volatile int FPS_T_V=0;
volatile int LAST_FPS=0;
void FPS_T()//used to calculate frames per second
{
LAST_FPS=FPS_T_V;
FPS_T_V=0;
}
END_OF_FUNCTION(FPS_T);
#endif

_______________________
[Insert signature here]

kdevil
Member #1,075
March 2001
avatar

If engine.h includes timers.h, do you have to do
#include timers.h
in main.cpp?

-----
"I am the Black Mage! I casts the spells that makes the peoples fall down!"

sicgamemaker
Member #1,365
June 2001
avatar

yeah, if i dont, i cant access those variables from main.cpp because im using a multiple file project in MSVC++ 6

_______________________
[Insert signature here]

DanTheKat
Member #1,990
March 2002
avatar

Heh. Allegro.h #includes timer.h.

---
"Even the best spellcaster can't transmute bread into bread." - spellcaster

orz
Member #565
August 2000

Generally, functions shouldn't be in .h files, unless they're inline functions. And callbacks shouldn't be inline functions.

Also, variables declared in .h files should be labeled extern, and have a .c or .cpp also declare a variable of the same name without the extern.

Otherwise, ever .c or .cpp files that includes those .h files will create a variable or function by that name, and all the variables and functions will conflict with each other.

sicgamemaker
Member #1,365
June 2001
avatar

ok, so i shoudl seperate the variable's from the declaration of the timer functions, and stick teh timer functions in a timers.cpp file... and the variables should work like this:

timers.h
extern double SECONDS
extern int FPS

timers.cpp
double seconds=0;
int FPS=0;
~~timer functions defined here~~

_______________________
[Insert signature here]

orz
Member #565
August 2000

Correct.

Another thing: I'm not sure, but I seem to recall floating point numbers not working from inside of timer functions on some platforms. If you don't need portability, maybe it's not an issue.

Go to: