Skip to main content

15.3) Better Approach


Although it involves more typing to begin with, the following is a more efficient approach to compiling a multi-file program:

g++ -c file1.cpp
g++ -c file2.cpp
g++ -c file3.cpp
g++ file1.o file2.o file3.o -o prog
  • Source files are compiled to object code separately, using -c option
  • Compilation (first three commands) is now separate from linking (the last command)
  • If only one source file has changed, we recompile that file only and then relink - which can be significantly faster in the case of large source files