VSCode Multi File Projects

VSCode is actually quite miserable at allowing you to build multi-file C++ projects. There are a number of extensions that can be installed to make it a little easier, but my suggestion to you is to just use dedicated folders for your assignments and then use the built-in Terminal widget (built in to VSCode) to build the project. Let's look at an example.

Suppose you are working on Assignment #4. Start by making a directory to store Assignment projects (you may already have one). Suppose I already had a "Projects" directory in my home directory on Windows. I'd begin by changing into that directory, like this:

01-ProjectsDir


Then, I'd create a new directory for my Assignment #4. I'm going to call it "A4". Below, I'll create the directory and then change into it:


02-MakeADirectory

Once I've created the directory, I go back into VSCode. If you already have a folder "open" in VSCode when you launch it, go to the "File" menu and choose "Close Folder". Then you should have a screen that looks something like this:

03-ClickOpenFolder


Click on "Open Folder"

04-SelectFolder

Navigate to the directory you just created for A4 and select it. You will be asked if you want to trust the folder:


05-TrustDialog


Go ahead and click "Yes, I trust the authors".

You will then be taken to the main VSCode window. Select "New Text File" from the File menu…

06-NewTextFile


You will be given a new text file window where you can type in C++ code. I'm going to start by typing in some code for Menu.h and then I'm going to save the file as Menu.h.


07-MenuH


When saving the file, it should default to saving into the folder that we just created for A4.

Now I'm going to repeat this process and put some code in for Menu.cpp. (create a new text file, type in the code for Menu.cpp and then save the file as Menu.cpp in the A4 directory):


08-MenuCPP


Finally, I'll repeat the process and create a main.cpp file in the same directory. (Create a new text file, type in the code for main.cpp and then save the file as main.cpp in the A4 directory):



09-mainCPP


Obviously, the code I've typed in is not a complete solution, but it's enough to show you how to build with multiple files.

Next, bring up the Terminal in the window by going to the "Terminal" menu and selecting "New Terminal"


10-ShowTerminal

You should already be in the proper directory, so you can type the command line command to build with multiple files (in this case we're building main.cpp AND Menu.cpp):


11-BuildApp


The command I entered above used the "-o" switch to specify that we want the executable created when the program is compiled to be called "a4". We can run it directly in the terminal by typing "./a4" like this:


12-RunApp


Again, this code I typed in was not a complete solution; but the code we typed in did run properly.


Again, this is not an optimal solution to building multi-file C++ projects. Most IDEs such as Xcode, Visual Studio, etc. provide you with a way to define a project where multiple .cpp files can be added and built properly without you having to type a command-line command!

I hope this helps while we continue to focus on C++03 code. Beginning with Assignment #7, you will be able to use Visual Studio/Visual C++ if you desire.