0

I've just been given my first real C++ application on the job after working through some books learning the language. It was my understanding that your cpp source files required the cooresponding header, yet one of the libraries in my project is building fine with a number of cpp files that DO NOT include the cooresponding header. This particular cpp implements a class found in a header that has a different name and a number of other pieces of code beyond just the original class declaration.

How is it that the cpp can compile functions belonging to a class that it has no knowledge of?

Can the implementation of these functions be compiled independently and are simply called when a client application using the library (and including the header with the class declaration) calls the corresponding member function? If this is the case, how is the implementation binary referenced by the client application? (I assume this is the linker...but I would love to have this cleared up).

I anticipate the answer may expose a misunderstanding of mine with regard to the include and compilation process, and I'd really like to learn this aspect of C++ well. Thank you!

MLam
  • 1
  • 1
    A header file is just textually included in the translation unit. There's no magic. On the contrary header files are astoundingly crude relics of the 1970s. You don't need header files at all, not that that is a good idea to take that idea seriously. If you have a translation unit that does not offer any functionality to other actors in the system then there would be no need for a header. A good example would be a small translation unit containing your `main` function. – David Heffernan Mar 27 '12 at 17:08
  • have a look at this SO qn about compilation and linking : http://stackoverflow.com/questions/6264249/how-does-the-compilation-linking-process-work – Sanish Mar 27 '12 at 17:11

2 Answers2

2

When a c++ source file is compiled the first stage it goes through is preprocessing. When the include directive is reached the file is found and the entire contents of the file, whatever that may be is included into the source file, as if it had been written in the source file itself.

You will be able to define any function from a class in any source file that includes the class's declaration, this is the source file "knowing" about the class / function".

There's also no requirement that the contents of a header and a source file will have any relationship. It's widely considered to be very good practise however.

The implementation of each compilation unit (a source file) is compiled independently. Any function definition could be placed in any compilation unit, and it would make not difference whatsoever. When the compilation units are linked together the usages of every declaration are matched to all the definitions.

The only other pattern that some people might use other than the 1:1 relationship between source files and header files (that I can think of) is that the header files each describe a class and each source file would implement a collection of related functionality. But this is a bad idea (in my opinion) because it would encourage the definitions of various classes to because highly coupled.

eyesathousand
  • 587
  • 2
  • 10
0

These are several questions. You should try to split these.

  1. The name of the files where something is declared is not relavant. The compiler gets the preprocessor output independent from the files that have been read by the preprocessor. The compiler might be use some file/line information in the preprocessed file to issue more readable diagnostic messages. When you declare a class in a header file and copy that declaration to a implementation file everything works fine as long as you don't change one of the copies of the declaration. This is bad dangerous and should be avoided. Declare anything always once.

  2. When you compile an implementation of a class member function you get a function that the linker can link to your client program. A good tool chain is able to link only the functions that are accessed. So you can separate the interface (in the header files) from the implementation that is provided in a static library. The linker will add each object module in the library to your executable until all symbol references are resolved.

harper
  • 13,345
  • 8
  • 56
  • 105