Sign in to follow this  
darkzerox

devcpp projects

Recommended Posts

i figure this is a newbish question. im having some trouble compiling my devcpp project. i've got 5 cpp files, that each include "main.h" main.h looks like this
#ifndef _MAIN_H
#define _MAIN_H

#include <SDL/SDL.h>
#include <SDL/SDL_image.h>

#include <cmath>
#include <cstdlib>

#include "global.h"
#include "screen.h"
#include "misc.h"
#include "draw.h"
#include "object.h"

#endif
the problem is, i'm getting multiple definition errors. all my variables are defined in global.h...
#ifndef _GLOBAL_H
#define _GLOBAL_H

SDL_Surface        *scr, *map;
Uint8              *key;

float              t = 1, g = 800;

int                mouse_x, mouse_y;
bool               mouse_left, mouse_right;
float              scroll_x, scroll_y;

#endif
i figured this ifndef stuff would prevent multiple definitions.. but.. it clearly isnt. suggestions?

Share this post


Link to post
Share on other sites
You need to organize your code.

*.h files usually just have declarations of functions and variables while *.cpp files carry the definitions.


///////////////////////
// main.cpp

#include "person.h"

int main(){
// Use person.h
Person joe;
joe.name("Joe");
return 0;
}


///////////////////////
// person.h

#if !defined(_PERSON_H_)
#define _PERSON_H_

#include <string>

class Person {
public:
std::string name(); // Some function PROTOTYPES
std::string name(std::string n);
private:
std::string pName;
};

#endif


///////////////////////
// person.cpp

#include "person.h"

// There are better ways to do this, this is just an example...
std::string Person::name(){
return pName;
}

std::string Person::name(std::string n){
return pName = n;
}



Warning: I haven't compiled it so there might be a few typos.

Notice that the declarations are in the .h and the definitions where we define it are in the .cpp .

Share this post


Link to post
Share on other sites
Quote:
Original post by mike25025
Everything in global.h should be in a cpp file. Then put extern before all of the variables in global.h and remove all initializers.


errr... so it should be in a cpp file, *and* in a .h file (with "extern")?

Quote:
Original post by Rob Loach
*.h files usually just have declarations of functions and variables while *.cpp files carry the definitions.


err.. i thought that's what i was doing?

i have all my code in the cpp files, and just the function prototypes in the .h files (with the exception of global.h, which has only global variables)

thanks anyways.

Share this post


Link to post
Share on other sites
mm.. okay, the problem is not with the functions or anything, its just with the global variables. how to i set them up so they are accessible by all the .cpp files??

im not sure how to make this "extern" thing work.

(your code compiles fine Rob, but it doesnt show me how to use global variables)

and yes i know global variables are "evil", but i need them.

Share this post


Link to post
Share on other sites
I think the problem might be in global.h.
SDL.h is simply not known there. I might be wrong though.
Probably that's why you can't define SDL surfaces there.

I'd reorganize it a little, too.
I wouldn't include all the header files in one main header.
Try to avoid that. Each cpp file should only need it's own header file and some required headers.
In that own header file you should only include the headers that are required for the declaration there.

Bad to write that as text ... I'll try to show it as code:


#ifndef GLOBAL_H
#define GLOBAL_H

// global.h

#include <SDL/SDL.h>
#include <SDL/SDL_main.h>

#include <cmath>
#include <cstdlib>

SDL_Surface* pScr
SDL_Surface* pMap;

Uint8* pKeys;

// ...

#endif






#ifndef DRAW_H
#define DRAW_H

// draw.h

// include main header
#include "global.h"

// include needed header
#include "screen.h"

void draw(SDL_Surface* pSurface, CScreen* pScreen);

#endif






// draw.cpp

// include own header
#include "draw.h"

// include other headers
#include "object.h"

void draw( SDL_Surface* pSurface, CScreen* pScreen )
{
CObject* pEvilCat = new CObject();
pScreen->clear_srceen();

// ... whatever
}

#endif





Have not compiled that either (typos guaranteed) ... might even be a worse solution.
But I think it is better to avoid including too many headers globally.

Btw. ... how does your key array work?

Share this post


Link to post
Share on other sites
Globals are not evil, they are extremely nice.

Here is how you go about using them:

The header:

extern int something;
extern float something_else;
extern char a_string[64];



The cpp:

#include “header_file_name.h"

// must put them here for them to actually get allocated memory, they will be recognized by all that include the header
int something = 0;
float something_else = 1;
char a_string[64] = “this is a string…………….\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"; //please for the love of sanity don’t initialize your strings this way…. But hey its just a demo





Share this post


Link to post
Share on other sites
Quote:
Original post by DareDeveloper
Btw. ... how does your key array work?


void getKeys()
{
key = SDL_GetKeyState(NULL);
}







then to check if a key is being pressed, just do

if( key[SDLK_LEFT] ) x--;







im going to try extern like you said cyberfox.

this whole multiple cpp file thing seems like a hell of a lot more work than its worth. ive always just left all the code in the headers, and only had one cpp...

----

doesnt work.

now i get

7 global.h [Warning] `t' initialized and declared `extern' 


and

6 global.cpp redefinition of  `float t' 


errors...

would anyone want to take a crack at it if i just post the project for you?

download here

using bloodshed devcpp 4.9.9.0

[Edited by - darkzerox on May 28, 2005 6:59:53 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by darkzerox

this whole multiple cpp file thing seems like a hell of a lot more work than its worth. ive always just left all the code in the headers, and only had one cpp...

It's easier once you get the hang of it. When you get several thousand lines of code, you don't want to have to search through one massive file for something.

Quote:
now i get

7 global.h [Warning] `t' initialized and declared `extern' 


and

6 global.cpp redefinition of  `float t' 


errors...

You don't want to define t or g in the .h file, just declare them like you do the other externs. The extern keyword basically tells a .cpp file "trust me that this variable is defined in another file," but when global.h is included in every .cpp file, t and g get defined in every file. Init their values in main.cpp.

Share this post


Link to post
Share on other sites
ahh.. okay. i see how it works now... i got it to work.

i hope this is worth it. i put extern <type> <name> in the .h files where it whines about a missing variable, and ive only included global.h from main.cpp.

thanks for all your help.

[Edited by - darkzerox on May 30, 2005 2:54:53 PM]

Share this post


Link to post
Share on other sites

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