views:

324

answers:

3

I have text.cpp which includes header.h and header.cpp which includes header.h.

Will header.cpp be compiled as well? I'm following a guide here, and I am thoroughly confused.

Also, what is the correct terminology for what I am asking? I know I sound like a moron, and I apologize, but I'm ignorant.

Oh, int main() is in test.cpp.

Also, if header.cpp includes <iostream>, why can't I use iostream function calls in text.cpp if it is included? If I include <iostream> in text.cpp will it be included in the program twice (in other words, bloat it)?

+3  A: 

You tell your compiler which C++ files to compile. #include has nothing to do with it.

For example, if you are using g++:

g++ text.cpp // Doesn't compile header.cpp
g++ text.cpp header.cpp // Compiles both

(or, alternatively you can compile one file at a time and then link them)
g++ text.cpp -o text.o
g++ header.cpp -o header.o
g++ text.o header.o -o your-program

If you use Visual Studio and you created a project, all C++ files you create will be automatically compiled.

If you are using neither, tell me the name of your compiler and I can tell you the exact syntax :)


Now, for your other question:

Also, if header.cpp includes iostream, why can't I use iostream function calls in text.cpp if it is included? If I include iostream in text.cpp will it be included in the program twice (in other words, bloat it)?

#include tells the compiler to simply "copy all the contents of the file you are including, and paste them where the #include line is". So, in theory, you could simply open iostream with notepad, select all, ctrl-c and ctrl-v it in your .cpp file and the end effect will be exactly the same =)

So yes it needs to be included for each .cpp file in which you wish to use it, and it won't "bloat" your program: it contains only class definitions, extern functions, etc.

Oh, and this goes without saying, but C++ is a very very vast and difficult programming language, you will have much better luck learning it through a book than a guide. If you don't want to spend any money, an okay free (downloadable) C++ book is Thinking in C++, Bruce Eckel. Otherwise if you want to buy one you can find a good list here.

Andreas Bonini
Thank you very much. I'll keep your advice in mind.
aaron
There is a C++ book-list on SO: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list
Georg Fritzsche
A: 

header.cpp is only compiled if you compile it. It doesn't get automagically sucked up when you compile test.cpp. To produce a running program, you also have to link the resulting .o files in a single binary, as follows (oversimplified):

c++ -c test.cpp
c++ -c header.cpp
c++ -o test test.o header.o
Marcelo Cantos
A: 

You compile each source file separately, or together, then link the resulting object modules.

E.g. Visual C++ (Together)

cl text.cpp header.cpp /Fefoo

or separately,

cl /c text.cpp
cl /c header.cpp
link text.obj header.obj /out:foo.exe

To get a resultant EXE image, foo.exe.

Also, if header.cpp includes , why can't I use iostream function calls in text.cpp if it is included? If I include iostream in text.cpp will it be included in the program twice (in other words, bloat it)?

Since headers only contain declarations, there is no harm in including them many times. They will not bloat anything, since the compiler throws them away after doing type checking.

Though if you have type definitions in them, you need #include guards so that the types are not redefined.

Alex