Public Group

# devcpp projects

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

## 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 on other sites
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.

##### 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 on other sites
Quote:
 Original post by mike25025Everything 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 on other sites
Also you shouldnt put underscores infront of Identifiers, thats reserved for the implementation.

##### 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 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*   pScrSDL_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 on other sites
Globals are not evil, they are extremely nice.

Here is how you go about using them:

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 headerint 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 on other sites
Quote:
 Original post by DareDeveloperBtw. ... 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?

using bloodshed devcpp 4.9.9.0

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

##### Share on other sites
Quote:
 Original post by darkzeroxthis 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 get7 global.h [Warning] t' initialized and declared extern' and6 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.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• 14
• 14
• 45
• 22
• 27
• ### Forum Statistics

• Total Topics
634044
• Total Posts
3015211
×