how to properly use #include and .h files

Hi there everyone. I'm slowly figuring things out but im still a bit hazy on how to use the #include directive and structure my .h & .cpp files heres an example.

project files:

main.cpp
peson.class.h
person_class.cpp

So, im main .cpp i use #include "person_class.h" , thats fine and makes sense, but then i would assume i have to put #include "person_class.cpp" into my header file or somewhere else, otherwise how does the person_class.cpp implementation actually get linked into the program? I say this because when i look at other peoples code i never see any .cpp file getting included and that throws me.

Could anyone shed some light on this as im getting a little confused...

Im have feeling that makefiles will come into this but i dont really know, if anyone knows a good tutorial on these things or could suggest something to read that would be great.

Thanks all
You include header files only. Do not include cpp files.

How it works is each source (cpp) file gets compiled on its own and gets turned into what is called an "object file". The linker (a separate program from the compiler) then links together all the object files to produce the final executable.


If you're using an IDE (like you should be), all this is done automatically for you. All you have to do is add all your source files to the same "project". Then just press a single button and the IDE will automatically compile every source file, then link all object files together.

If you're a masochist and aren't using an IDE, you'll have to use some commandline wizardry (see your compiler's documentation) or write a makefile to do this (see make's documentation). Of course neither of these solutions are as simple, and personally I think anyone that willingly compiles their programs that way is insane. But that's just me.

A more sane alternative would be to use a tool that automatically generates a makefile for you.



More general reading on the subject:

http://cplusplus.com/forum/articles/10627/
Hi jonathontopf,

in the .hpp file you need to #include the headers for the classes, types that you use in that file , that have been declared elsewhere (e.g., #include <vector> if you use std::vector).

Also, in any .cpp file, you need to #include all header files for the classes, types, etc. that you use in that file that have been declared elsewhere. In person_class.cpp you undoubtedly define and use classes, types, etc. that you declare in person_class.hpp, so you need an #include "person_class.hpp" in there, as well as the standard headers such as #include <vector> , if you use vectors, as mentioned above. (Also note that you should use quotes for header files that you define and <> for standard headers.)

You then compile your individual source (i.e. .cpp files) to obtain an identical number of object files. The included header files will expand to provide the complier with the declarations contained within it. In the case of person_class.cpp, you will get a file person_class.o by doing, e.g.,

g++ -c person_class.cpp


In main.cpp, once again, you include all header files for the types, classes that you use in main.cpp that are declared elsewhere.

You then build the execution file and link it to the object files of your source files by doing, e.g.,

g++ -o main main.cpp person_class.o

You will then get an executable ``main'' that, when run, will execute your full program.

As Disch says above, if you use an IDE then this is done automatically once you have specified you build configurations. I have shown here how you could compile and link your source files to you main application you from the command line.

Hope that helps.
Thanks for the quick response, that seems to clear things up a bit for me. I'm just compiling with gcc right now and ive beet trying to stear clear from an IDE to keep everything easily portable, i dont want to be stuck with an xcode project when i want to compile a program for windows as well. This is the theory anyway, please say if it makes no sense, I'm most concerned with doing things the right way right now as i know i am quick to pick up bad habits

The other reason i havent been using an ide is because i find it stops me actually learning whats going on under the hood and thats usually a big factor in terms of problems arising.

I'd love to hear from other people about how many people use IDE's and what people think of the pros and cons, its one of those subjects that is hard to find good info on.

What IDE do you recommend Dish? im on a mac but also have a kubunto partition.

I'm off to read up on make files me thinks
thanks dangrr888, im slightly confused .hpp? did you meen .h? Linking via the command like is making sense now but i can see how it could get out of hand with lots of files

I thought of another question though, if i have a class called person_class which uses <vector> where should #include <vector> go? should it go in person_class.cpp or person_class.h or both?

Also im beginning to understand how important an ide or a makefile is. Could anyone show me what a simple makefile for the above example would look like e.g. for a project withe the following files:

person_class.h
person_class.cpp
main.cpp

thanks everyone, i seriously appreciate it.

j
When I started out, I built everything using gcc via the terminal on my mac, since, as you say, I wanted to be clear about what was going on under the hood. But now I use Eclipse, and everything is easier and faster (e.g., you don't have to compile each source file individually!) with much better interfaces for debugging.
Hi,

im slightly confused .hpp? did you meen .h?

you can use either, makes no difference as far as I'm aware, so long as you use .hpp for files with a .hpp extension and .h for files with a .h extension.

if i have a class called person_class which uses <vector> where should #include <vector> go? should it go in person_class.cpp or person_class.h or both?

If you explicitly use vector it in both the .hpp and .cpp files it should go in both. I believe that if its not needed by the compiler it wont include it more than once provided you use header guards. Btw you should always use header guards in your header files.

Also im beginning to understand how important an ide or a makefile is. Could anyone show me what a simple makefile for the above example would look like e.g. for a project withe the following files:

Using my example above:

1
2
3
4
5
6
7
8
all:
   g++ -o main main.cpp person_class.o

clean:
   rm person_class.o main

person_class:
   g++ -c person_class.cpp


EDIT: Btw these are what header guards look like:

1
2
3
4
5
6
7
8
#ifndef GUARD_PERSON_CLASS_H //top of header file. Make sure whatever 
                             //you put after the directive is unique to your whole application 
                             //(i.e. not the same name as an other object, class, header guard)
#define GUARD_PERSON_CLASS_H //whatever you put after the define directive is the same as after the ifndef directive

//contents of header file

#endif 

Last edited on
ive beet trying to stear clear from an IDE to keep everything easily portable


Using an IDE does not hinder portability of your code in any way. It just makes writing code and compiling easier and less error prone.

Don't make things harder on yourself "just because". Writing code is hard enough by itself.

The other reason i havent been using an ide is because i find it stops me actually learning whats going on under the hood


I suppose there is some truth to this. Although I'd argue the IDE doesn't stop you from learning things, it just doesn't force you to learn some things.

What IDE do you recommend Dish?


When I wasn't on Windows I used Code::Blocks and it was pretty nice.
@Disch
When I wasn't on Windows I used Code::Blocks and it was pretty nice.


I tried Code::Blocks before migrating to Eclipse, but found it to be very slow and that it kept crashing when run from a mac. Heard others with similar problems. If your not using a mac then I think its better.
Yeah I was on Ubuntu at the time and it was fine. Not as good as VS though.
Topic archived. No new replies allowed.