Right now I'm basically having two of the exact same files except for having the #define HEADER_H type of stuff on the header file. |
Well no wonder you're getting errors.
Header files are for the "interface". Things like typedefs, constants, classes, structs, and function prototypes. This way any .cpp file can access any of those things by #including the file which declares them.
Source files are for the "implementation". Which is basically like function bodies. This way if you modify the code for X function, only one .cpp file needs to be recompiled, rather than every file which #includes the header.
Here's an example:
1 2 3 4 5 6 7 8 9 10 11
|
// myclass.h
#ifndef __MYCLASS_H__
#define __MYCLASS_H__
class MyClass
{
public:
void PrintSomething();
};
#endif // __MYCLASS_H__
|
1 2 3 4 5 6 7 8
|
// myclass.cpp
#include <iostream>
#include "myclass.h"
void MyClass::PrintSomething()
{
std::cout << "MyClass::PrintSomething was called\n";
}
|
1 2 3 4 5 6 7 8 9
|
// main.cpp
#include "myclass.h"
int main()
{
MyClass obj;
obj.PrintSomething();
return 0;
}
|
Am I crazy or should someone write a compiler that does this stuff automatically with simply one version of the code anywhere? |
I don't know how what you're doing is working -- but it's no wonder you're getting weird segmentation faults. You probably have some kind of weird compiler/linker confusion going on where code that shouldn't compile is because you're not compiling all necessary files or something. Any number of things could be going wrong.
You definately don't need to duplicate code though. In fact if you do -- the code shouldn't even be compiling. You should be getting linker errors.
Get one.
IDE is short for "interactive development environment" or something like that. Basically it's a frontend for the compiling/linking process. It consists mainly of a text editor and treeview file viewer, and sort of attaches itself to a compiler and a debugger.
How it works is you add files to a "project", edit those files as you would in any other editor (although IDEs often add other nice features to make editing easier, like syntax highlighting, auto complete, etc) then
press a single button to compile. No hassling with makefiles, no worrying about which files you need to compile and how to link them properly. The IDE handles all of it.
Here's a link to Code::Blocks, which is what I use on Ubuntu. There are screenshots and stuff so you can get an idea:
http://www.codeblocks.org/
If you're on windows you might want to look up Visual Studio as well.
But seriously. Get one.