Text Editor and CLI to an IDE

I've been flopping around with the good old text editor and terminal for a while now. Toying with SDL2 and all. Now I want to make a proper game out of it and so I'm starting completely over. I've tried a couple different IDEs include Eclipse CDT (C Dev Tools) and Code::Blocks.

I've got three files.

2dgame.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <SDL2/SDL.h>

#include "GraphicsEngine/GraphicsEngine.cpp"

SDL_Renderer* testren;

using namespace std;

int main(){
    cout << "whee" << endl;
    return 0;
}


GraphicsEngine.cpp
 
#include ""GraphicsEngine.h" 


GraphicsEngine.h
1
2
3
4
5
6
class GraphicsEngine{
private:
    SDL_Renderer* renderer;
public:
    GraphicsEngine();
};


It tells me SDL_Renderer doesn't name a type in GraphicsEngine.h .
It seems like it is trying to build from GraphicsEngine.cpp instead of 2dgame.cpp .
It compiles fine with "g++ 2dgame.cpp -o 2dgame".
The SDL mailing list told me it wasn't an SDL specific issue which seems to be the case. I tried the same thing with string. Here's a clips from the reply, "In general, it's good practice to not include source (.cpp) files from
other source files. Only ever include header (.h) files. Each .cpp
file must be able to be compiled individually."

Eclipse automagically set up the class to be like that. So does Code::Blocks. So I tried changing the source extension to hpp instead. It compiles fine. With a couple of caveats. First is it generates an #include protection in the source file which it only did in the header before. Second, in Eclipse, if I right click an unimplemented method and select "Source > Implement Method" it does it inline in the header instead of in the source file like before. I'm sure there's a plethora of things I've yet to discover.

I've tried other extensions, too. cxx and cc both do the same errors as cpp. Mysteriously, random extensions like cx, ccc, and cplusplus open up GraphicsEngine.h in a text editor and does nothing with the source file. Then if I switch to a regular extension like cpp it has the class definition twice in the header.

It's frustrating to me that the default behavior is broken. I came to try IDEs to make my life easier and more organized.
Never include source files in your code, and stick to using the .cpp extension for source files. Headers (.h, .hpp) are fine for inclusion.

You never #include <SDL2/SDL.h> in your header, yet have an SDL pointer in your class. That may be why the compiler is whining.

-Albatross
I'm thoroughly confused here. I thought it was good practice to have the class declaration in the header and a separate file that implements all the methods.

When I right click on the folder I want all my graphics code in, I click New > Class. It pops up a dialog. I type in the name of my class and it automagically makes and fills in two files: MyClass.cpp and MyClass.h . MyClass.h contains an include guard and the class declaration. MyClass.cpp includes its header and has stubs for the constructor/destructor if I didn't uncheck the boxes. I thought it was odd that the source included the header, but I just rolled with it. Previously I had the header include the source so I could include the header.

Right, so I swapped the include into the header instead and tried compiling it. No dice. When it has the constructor and destructor stubs in the source it says the class name does not name a type. When the header includes SDL and the source is completely empty, it compiles fine. Not so useful, though.

Back to how it generated, with GraphicsEngine.cpp including GraphicsEngine.h . I added the include for SDL in the source above where it includes the header. It compiles and runs in this state except for one thing. Previously I was deleting the implementation stubs in the source because I wasn't doing anything with them. I left them in this time but now it's complaining that the constructor and destructor are defined already.

How does the modified code look now. Could you post the latest versions please.
> I thought it was good practice to have the class declaration in the header
> and a separate file that implements all the methods.
yes

> I thought it was odd that the source included the header
No, that's the idea. Headers are supposed to hold function declarations and class definitions.
If you want to use something it must have to be declared first. Suppose that you want to call the `foo()' function, then you need to see the declaration of that function, so you include the correspondent header
There is no need to see the body of that function, that would be resolved later.
http://www.cplusplus.com/forum/general/113904/#msg622040
http://www.cplusplus.com/forum/general/113904/#msg622055

Also, you should make your headers self-contained. If a source needs to use a header it should simply include it, no need to include other things prior.
Also, make sure to use header guards, so there is no problem with multiple inclusion or order.
http://www.cplusplus.com/forum/articles/10627/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//2dgame.cpp
#include <iostream>
#include <SDL2/SDL.h>

#include "GraphicsEngine/GraphicsEngine.h"

SDL_Renderer* testren;

using namespace std;

int main(){
    cout << "whee" << endl;
    return 0;
}

1
2
3
4
5
6
7
8
9
//GraphicsEngine.h
#pragma once //header guard
#include <SDL2/SDL.h> //a forward declare may be enough
class GraphicsEngine{
private:
    SDL_Renderer* renderer;
public:
    GraphicsEngine();
};

1
2
3
4
5
6
//GraphicsEngine.cpp
#include "GraphicsEngine.h"
//implement the methods
GraphicsEngine()::GraphicsEngine(){

}
Okay, after letting all that stew in my mind during work today, I figured it out. I'm used to a handwritten Makefile kind of like this:
1
2
3
4
compile:
    g++ 2dgame.cpp -o 2dgame
debug:
    g++ -g 2dgame.cpp -o 2dgame -DGOGOGDB


Whereas an IDE generates an indecipherable mess. Well, that's what it looks like to me.

Anyways, moral of the story: With a simple call to g++, everything has to be hierarchically included. My code would include SDL and MyClass.h. MyClass.h would include its source. Etcetera. An IDE will compile each cpp file as its own whatever, so each source file needs to include absolutely everything it needs, including its header.

So, 2dgame.cpp includes SDL and GraphicsEngine.h . GraphicsEngine.cpp includes SDL and GraphicsEngine.h . During the linking stage or something, 2dgame.cpp will make a call to a function declared in GraphicsEngine.h but is implemented in GraphicsEngine.cpp . It knows to link the GraphicsEngine.o (or whatever) even though nothing 2dgame.cpp or what it included specifically includes GraphicsEngine.cpp . It's automagical.

Which is basically what you were trying to tell me. Thanks!
I'm not sure if you get it.
Stop including source files because you'll encounter `multiple definitions' errors. It does not matter if you use an IDE or not.

$ g++ 2dgame.cpp GraphicEngine/GraphicsEngine.cpp -o program.bin
there, a simple call to g++ that compile and links all the sources.
Last edited on
Topic archived. No new replies allowed.