# splitting source code into .h/.cpp files

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

## Recommended Posts

well, i started out work on a renderer for a project some friends and i are working on and i made the mistake of not splitting up the source code as i went into appropriate .h/.cpp files. well, now i'm paying the price. for some reason i'm faced with mysterious "multiple definition of <variable name>" errors for every variable in one of my files ("global.h" to specific). and i have no idea what i'm doing wrong. i am using #ifndef, #define, and #endif for all my .h files to make sure there isn't any repeat definitions, but that isn't working. i had sporadic trouble with this in other projects, and it might be an issue with dev-cpp, but i doubt it. anyways, if anyone can see the issue in the following code and would be kind enough to let me know what it is, i'd be more than thankful!
//-------- main.cpp --------//
// the troublesome variables
// are not directly used here
// but it has the #include, so
// i figured i'd put it in :)
#include "globals.h"

int main()
{
...
}

//-------- globals.h --------//
// this is the file that holds
// the variables that have the
// mysterious "multiple
// definition" error, but i'm
// not sure it occurs in this
// file
#ifndef GLOBALS_H
#define GLOBALS_H

#include <GL/glfw.h>
#include <IL/ilut.h>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#include <io.h>
using namespace std;
#include "initializations.h"

int WINDOW_WIDTH = 800;
int WINDOW_HEIGHT = 600;

// the variables below here
// are the ones messing up

float ypos = 15.0f;
GLfloat	yrot;
GLfloat lookupdown = 0.0f;

int mousebasex = WINDOW_WIDTH/2;
int mousebasey = WINDOW_HEIGHT/2;
int dx, dy, dmousewheel;
int mousewheelbase;
bool iscrouching = false;
bool ismovingright = false;
bool ismovingleft = false;
bool ismovingforward = false;
bool ismovingbackwards = false;
bool isjumping = false;

#endif

//-------- initializations.h --------//
// no issues here that i know of, but
// i have no idea where the issue is
// to begin with :)
#include "globals.h"

#ifndef INITIALIZATIONS_H
#define INITIALIZATIONS_H

void initializecallbacks();
void GLFWCALL keyboardcallback(int key, int action);
void GLFWCALL mouseposcallback(int x, int y);
void GLFWCALL mousebuttoncallback(int button, int action);
void GLFWCALL mousewheelcallback(int pos);
void GLFWCALL resize(int width, int height);
void initializevisuals();

#endif

//-------- initializations.cpp --------//
// everything that doesn't use the
// troublesome variables has been cut out

#include "initializations.h"

void initializecallbacks()
{
...
}

void GLFWCALL keyboardcallback(int key, int action)
{
switch(key)
{
case GLFW_KEY_ESC:
glfwTerminate();
exit(0);
break;

case 32: // space
//      yspeed = 200.0f;
//      totaltime = yspeed / gravity;
isjumping = true;
break;

case 65: // 'A'
case 97: // 'a'
ismovingleft = !ismovingleft;
ismovingright = false;
break;

case 68: // 'D'
case 100: // 'd'
ismovingright = !ismovingright;
ismovingleft = false;
break;

case 83: // 'S'
case 115: // 's'
ismovingbackwards = !ismovingbackwards;
ismovingforward = false;
break;

case 87: // 'W'
case 119: // 'w'
ismovingforward = !ismovingforward;
ismovingbackwards = false;
break;

case 67: // 'C'
case 99: // 'c'
iscrouching = !iscrouching;
if(iscrouching)
ypos -= .125f;
else
ypos += .125;
break;
}
}

void GLFWCALL mouseposcallback(int x, int y)
{
dx = x - mousebasex;
dy = y - mousebasey;

mousebasex = x;
mousebasey = y;

lookupdown += dy * 0.15f;
}

void GLFWCALL mousebuttoncallback(int button, int action)
{
}

void GLFWCALL mousewheelcallback(int pos)
{
dmousewheel = pos - mousewheelbase;
mousewheelbase = pos;
ypos += dmousewheel * 15;
}

void GLFWCALL resize(int width, int height)
{
...
}

void initializevisuals()
{
...
}


again, thank you for any help you can provide (i did search the forums, but unfortunately i only could find the #ifndef, #define, and #endif solution)!

##### Share on other sites
Anything you put in a .h file needs to be declared extern, unless it's a function declaration, or if it's a const.

Also, all your globals will need exactly one definition in a .c/.cpp file.

To use your globals.h as an example, the easy fix would be to rename it to globals.cpp, and then create a new globals.h file that has all those variables declared to be extern, and then remove all the definitions (the = [value] stuff)

Hope this helps.

##### Share on other sites
never declare variables in header files, unless they are const or extern...

const says "this variable is not actually variable, its constant, like a define" so its ok.

extern says "this variable exists... somewhere, but its not here", so its ok too coz you're not actually creating a variable (Make sure you never use = in the same line as extern tho, coz that says "put this value into a box which is somewhere i dont know")

If you extern a variable in a header, u have to put it into ONE c file, and this is where you put the actual initial value.

##### Share on other sites
ah, okay. man, i knew it would be simple but i really didn't know what kind of simple. :)

strangely, when i searched through gd.net i don't think i came upon that article (probably used the wrong key words), but is a very good one.

again, thank you for your help!

1. 1
2. 2
3. 3
4. 4
frob
13
5. 5

• 16
• 13
• 20
• 12
• 19
• ### Forum Statistics

• Total Topics
632171
• Total Posts
3004553

×