#include <windows.h>
#include <gl/Gl.h>
#include<gl/glut.h>
#include <gl\glaux.h>
#include <iostream.h>
#include <math.h>
#include "Model.h"
#include "light.h"
#include "camera.h"
#include "Matrix.h"
#include "Frustum.h"
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "glu32.lib")
#pragma comment(lib, "glaux.lib")
#define PI 3.141592654
#ifndef _MAIN_
#define _MAIN_
sVertex at;// defines these each implementation file.
int fr=0;
int lfr=0;
int time2=0;
inline bool VectZero(sVertex vect)
{
if(vect.x==0&&vect.y==0&&vect.z==0)
return true;
else return false;
}
// some other functions
//
#endif
Need help with linker error
I am getting linker errors because I am putting "main.h" in every implementation file and I define the same variables all of the time. Here is my code:
I thought #ifndef, #define, and #endif would solve the problem, but apparently it doesn't.
Inclusion guards don't protect against multiple definiton errors at link time. Global variables declared in header files should be declared with a storage class declaration, usually extern. extern'd variables then need to be defined in a single source file, much like static class variables.
See this article for more details.
See this article for more details.
#include <windows.h>#include <gl/Gl.h> #include<gl/glut.h>#include <gl\glaux.h>#include <iostream.h>#include <math.h>#include "Model.h"#include "light.h"#include "camera.h"#include "Matrix.h"#include "Frustum.h"#define PI 3.141592654#ifdef _MAIN_#pragma comment(lib, "opengl32.lib")#pragma comment(lib, "glu32.lib")#pragma comment(lib, "glaux.lib") sVertex at;// defines these each implementation file. int fr=0; int lfr=0; int time2=0;inline bool VectZero(sVertex vect){ if(vect.x==0&&vect.y==0&&vect.z==0) return true; else return false;}// some other functions//#else extern sVertex at; extern int fr; extern int lfr; extern int time2;inline bool VectZero(sVertex vect);// some other functions prototypes#endif
- defenitions dont carry across .cpp files, hence
#ifndef _MAIN_
#define _MAIN_
defines _MAIN_ in every cpp file you use this header with
- linking using pragma only needs to be done once in each project
- to use the above code, define _MAIN_ in only one of your .cpp files (ideally main.cpp)
hope that helps
EDIT: sorted out the source tags
I've tried extern, but it doesn't help. Static will work, but it doesn't quite work as it is supposed to(at is always <0,0,0>). However, if I change the code back right after I compiled with a static, it will compile and work for a while (although it will still display the errors), but it eventually stop running again.
ah the niceities of c++.. get on and learn assembler, its so much simpler.
well im stuck on this point then, sry
well im stuck on this point then, sry
Eh, also the second and third line in your source for including gl and glut ... you have the '/' turned the wrong way... heh
Quote:Original post by SiCrane
Inclusion guards don't protect against multiple definiton errors at link time. Global variables declared in header files should be declared with a storage class declaration, usually extern. extern'd variables then need to be defined in a single source file, much like static class variables.
See this article for more details.
OK, I read that article a little more closely and solved the problem. Thanks for the help.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement