A header file is a plain text file containing constants, variables, function prototypes, etc - so you can simply write it in any plain text editor and save it as "filename.h".
If you want to include code, then put this in a seperate .cpp file ("filename.cpp").
Here's a very simple example;
First a program which uses a function and a constant defined in a header file;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
// header_howto.cpp
// a program which uses the howto.h header
//
#include <cstdlib>
#include <iostream>
#include "howto.h"
using namespace std;
int main(int argc, char *argv[])
{
cout << "\n\n";
// call a function - prototype in howto.h, code in howto.cpp
hello();
cout << "\n\n";
// declare a variable and initialize to CRIKEY (found in howto.h)
int yada_yada = CRIKEY;
cout << yada_yada;
cout << "\n\n";
system("PAUSE");
return EXIT_SUCCESS;
}
|
Now the header file (howto.h)
1 2 3 4 5 6 7 8 9
|
// howto.h - header file
// constants
#define CRIKEY 100;
// variables
// function prototypes - BUT code is in howto.cpp
void hello();
|
and the code for the function hello() is in howto.cpp;
1 2 3 4 5 6 7 8 9 10 11 12
|
// howto.cpp
// code for header file howto.h
#include <cstdlib>
#include <iostream>
#include "howto.h"
using namespace std;
void hello()
{
cout << "Hi World!";
}
|
I can only speak for how these are put together using Dev-Cpp, which is as follows;
1. Open a new project "header_howto"
2. Add the above three files "header_howto.cpp", "howto.cpp" and "howto.h" to the project.
3. Compile just like any ordinary single file program.
I believe that for a command line compiler such as cc you need only use;
|
cc header_howto.cpp howto.cpp
|
In broad outline, it's that easy.
Hope it helps,
Muzhogg