Page 1 of 2

linking files

Posted: Mon May 31, 2010 3:27 pm
by mary
I'm using codeblocks and am having tons of linking errors, I was wondering if I am linking files incorrectly

Code: Select all

main
#include "class.cpp"
#include "class2.cpp"

class.cpp
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include "class2.cpp"
#include "class.h"

class2.cpp
#include "class2.h"

Re: linking files

Posted: Mon May 31, 2010 3:32 pm
by Bakkon
First, including isn't the same as linking. Read this depending on your OS and IDE: http://www.lazyfoo.net/SDL_tutorials/lesson01/index.php
Second, I've never seen a .cpp included. Just include headers because those contain your function prototypes.

Re: linking files

Posted: Mon May 31, 2010 3:37 pm
by X Abstract X
In general you shouldn't be including .cpp files. You want to put the function prototypes in a header file and define them in their respective .cpp file. Then if you need to use those functions, you just include the header that has their prototypes.

I think the only time you might have to include a .cpp is if your using templates inside it.

Re: linking files

Posted: Mon May 31, 2010 3:41 pm
by mary
I changed all the includes to .h, but the compiler is complaining about redefinitions, each .h has its own
#ifndef CLASS_H
#define CLASS_H
#endif

Re: linking files

Posted: Mon May 31, 2010 3:43 pm
by X Abstract X
Are you able to post your full code? For future reference those are called "include guards".

Re: linking files

Posted: Mon May 31, 2010 3:47 pm
by mary
I would, but the files are incredibly huge, and they compile under gcc, so I would assume the problem is codeblocks specific. I have the file as a GUI application, and the linker settings are -lmingw32 -lSDLmain -lSDL -lSDL_image.

Re: linking files

Posted: Mon May 31, 2010 3:48 pm
by X Abstract X
Can you atleast show us a single header file so we can see how you are splitting things up?

Re: linking files

Posted: Mon May 31, 2010 3:55 pm
by mary
here is a header file, all the header files pretty much follow this same format

Code: Select all

#ifndef GLOBALS_H
#define GLOBALS_H
#include <SDL/SDL.h>

extern SDL_Surface *buffer;
extern SDL_Surface *title;
extern SDL_Event event;
extern bool done;
extern bool turn;
extern bool bkey;
extern bool ckey;
extern bool lkey;
extern bool tkey;
extern const int SCREENWIDTH;
extern const int SCREENHEIGHT;
extern char gamestate;
extern char mouse_key[5];
extern int mouse_x;
extern int mouse_y;
extern int loopStop;

#endif

Re: linking files

Posted: Mon May 31, 2010 4:39 pm
by mary
if I have main.cpp linking to class1.h, wouldn't I need class1.h to link to class1.cpp to work?

Re: linking files

Posted: Mon May 31, 2010 4:42 pm
by X Abstract X
Linking isn't including. Following what you described though it should end up like:

class1.h includes nothing
class1.cpp includes class1.h
main.cpp includes class1.h

Re: linking files

Posted: Mon May 31, 2010 4:45 pm
by mary
in main i am including the .cpp's and in the .cpp's I am including the .h. I don't know why this works in gcc, but if I switch it so main includes .h's it doesn't work.

Re: linking files

Posted: Mon May 31, 2010 4:48 pm
by X Abstract X
Well I have to admit I haven't used extern before so I'm not familiar with how it works, perhaps it's the problem. You should really be using classes and put your globals in a namespace.

Re: linking files

Posted: Mon May 31, 2010 4:49 pm
by mary
I had this project working in codeblocks before then I added a new header and a new .cpp, and all the errors are with including them. I did have to switch the files in main to .h

Re: linking files

Posted: Mon May 31, 2010 11:46 pm
by xiphirx
mary wrote:if I have main.cpp linking to class1.h, wouldn't I need class1.h to link to class1.cpp to work?
What?

class1.h is its own standalone file, it prototypes whatever you want
class1.cpp is the implementation of class1's prototype. It MUST include class1.h
main.cpp this just includes class1.h

Re: linking files

Posted: Tue Jun 01, 2010 6:37 am
by RyanPridgeon
So what exactly are the errors and what is your code?