linking files

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

mary
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 33
Joined: Tue Apr 27, 2010 2:13 pm

linking files

Post 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"
User avatar
Bakkon
Chaos Rift Junior
Chaos Rift Junior
Posts: 384
Joined: Wed May 20, 2009 2:38 pm
Programming Language of Choice: C++
Location: Indiana

Re: linking files

Post 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.
X Abstract X
Chaos Rift Regular
Chaos Rift Regular
Posts: 173
Joined: Thu Feb 11, 2010 9:46 pm

Re: linking files

Post 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.
mary
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 33
Joined: Tue Apr 27, 2010 2:13 pm

Re: linking files

Post 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
X Abstract X
Chaos Rift Regular
Chaos Rift Regular
Posts: 173
Joined: Thu Feb 11, 2010 9:46 pm

Re: linking files

Post by X Abstract X »

Are you able to post your full code? For future reference those are called "include guards".
mary
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 33
Joined: Tue Apr 27, 2010 2:13 pm

Re: linking files

Post 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.
X Abstract X
Chaos Rift Regular
Chaos Rift Regular
Posts: 173
Joined: Thu Feb 11, 2010 9:46 pm

Re: linking files

Post by X Abstract X »

Can you atleast show us a single header file so we can see how you are splitting things up?
Last edited by X Abstract X on Mon May 31, 2010 4:08 pm, edited 1 time in total.
mary
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 33
Joined: Tue Apr 27, 2010 2:13 pm

Re: linking files

Post 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
mary
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 33
Joined: Tue Apr 27, 2010 2:13 pm

Re: linking files

Post by mary »

if I have main.cpp linking to class1.h, wouldn't I need class1.h to link to class1.cpp to work?
X Abstract X
Chaos Rift Regular
Chaos Rift Regular
Posts: 173
Joined: Thu Feb 11, 2010 9:46 pm

Re: linking files

Post 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
mary
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 33
Joined: Tue Apr 27, 2010 2:13 pm

Re: linking files

Post 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.
X Abstract X
Chaos Rift Regular
Chaos Rift Regular
Posts: 173
Joined: Thu Feb 11, 2010 9:46 pm

Re: linking files

Post 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.
mary
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 33
Joined: Tue Apr 27, 2010 2:13 pm

Re: linking files

Post 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
User avatar
xiphirx
Chaos Rift Junior
Chaos Rift Junior
Posts: 324
Joined: Mon Mar 22, 2010 3:15 pm
Current Project: ******** (Unkown for the time being)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

Re: linking files

Post 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
StarCraft II Zerg Strategy, open to all levels of players!

Looking for paid work :< Contact me if you are interested in creating a website, need a web design, or anything else you think I'm capable of :)
User avatar
RyanPridgeon
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 447
Joined: Sun Sep 21, 2008 1:34 pm
Current Project: "Triangle"
Favorite Gaming Platforms: PC
Programming Language of Choice: C/C++
Location: UK
Contact:

Re: linking files

Post by RyanPridgeon »

So what exactly are the errors and what is your code?
Ryan Pridgeon
C, C++, C#, Java, ActionScript 3, HaXe, PHP, VB.Net, Pascal
Music | Blog
Post Reply