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.
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.
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&, unsignedint, 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&, unsignedint)'|
obj\Debug\main.o:C:\Users\chris\Documents\codeblocks\glutsnowmen\glfont.cc|171|first defined here|
||=== Build finished: 28 errors, 2 warnings ===|
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:
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.