3

I have a question about source file and header in c++.

I have a header which is used to declare the functions. And I implement these functions in C++.

Now I need to use these functions in other files. Should I include both source file and header file in order to use these functions?

sbi
  • 219,715
  • 46
  • 258
  • 445

4 Answers4

8

The basic compilation model used by (C and) C++ is thus:

  1. You put declarations that are to be shared between source files into header files. The source files that need to access those declarations then include these header files.
  2. When compiling, the preprocessor will (recursively) replace #include directives with the file(s) included. The result is called a compilation unit.
  3. The compiler then compiles these compilation units, one at a time, into object files. (Note: The compiler always only ever sees a single translation unit. It has no access to whatever is declared and defined in other translation units.) Basically, object files are readily compiled code, but all the references to symbols (functions, variables) outside of their compilation unit are still symbolic.
  4. The linker is then passed all object files and links references to symbols across those object files to the symbols' definitions, spitting out an executable if all goes well.

In C++, this is often a bit more complicated in practice (especially due to inlining and templates, but also with features like link-time code generation), but that's the basic principles.

The implications of that are:

  • Source files usually only include header files.
    Since the whole preprocessor magic is just a simple text replacement engine without any knowledge of (C or) C++ and about its purpose in the process described above, it can be bend and and abused to do other things. In this, including source files has been done sometimes to achieve some goal. But it's rare.
  • Symbols can be declared as often as wanted, but must be defined exactly once.
    If a linker can't find a symbol that has been declared (and thus references to it have been accepted by the compiler), it will spit a nasty error message into your face. If it finds multiple definitions it will do likewise.
  • The compiler doesn't care whether a declaration is coming from a header included by the preprocessor or has been written directly into the source file.
    However, if you write declarations directly into source files, the compiler, not being able to "look" into other translation units, can't warn you that they are out of date. If you put declarations into header files instead, it is much easier to keep them in sync with their corresponding definitions, and often the compiler can even diagnose if they don't match.
  • Any project with more than one source file can only be built by several compilation runs (one for each source file) with the linker linking the resulting object files. Often though, IDEs hide that behind their project management.
    If you change a header that is, directly or indirectly, used in many source files, you will have to re-compile most of your project. If you change a source file, you will only have to re-compile that one source files (and re-link the executable, of course).
Community
  • 1
  • 1
sbi
  • 219,715
  • 46
  • 258
  • 445
  • @duedl0r: Those who can muster the attention span. – sbi Aug 03 '11 at 12:13
  • 1
    those who ask questions because they want answers? Those who aren't averse to *learning*. – jalf Aug 03 '11 at 12:18
  • [Yeah, too long! Nobody on this site needs good answers!](http://dl.dropbox.com/u/26824/facepalm.gif) – Cat Plus Plus Aug 03 '11 at 12:19
  • Can we implement the functions in headers? –  Aug 05 '11 at 13:54
  • If you do so, and those headers are included by more than one source file, then these definitions will end up in multiple translation units, making the linker very upset about it, because it doesn't know which definition to link references to. The only exceptions (I can think of) are inline functions (note that class member functions defined within their class' definition are automagically inlined) and function templates. – sbi Aug 05 '11 at 15:02
1

You should include only header files, but during linking time you should pass to compiler object file names where these functions implemented.

1

You only include the header in the file that you wish to use the functions. During linking the linker will look for the object file that matches the header's definitions, so you have to make sure that it is also visible to the compiler (by having the source file present).

On the other hand; if you did include the source file as well, most likely you would get a multiple symbol definition error. So, don't do that.

Seb Holzapfel
  • 3,793
  • 1
  • 19
  • 22
  • Actually, during linking the _linker_ will look for _object files_ with definitions matching the declarations in the header. – sbi Aug 03 '11 at 09:07
  • @sbi - Edited. I always thought that linkers were a *part* of the compiler though. – Seb Holzapfel Aug 03 '11 at 09:15
0

Only include the header files in other source files (clients of your functions). While building, however, you need to input all source files to compiler/linker.

Ajay
  • 18,086
  • 12
  • 59
  • 105