trouble including header and cpp files

Hi all,

At the moment I'm trying to include a header file and cpp source file (glfont if you wanted to know) and i'm finding it difficult to work out how i'm supposed to correctly do it. It's a standard set up from what I've seen with:

glfont.h containing the class declaration and then glfont.cc containing the function definitions. glfont.h is included in glfont.cc if that means anything.

So far I have tried:

1) including the glfont.cc in my main. Generates tons of multiple definitions and I can't find them.

and 2) including glfont.h in my main. Objects can't be created and functions can't be called.


So, how am I supposed to do this.

Thanks, Chris
and 2) including glfont.h in my main. Objects can't be created and functions can't be called.

What is that supposed to mean?
Does this answer your question?

glfont.cc:
1
2
3
4
5
6
7
#include <iostream>
#include "glfont.h"

void GLFont::doit() {
	std::cout << "I'm Here!" << std::endl;
	return;
}

glfont.h
1
2
3
4
5
6
7
8
9
#ifndef GLFONT_H_
#define GLFONT_H_

class GLFont {
public:
	void doit();
};

#endif 

main.cc
1
2
3
4
5
6
#include "glfont.h"

int main(int argc, char* argv[]) {
	GLFont a;
	a.doit();
}


If you're using g++ to compile, you can type:

g++ -o test glfont.cc main.cc

That will make a program called "test" that runs your program. The command may be different based on your compiler and operating system.
Last edited on
Thanks for the reply.

Not a great deal really. It doesn't really make sense as a solution. What I really want to know is:

How do you usually go about including a file.h (class declaration) and file.cpp (function definitions) into a project?

I thought number one would work but apparently not.

Chris
How do you usually go about including a file.h (class declaration) and file.cpp (function definitions) into a project?

Header files contain declarations and are included by source files. The source files contain the definitions and those are the ones you have to compile and link.
Hi JMJAtlanta. I believe that is exactly how I have it set up but I get that GLFONT was not declared in the scope, any ideas?

Also i'm using codeblocks and mingw.

Chris
Last edited on
Is that not the opposite to what JMJAtlanta said Athar????

When I try it your way round I get tons of multiple declaration errors:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
C:\Users\chris\Documents\codeblocks\glutsnowmen\rendering.h|61|warning: missing braces around initializer for 'unsigned int [4][40]'|
C:\Users\chris\Documents\codeblocks\glutsnowmen\rendering.h|61|warning: missing braces around initializer for 'unsigned int [40]'|
obj\Debug\glfont.o||In function `GLFontBase':|
C:\Users\chris\Documents\codeblocks\glutsnowmen\glfont.cc|16|multiple definition of `GLFontBase::GLFontBase()'|
obj\Debug\main.o:C:\Users\chris\Documents\codeblocks\glutsnowmen\glfont.cc|16|first defined here|
obj\Debug\glfont.o||In function `GLFontBase':|
C:\Users\chris\Documents\codeblocks\glutsnowmen\glfont.cc|16|multiple definition of `GLFontBase::GLFontBase()'|
obj\Debug\main.o:C:\Users\chris\Documents\codeblocks\glutsnowmen\glfont.cc|16|first defined here|
obj\Debug\glfont.o:C:\Users\chris\Documents\codeblocks\glutsnowmen\glfont.cc|21|multiple definition of `GLFontBase::CreateImpl(std::string const&, unsigned int, bool)'|
obj\Debug\main.o:C:\Users\chris\Documents\codeblocks\glutsnowmen\glfont.cc|21|first defined here|
obj\Debug\glfont.o:C:\Users\chris\Documents\codeblocks\glutsnowmen\glfont.cc|85|multiple definition of `GLFontBase::FreeResources()'|
obj\Debug\main.o:C:\Users\chris\Documents\codeblocks\glutsnowmen\glfont.cc|85|first defined here|
obj\Debug\glfont.o:C:\Users\chris\Documents\codeblocks\glutsnowmen\glfont.cc|92|multiple definition of `GLFontBase::Begin()'|
obj\Debug\main.o:C:\Users\chris\Documents\codeblocks\glutsnowmen\glfont.cc|92|first defined here|
obj\Debug\glfont.o||In function `~GLFontBase':|
C:\Users\chris\Documents\codeblocks\glutsnowmen\glfont.cc|101|multiple definition of `GLFontBase::~GLFontBase()'|
obj\Debug\main.o:C:\Users\chris\Documents\codeblocks\glutsnowmen\glfont.cc|101|first defined here|
obj\Debug\glfont.o||In function `~GLFontBase':|
C:\Users\chris\Documents\codeblocks\glutsnowmen\glfont.cc|101|multiple definition of `GLFontBase::~GLFontBase()'|
obj\Debug\main.o:C:\Users\chris\Documents\codeblocks\glutsnowmen\glfont.cc|101|first defined here|
obj\Debug\glfont.o||In function `~GLFontBase':|
C:\Users\chris\Documents\codeblocks\glutsnowmen\glfont.cc|101|multiple definition of `GLFontBase::~GLFontBase()'|
obj\Debug\main.o:C:\Users\chris\Documents\codeblocks\glutsnowmen\glfont.cc|101|first defined here|
obj\Debug\glfont.o||In function `PixelPerfectGLFont':|
C:\Users\chris\Documents\codeblocks\glutsnowmen\glfont.cc|110|multiple definition of `PixelPerfectGLFont::PixelPerfectGLFont()'|
obj\Debug\main.o:C:\Users\chris\Documents\codeblocks\glutsnowmen\glfont.cc|110|first defined here|
obj\Debug\glfont.o||In function `PixelPerfectGLFont':|
C:\Users\chris\Documents\codeblocks\glutsnowmen\glfont.cc|110|multiple definition of `PixelPerfectGLFont::PixelPerfectGLFont()'|
obj\Debug\main.o:C:\Users\chris\Documents\codeblocks\glutsnowmen\glfont.cc|110|first defined here|
obj\Debug\glfont.o:C:\Users\chris\Documents\codeblocks\glutsnowmen\glfont.cc|115|multiple definition of `PixelPerfectGLFont::Create(std::string const&, unsigned int)'|
obj\Debug\main.o:C:\Users\chris\Documents\codeblocks\glutsnowmen\glfont.cc|115|first defined here|
obj\Debug\glfont.o||In function `GLFont':|
C:\Users\chris\Documents\codeblocks\glutsnowmen\glfont.cc|166|multiple definition of `GLFont::GLFont()'|
obj\Debug\main.o:C:\Users\chris\Documents\codeblocks\glutsnowmen\glfont.cc|166|first defined here|
obj\Debug\glfont.o||In function `GLFont':|
C:\Users\chris\Documents\codeblocks\glutsnowmen\glfont.cc|166|multiple definition of `GLFont::GLFont()'|
obj\Debug\main.o:C:\Users\chris\Documents\codeblocks\glutsnowmen\glfont.cc|166|first defined here|
obj\Debug\glfont.o:C:\Users\chris\Documents\codeblocks\glutsnowmen\glfont.cc|171|multiple definition of `GLFont::Create(std::string const&, unsigned int)'|
obj\Debug\main.o:C:\Users\chris\Documents\codeblocks\glutsnowmen\glfont.cc|171|first defined here|
||=== Build finished: 28 errors, 2 warnings ===| 



Cheers,

Chris
Last edited on
Is that not the opposite to what JMJAtlanta said Athar????

No, you just read something wrong. You are supposed to include the header, but you included the source file.
Last edited on
Oh ok standardly just me being an idiot. Cheers guys I've sorted it now.
Hrmmmm... that works for me Chris. You may wish to double-check your spelling and remember things in C++ are case sensitive.

Athar was correct in what he said. Perhaps it would be easier to think of this in two steps. The compile step, then the link step:

Compile:

main.cc needs to know what everything is (not how it is implemented) in order to compile. Therefore, main.cc needs to #include "glfont.h". The compiler "tacks on" all the stuff in glfont.h before compiling main.cc. So the compiler knows that your call of a.doit is correct. The resultant object file can be confidently created by the compiler.

Link:

In order to link, all the necessary definitions (not just declarations) are required. So the linker needs not only the compiled "main.cc" but also the compiled "glfont.cc". If it finds it has everything it needs, it will put both files together and make your program.

I don't use mingw, but here's how you could see both steps in g++:

Compile the source files into binary object files:

g++ -c -o main.o main.cc
g++ -c -o glfont.o glfont.cc

Link the object files into a binary executable:

g++ -o test main.o glfont.o

Am I making things any clearer, or only digging a hole?

Just saw your last post. Glad to see its sorted.
Last edited on
All sorted now JMJAtlanta, cheers. It was the case sensitive thing. The documentation I was reading had GLFONT in all caps like that and so I was writing that. SHould have checked.

My next question was going to be how exactly does the compiler get to the function definitions if only the header file is included but you went on to answer that, very helpful.

Thanks
Chris
Topic archived. No new replies allowed.