Public Group

This topic is 4859 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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; } } }

##### Share on other sites

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

##### Share on other sites
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.

##### Share on other sites
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 , 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.

##### Share on other sites
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.

##### Share on other sites
So everything works when you include allegro in the input_.h file only?

##### Share on other sites
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.

##### Share on other sites
Do I have to use a #ifdef, #define in my .h?

##### Share on other sites
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...

##### Share on other sites
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.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 24
• 15
• 36
• 12
• ### Forum Statistics

• Total Topics
634823
• Total Posts
3019459
×