How C ++ header files hold the cpp respective file definitions if they do not include them on themselves?

79 Views Asked by At

I know that cpp libraries contains:

the .h file (header file) in where are the function, variables and classes' members declarations and

the cpp file in where it contains the variables,function and classes' members definitions.

It includes the respective existent .h file in the same diectory.

While coding and using the libraries, it is included the .h file.

How the file holds the cpp definitions if it does not include the cpp file itself?

1

There are 1 best solutions below

3
Remy Lebeau On

How the file holds the cpp definitions if it does not include the cpp file itself?

It doesn't "hold the cpp definitions" at all.

A .cpp file (aka, a Translation Unit) contains definitions, and it can also include whatever .h files it needs to pull in their declarations. A Translation Unit (and everything it includes) gets compiled into an Object File. Any declarations are just references to symbols which may be defined in any Object File.

The linker, not the compiler, then brings all of the Object Files together, resolves their symbol references to connect them with their relevant definitions, and creates the final Executable File.

Thus, a symbol that is declared in one Translation Unit can be matched up to its corresponding definition, regardless of which Translation Unit actually implements it.