How To Use Multiple Code Files In C++

Introduction

When we code in any programming language, if our code is a small console application, then we can easily make it in a single file. But, once our program gets larger we should split our programs into multiple files for easy maintainability and reusability of our code.

To do this, we strongly recommend using an IDE. this will greatly ease the process. Although you can also use Visual studio code, we suggest you use Visual Studio Community (it is free). In this article, we will be using Visual Studio Community.

Requirements

  • Visual Studio Community
    • Desktop development with C++ workload

How To Use Multiple Code Files In C++ For Beginners

  • Basic Knowledge of C++

NOTE: I will not be covering how to install or step up visual studio community in this article.

Step-by-step instructions

Step 1

Launch Visual Studio Community by double clicking on its icon or right click/open.

Step 2

Click on create a new Project.

How To Use Multiple Code Files In C++ For Beginners

Step 3

Search for C++ Console app and Click on it.

How To Use Multiple Code Files In C++ For Beginners

Step 4

Put your Project name and a location (Where you want to store your project) in the fields. And click Create.

How To Use Multiple Code Files In C++ For Beginners

Now VSCommunity will create a solution for you.

Solutions: A solution is simply a container Visual Studio uses to organize one or more related projects.

Step 5

When VSCommunity Editor opens up. Look for Solution Explorer, it's either in RHS or LHS. In solution explorer click on Source files and you will see a initial file that the vs code provides (its name might vary, you can rename it). And it will open the hello world program in editor

Step 6

Create a new item in solution. By right clicking Project in solution explorer then click Add and in next option menu click on new item.

Step 7

Select C++ File (.cpp) File. Give the new file a name(we will use “square”), then click add and it will be added to your project.

How To Use Multiple Code Files In C++ For Beginners

In newly created C++ file, for the sake of simplicity, we will write a function that takes one integer number as a parameter and then returns the square of given integer number.

How To Use Multiple Code Files In C++ For Beginners

Now edit the Main File(renamed to “Multiple Files Article.cpp”) that VSCommunity provided us to use the function Square we have just created.

Now we have 2 files in our solution

Multiple Files Article.cpp

#include <iostream>
int main()
{
    int num = 0;
    std::cout << "The a number to find it's square: ";
    std::cin >> num;
    std::cout << "The square of the number" << num << " is " <<square(num);
}

Square.cpp

int square(int num)
{
    return (num * num);
}

Now we try to run the program.

(To run the code, on the menu bar, choose Debug, Start without debugging. A console window opens and then runs your app.)

How To Use Multiple Code Files In C++ For Beginners

Vscommunity will compile either Multiple Files Article.cpp first or Square.cpp first either way it will throw an error.

How To Use Multiple Code Files In C++ For Beginners

Compiler compiles the file individually and doesn’t know/remembers the contents of other cpp file implicitly. This is done intentionally in C++ for avoiding name conflict.

To solve this we will have to either place the definition of function before function main or use a forward declaration of function which is what we will be doing, Square.cpp will remain unchanged.

Step 8

Add a forward declaration in our main file(Multiple Files Article.cpp) like this.

Multiple Files Article.cpp

#include <iostream>
int square(int num); //This will tell the compiler that this function is written somewhere in solution.
int main()
{
    int num = 0;
    std::cout << "The a number to find it's square: ";
    std::cin >> num;
    std::cout << "The square of the number" << num << " is " <<square(num);
}

If you try to run the program now you will see that this time the program runs as expected.

Congratulations, you’ve just used multiple files in your C++ Solution/project.

If you get an error, read this:

  • If you get a compiler error about square not being defined in main, i means that you must have forgotten the forward declaration for function square in main.cpp.
  • If you get a linking error about square not being defined it’s most likely that square.cpp is not added to your solution correctly.
  • It’s possible that you added square.cpp to the wrong project.

NOTE
Do not #include “square.cpp” in “Multiple Files Article.cpp.” Here use compiler to insert the code of square.cpp directly into main.cpp instead of treating them as separate files.

Summary

In this article, we have learned that instead of writing all the functions in same C++ file in which we have main function, which can actually get messy real quick, we can have the functions in separate files, this helps in reusability of code and maintaining the code much easier.

In future, I will be covering how you can have classes in different files as well as using custom header files without conflict.

I hope this article helped you, if you liked it please share it with your friends.