Main.cpp

#include <iostream.h>
#include "functions.h"

int main(){
    print_hello();
    cout << endl;
    cout << "The factorial of 5 is " << factorial(5) << endl;
    return 0;
}

hello.cpp

#include <iostream.h>
#include "functions.h"

void print_hello(){
   cout << "Hello World!";
}

factorial.cpp

#include "functions.h"

int factorial(int n){
    if(n!=1){
    return(n * factorial(n-1));
    }
    else return 1;
}

function.h

void print_hello();
int factorial(int n);

When I compile them simultaneously, then it worls fine. But can you please explain how linking occurs in this example? what is purpose of functions.h and how other files are getting the definitons of the 2 functions (factorial and other)? Can you explain what is happening when I compile them together and what will be the content of .o file. Explain the flow. Any help will be appreciated. Thanks in advance.

The purpose of the header files is to provide declarations for the functions, so that the compiler knows what their types are. This is necessary for the compiler to properly type check the code and generate the right code for function calls (it needs to know the types to generate the proper implicit conversions for example and it needs to know which functions are variadic because then the steps to call the function are likely different). It is unrelated to linking.

The contents of each .o file will simply be the code for the functions defined in the corresponding .cpp file with the caveat that function calls contain place holder addresses. For example factorial.o will contain the code for factorial, hello.o the code for print_hello and Main.o the code for main.

Then when the .o files are linked together, an executable is created that contains all of the code of the .o files together and the placeholders in the function calls are replaced with the actual addresses that the functions have in the executable file.

What would have happened if I didn't involve function.h? I think function.h is not doing anything in this example. Explain this thing.

If you hadn't included functions.h in Main.cpp, you'd have gotten a compilation error telling you that print_hello and factorial were undeclared.

When compilation happens functions are transformed to binary and are given a well-known location in the file so that other functions may jump to them by name. Each file (compilation unit) is compiled seperately so if you provide a definition elsewhere (in another source file) you have to tell the compiler that you expect to call a function with a particular signature and that the definition for that function will appear in another location provided at link time. This is why you have to provide/include the header file that has function declarations in order to compile.

Once each compilation unit is compiled to binary the linking process takes place where unresolved symbols are linked to the external locations in other files. This is why you get linker failures when you include the proper header files but fail to provide the necessary library with the function definition.

In your example:

main.cpp compiles with the declaration of factorial and print_hello (via the included file) but has no definitions so compilation will succeed syntactically but linking will still need to resolve the definitions.

hello.cpp and factorial.cpp each provide a definition of a function that can be linked to since they have the same signature as the declarations in function.h

function.h provides the function signatures for main.cpp to reference at compile time and hello.cpp and factorial.cpp to provide definitions for references to resolve to at link time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.