Headers, Libraries and the rest

This is a discussion on Headers, Libraries and the rest within the C++ Programming forums, part of the General Programming Boards category; Hi,
Just a quick question, I'm wondering about the way code should be stored in files as headers. For example:
...

Headers, Libraries and the rest

Just a quick question, I'm wondering about the way code should be stored in files as headers. For example:

I have a class, called Books for example. What extension should I give to the file that contains the definition, the declaration and the other stuff I left out.

This is what I normally do, but I'm not sure if its considered right:

books.HPP

Code:

#ifnef...
class Books
{
....
int CountBooks();
}
#endif

books.CPP

Code:

int Books::CountBooks()
...

Is that considered the proper way to do it? Also, which file includes which? I assume that the CPP file should #include the HPP file, but if I do it like that, and I have another CPP file that includes the HPP file, ie:

main.CPP

Code:

#include "books.HPP"
Books myBooks......

There is no way that books.CPP gets included.

If anyone can tell me whats considered the standard way of doing this I would be most grateful (and for C files as well, eg *.H and *.C)

Beautiful! Thanks Darkstar it works perfectly. One question though - how come I dont have to tell g++ to use object files for headers such as the standard library? I've looked in a few of them and they dont have the function definitions in them, are their .o files located elsewhere?

Yes, the .o files related to the headers supplied by GNU are located in the .a archive (library) files in the Lib directory. GCC and G++ will automatically have LD search this folder to resolve externally referenced object code to build the exe.

I believe that if you put books.o in the lib folder, you do not even have to pass it to G++, all you would have to do is just hand it the main source file.

Originally posted by DarkStar You don't include it. That file must be compiled to an obj. When you hand main.cpp to the compiler, you must also hand books.obj to the compiler to resolve the external reference to it in main.