4

I have a question about libraries. When I write a library I have 2 files: library.h and library.cpp.

The first one contains the prototypes of the functions and the second one contains the definitions. Well, in library.cpp I include #include "library.h", so they are connected in one way, but what happens with the header? Why don't I have to write #include "library.cpp" in the header?

When I use the library in the main file, I write #include "library.h", which includes the prototypes, but how does the compiler know where the definitions are?

eldarerathis
  • 35,455
  • 10
  • 90
  • 93
Miguel Ruiz
  • 65
  • 2
  • 7

5 Answers5

13

Explained briefly:

(1) Your library.cpp file is sent to the preprocessor.

(2) The preprocessor reads the line #include "library.h" and goes and finds the file library.h.

(3) The contents of library.h are literally copied into the library.cpp file.

(4) The library.cpp file is compiled and linked with the main file.

Thus, all of the "prototypes" in the header are copied into the top of the implementation file. .cpp files are compiled and linked. The header files themselves are not compiled -- their contents are copied into the .cpp files.

Derek
  • 1,104
  • 13
  • 35
  • Why (1) the `library.cpp` file is sent to the preprocessor? Does it only apply to using CMake? If I simply use `g++` on a file that `#include "mylibrary.h"`, it seems that it does not work? – SOFe Jul 09 '16 at 10:32
1

The preprocessor pulls the header file into the CPP file, so the compiler sees the prototypes and the definitions together.

If you were to pull the CPP file into the header file, you would either send the preprocessor into an infinite loop, or by using

#ifndef __FOOBAR__  
#define __FOOBAR__  
(code file)  
#endif

around the header AND source files you would read the file just once.

Codes with Hammer
  • 788
  • 3
  • 16
  • 47
1

There is a tool called linker which is responsible for link your generated object files. You should look for compilation process to understand it better.

dieram3
  • 592
  • 3
  • 7
  • he asked, "where the definitions are", not declarations (prototypes), the search is done by the linker – dieram3 Jul 31 '13 at 02:05
  • I'll admit I did miss that last question on his part, to which you are answering. Your answer doesn't really speak to the any of the `#include` questions however. – Jonathon Reinhart Jul 31 '13 at 14:13
0

All the cpp files are compiled seperately and the code is accumulated in a big pile somewhere. The linker collects all the symbols in your code and assigns them an address within this pile. So although your file doesn't see library.cpp directly, it knows the symbols from library.h and the assigned addresses. It can then zoom directly to the required code in the big pile. Not the most technical answer I know..

Neil Kirk
  • 21,327
  • 9
  • 53
  • 91
  • -1 This is just so full of inaccuracies, and you were even aware of it. – Jonathon Reinhart Jul 30 '13 at 21:09
  • I thought it was obviously a simplification. Where's your answer Mr Smartypants? – Neil Kirk Jul 30 '13 at 22:42
  • 2
    Not sure why you're so offended. But there's no point in me answering - there's already an accepted answer, which with I agree. So I've up-voted that answer, and down-voted any others that I thought were poor or incorrect. That's how StackOverflow is supposed to work. I don't need to *prove* myself to you by adding another answer to the mix. If you're questioning my abilities, you can simply [review my profile](http://stackoverflow.com/users/119527/jonathon-reinhart) yourself. – Jonathon Reinhart Jul 31 '13 at 14:08
  • Besides, you're answering this at a linker stage, when the question was asking about header files, which no longer play a part after the compilation stage. – Jonathon Reinhart Jul 31 '13 at 14:10
  • "which includes the prototypes, but how does the compiler know where the definitions are?" – Neil Kirk Jul 31 '13 at 15:18
-1

It was my understanding that when you type #include "library.h" in your code the compiler is set to also load library.cpp. I was taught in school that there isn't really any way to break this connection unless you were to manually compile and link each file either using compiler commands or a makefile appropriate for the system you're using (Windows, *nix, MacOS, etc.)

You have to type #include "library.h" in the library.cpp file because that is where the function prototypes are stored. You can "cheat" and put the function prototypes in the .cpp file and just type #include "library.cpp" but this is bad practice and is generally discouraged in the programming community.

Joel Trauger
  • 720
  • 1
  • 4
  • 24
  • If you are using an IDE, it knows to compile your `cpp` files to object files and link against them. If not, you have to do it manually. Including your .cpp file will only work if you only include the file once, or if all functions are marked inline. Otherwise linking will fail with `multiple definition` errors. – Bill Jul 30 '13 at 20:59
  • 1
    Putting `#include "library.h"` *does not* tell the compiler to also compile `library.cpp`, and it doesn't tell *anything* to the linker. What you were taught in school about "breaking this connection" is borderline bogus. And I would openly go Linus Torvalds on anyone that would include a `.c` file. By the way, I'm not sure what you were -1'ing there. – Jonathon Reinhart Aug 15 '13 at 21:46
  • I never said that. I said that typing `#include "library.h"` would tell the compiler to load the .cpp file, too. And then I later said that the .h file contained the prototypes and you could cheat by putting the prototypes in the .cpp file, but that is bad practice. Nothing I said there was false. I was -1'ing your negative attitude. No one needs that. – Joel Trauger Nov 08 '13 at 16:59