Sign in to follow this  

Need help with linker error

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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:

#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

I thought #ifndef, #define, and #endif would solve the problem, but apparently it doesn't.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

#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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Eh, also the second and third line in your source for including gl and glut ... you have the '/' turned the wrong way... heh

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this