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.