Multiple files project in C++

It’s not uncommon that many projects contains different files. For example:

Project proj contains file add.h, add.cpp, and main.cpp. Here: add.h contains declarations of functions, add.cpp defines the functions while main.cpp has the main function.

add.h: starts with header guard

#ifndef ADD_H

#define ADD_H

int add(int x, int y);

int max(int x, int y);


add.cpp: defines the above functions

int add(int x, int y){

return x+y;


int max(int x, int y){

return (x>y)?x:y;


main.cpp: contains the main functions

#include <iostream>

#include “add.h”

using namespace std;

int main(){

int a=2,b=3;




The sequence of performance is illustrated as following:



#header-guard, #h_file, #multiple-files-project