Compiling Multiple Files

The file below contains only the main function, not userInputNum or printNum.

<aside> 💡

We don’t actually need to include the #include <iostream> since the main function doesn’t actually use anything from that library. All our input and output are implemented in the input.cpp and print.cpp files, which are linked via header files into main.

</aside>

#include <iostream>  // directs to the standard library
using namespace std;
#include "input.h"  // searches for the file in the same directory
#include "print.h"

int main(){
	int x;
	x = userInputNum();
	printNum(x);
	return 0;
}

We have to include 4 total files to make the original main.cpp function work:

int userInputNum()
void printNum(int)
#include "input.h"
#include <iostream>
using namespace std;

intUserInput(){
	// function implementation
}
#include "print.h"
#include <iostream>
using namespace std;

void printNum(){
	// function implementation
}

Compilation

To compile all of these files with one command, we do the following:

g++ main.cpp input.cpp print.cpp -o main