Jump to content
  • Advertisement
Sign in to follow this  

splitting source code into .h/.cpp files

This topic is 4525 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

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


//-------- initializations.h --------//
// no issues here that i know of, but
// i have no idea where the issue is
// to begin with :)
#include "globals.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();


//-------- 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)
    case GLFW_KEY_ESC:
    case 32: // space
//      yspeed = 200.0f;
//      totaltime = yspeed / gravity;
      isjumping = true;
    case 65: // 'A'
    case 97: // 'a'
      ismovingleft = !ismovingleft;
      ismovingright = false;
    case 68: // 'D'
    case 100: // 'd'
      ismovingright = !ismovingright;
      ismovingleft = false;
    case 83: // 'S'
    case 115: // 's'
      ismovingbackwards = !ismovingbackwards;
      ismovingforward = false;
    case 87: // 'W'
    case 119: // 'w'
      ismovingforward = !ismovingforward;
      ismovingbackwards = false;
    case 67: // 'C'
    case 99: // 'c'
      iscrouching = !iscrouching;
        ypos -= .125f;
        ypos += .125;

void GLFWCALL mouseposcallback(int x, int y)
  dx = x - mousebasex;
  dy = y - mousebasey;
  mousebasex = x;
  mousebasey = y;
  lookupdown += dy * 0.15f;
  heading -= dx * 0.15f;
  if(heading < 0.0f)
    heading = heading + 360.0f;
  else if (heading > 360.0f)
    heading = heading - 360.0f;
  yrot = heading;

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 this post

Link to post
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 this post

Link to post
Share on other sites
Guest Anonymous Poster
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 this post

Link to post
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!

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!