header files

Started by
8 comments, last by dpadam450 18 years, 4 months ago
Usually never used them because of small programs, but im working up an RTS. I keep gettin an error 'undefined reference to terrain_x ....etc' Here is my main.c++; #include "input_.h" int terrain_x,terrain_y, current_tile_x,current_tile_y; //while loop then calls input() And here is my "input_.h" extern int terrain_x, ....etc void input() { if(key[KEY_A]) { terrain_x+=20; current_tile_x--; //STOP FROM SCROLLING PAST MAP if(terrain_x > 0) { terrain_x = 0; current_tile_x = 0; } } if(key[KEY_D]) { terrain_x-=20; current_tile_x++; if(terrain_x < -640) { terrain_x = -640; current_tile_x = 32; } } if(key[KEY_W]) { terrain_y += 20; current_tile_y--; if(terrain_y > -60) { terrain_y = -60; current_tile_y = 0; } } if(key[KEY_S]) { terrain_y -=20; current_tile_y++; if(terrain_y < - 620) { terrain_y = -620; current_tile_y = 28; } } }

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

Advertisement
Did you add input_.cpp to your project?

Note that extern keyword basically tells the linker that the variable is declared somewhere else... which is not the case as far as I can see from your code.

Im not sure realy, if you post the complete code its easier to find the problem
I did post one c++ and one .h. in main.c++ I declared int terrain_x, terrain_y....

then in my .h file i used extern. They are in the same project. It says something about a linker error. So I went to project and had it link the .h with my project (which is weird, i thought include took care of that), anyway I got rid of the errors, but now it wont read my allegro functions in the .h file. I used
#include <allegro.h> above my externs..........help...... :)



Update: so if I use the above #include <allegro.h>, get the "undefined reference" error, but when I take the include away, the compiler only complains about allegro functions.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

A typical use of the extern keyword could be the errno variable.

In errno.h you see something like:
extern int errno;...


In errno.cpp you see something like
int errno;...


The main.cpp would only need
#include <errno.h>...

to use it.

If you have a input_.cpp file that goes along with the input_.h file, this must be added to your project.

Quote:
so if I use the above #include <allegro.h>, get the "undefined reference" error, but when I take the include away, the compiler only complains about allegro functions.


You want to #include the allegro.h file, so that you get the "undefined reference" linker errors, then you need to add the allegro lib folder to your project, and maybe you need to add some .lib files aswell.
How to do this depends on what IDE and environment you are working on.
Well I have a devC++ allegro project, a main.c++ file and a input_.h file.

I #include <allegro.> in main.c++ but I also use allegro functions is my .h.

Everthing seems fine except when I inlude allegro.h in both files.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

So everything works when you include allegro in the input_.h file only?
ahhhh its hard to explain it. Ok when i dont include allegro in the input_.h file, then the compiler complains about the allegro funcions i use. But when i add #include <allegro.h> at the top of input_.h, the compiler complains "undefined reference" and not the allegro functions.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

Do I have to use a #ifdef, #define in my .h?

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

When you get the "undefined reference" errors you are actually on the right path.
At that point the compiler is satisfied, but the linker can not find the allegro lib files.

PS. Im not sure Allegro is compatible with the Dev-C++ w/mingw.
Maybe someone else know this...
Yes allegro is compatible devc++ it comes with its own package specific to devc++.

and yes it does say it is a linker error, but the program runs fine the way i had it. But when i took it and split it into 2 files. it not working. i dont know im going to allegro.cc forums to see if they know anything better.


Ok, i got it to work. I define my variables in int main() so the .h couldnt find them, but its weird ......the only reason i put my definitions there was because two of the variables only work when i declare them inside of int main(). thanks everyone.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

This topic is closed to new replies.

Advertisement