0% found this document useful (0 votes)
81 views1 page

Beginning C++ Programming: Compiling Multiple Source Files From The Command-Line

When compiling C++ programs with multiple source files from the command line, each .cpp file must be compiled and then linked together with any system libraries to create an executable. If using an IDE, it handles including files in the build process, but from the command line each .cpp file must be included. For example, a project with main.cpp, Account.cpp, and Account.h would be built with g++ -Wall -std=c++14 -g main.cpp Account.cpp. Failing to include a .cpp file will cause linker errors, and #including a .cpp file in another should be avoided.

Uploaded by

Benjamin Mullen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views1 page

Beginning C++ Programming: Compiling Multiple Source Files From The Command-Line

When compiling C++ programs with multiple source files from the command line, each .cpp file must be compiled and then linked together with any system libraries to create an executable. If using an IDE, it handles including files in the build process, but from the command line each .cpp file must be included. For example, a project with main.cpp, Account.cpp, and Account.h would be built with g++ -Wall -std=c++14 -g main.cpp Account.cpp. Failing to include a .cpp file will cause linker errors, and #including a .cpp file in another should be avoided.

Uploaded by

Benjamin Mullen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Beginning C++ Programming

Compiling Multiple Source Files


from the Command-Line

When we compile C++ programs that consist of multiple .cpp source files we must be sure that each is
compiled and then all are linked together along with any system libraries to produce an executable
program.

If you are working within an Integrated Development Environment (IDE), such as CodeLite, Visual
Studio, Xcode, and others, when you add the new classes to your project, the IDE takes care of including
them in the build processes.

However, if you are building from the command line, you must make sure that all of the .cpp source files
are included on the command-line command that builds your project.

For example, suppose our project consists of:

main.cpp -- the main program driver which includes “Account.h”


Account.cpp -- the Account implementation which also includes “Account.h”
Account.h -- the Account specification

In order to build this project from the command-line you must include all (2 in this case) of the .cpp
source files as arguments to the compiler. For example:

g++ -Wall –std=c++14 –g main.cpp Account.cpp

If you omit any of the .cpp source files then you will surely get linker error.

It is common for students to ‘fix’ the linker errors by using #include “Account.cpp” from main.cpp
This is a bad idea and will lead to all sort of errors later as our projects become larger.

Please do not #include any .cpp source files, you should only #include .h header files.

Please let me know in the Q/A or Private Message if you have any questions or run into any issues
building your multiple source file projects from the command-line.

Best regards,
Frank Mitropoulos

1 Updated: 5/2018

You might also like