Tutorials on common developer tools for computer science students.
Unlike some IDE softwares (remember, vscode is not a full ide) Visual Studio Code doesn’t have any extremely clear methods to compile multiple files without the user doing so manually. We can address this issue in a few different ways which will be detailed below.
The following is assumed about your environment:
If any of the above assumptions are not true of your environment and you are on Windows, follow this tutorial.
By far the most direct method of compiling and running code: use the command
line. You can open a terminal within VSCode by pressing CTRL+`
. Once you
have your terminal open, we can use commands to compile your files and run
the program.
cd ./path/to/my/folder
projects/
and want to compile code in projects/assignment1/
cd ./assignment1
.cpp
files. .h files will be added with #include""
in the code itself, so we don’t have to worry about those.
g++ *.cpp -o prog1.exe
g++
we are running the compiler with this command, pretty basic*.cpp
here is where you usually put a file name, but in this case we want to compile all files ending in .cpp
. We can do this by using *
, which basically translates to “anything”. So “anything” followed by .cpp
translates to all files that end with .cpp
.-o prog1.exe
the -o
flag just tells g++ that I’m about to give it the name of my program’s executable. On Windows we usually make the extension .exe
but you can make it whatever you want. You can also name it whatever you want, it doesn’t have to be prog1
. This is the file you will run your program with.prog1
./prog1.exe
That’s it! You should have the output of your program in your terminal. Now you just have several thousand bugs and a few hundred debugging statements to get through and you’re done.
This method is simple. When you run something using Code Runner, you’ll notice that is shows a command being executed in your output. That’s because code runner is really just automatically executing some commands for you. We can configure what command it uses in something called the executor map. Don’t worry, you don’t have to know command line for this one, just use the command I’ve provided.
file
in the top leftpreferences
, then settings
in the drop down menu
executor map
Code-runner: Executor Map. Set the executor of each language.
Edit in settings.json
"code-runner.executorMap": {
"cpp": "cd $dir && g++ *.cpp -o $fileNameWithoutExt && $dir$fileNameWithoutExt"
}
code-runner.executorMap
, or if it autocompletes a bunch of lines all at once, its okay! Just find the line that says cpp
and change it to the command written above.
With some careful placement of #include
preprocessor directives, we can make
sure to include and compile all our files just by compiling our main .cpp
file. For classes, your .h
file will need to include the .cpp
implementation file. Now when you include the .h
you get the .cpp
as well.
Your .h
file will also need to include #pragma once
so it isn’t included
multiple times. If I have a class called A
divided into definition file A.h
and A.cpp
, with my program’s main functionality being found in main.cpp
,
I would need each file to look something like this:
#include"A.h"
//...
int main(){
//...
}
//...
#pragma once
//...
class A {
//...
};
//...
#include"A.cpp"
#include
is at the bottom of the file in this case. That is because the implementation (inside the .cpp
) must come after the prototype (which is what we were defining above that in the .h
file) #include"A.h"
//...
include_example/