This is for students who are running on a Non-Microsoft OS, or students who would rather use a lighter weight IDE
-
First download VS Code and install it.
-
Install a C++ extension in VS Code The extension button is the square logo on the left side bar
-
If you would prefer to download this template as a package you can find it here: CPP Template, once downloaded skip to Step 7, otherwise please continue: Lets now add some intellisense and OS specific settings, create a directory in your current workspace and name it
.vscode
, note the.
prepended the the folder name. Inside create a file namedc_cpp_properties.json
and copy the following contents inside.
{
"configurations": [
{
"name": "Mac",
"includePath": [
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1",
"/usr/local/include",
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/8.1.0/include",
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include",
"/usr/include",
"${workspaceRoot}"
],
"defines": [],
"intelliSenseMode": "clang-x64",
"browse": {
"path": [
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1",
"/usr/local/include",
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/8.1.0/include",
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include",
"/usr/include",
"${workspaceRoot}"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
}
},
{
"name": "Linux",
"includePath": ["/usr/include", "/usr/local/include", "${workspaceRoot}"],
"defines": [],
"intelliSenseMode": "clang-x64",
"browse": {
"path": ["/usr/include", "/usr/local/include", "${workspaceRoot}"],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
}
},
{
"name": "Win32",
"includePath": ["C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/include", "${workspaceRoot}"],
"defines": ["_DEBUG", "UNICODE"],
"intelliSenseMode": "msvc-x64",
"browse": {
"path": ["C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/include/*", "${workspaceRoot}"],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
}
}
],
"version": 2
}
-
Now you can begin creating C++ files and manually compiling them and then running them. If you would like this setup for your current workspace you can continue on...
-
Next we will create a task runner by pressing
CMD+Shift+P
on OSX orCTRL+Shift+P
on Windows, then typeconfigure task runner
. This will create atasks.json
file in a directory.vscode
in your current work space. It imports a default runner, select it all and delete it and paste the following code in.
{
"command": "g++", // change this name to something else if you'd like
"args": [
"-Wall",
"-o main.out",
"main.cpp", // change this to the name of the file you are choosing to compile and run, this is the only thing you will need to change
"-std=c++11", // Tag to compile as c++11
"&&",
"./main.out"
],
"isShellCommand": true,
"showOutput": "always",
"problemMatcher": {
"owner": "cpp",
"fileLocation": [
"relative",
"${workspaceRoot}"
],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
-
Now you can run this simple command by pressing
CMD+Shift+P or CTRL+Shift+P
type inRun Task
, then press enter and navigate to the task namedg++
or whatever you chose to name the command above. If you're like me and would prefer to just press a button instead of having to typeRun Task
continue on... -
Now lets bind a shortcut key to the build task, in the bottom left corner there is a cog icon, click this and choose
Keyboard Shortcut
. This will open up theKeyboard Shortcuts
tab inside that tab just under the search bar there it a line reading 'For advanced customizations open and edit keybindings.json' click on the keybings.json text, this will open another file, naturally calledkeybindings.json
this file is JSON object, and accepts an array of objects in JavaScrict Object Notation, very similar to JavaScript objects. You can highlight everything and delete it and paste the following, or if you already have some keybindings just add the object in the following code.
[
{
"key": "f8",
"command": "workbench.action.tasks.build"
}
]
Thats it, you can now begin coding in C++ and automate the build and run process by simply placing your cursor inside your C++ file and pressing f8 and view the output in the integrated terminal that pops up.
I will continue to update this as I find more elegant ways of automating this process. Ideally I would like to find a way to press one button and it will run the build and compile process without having to create a tasks.json in each workspace. For now the Github Repo template will be the easiest way to get automated build and run capabilities.
If you don't like dealing with unzipping files I highly recommend using the git
utility, to install it, follow below:
In order to have "code ." work in the command line you will first have to install the command path, to do so open VS Code and press CTRL+Shift+P or CMD+Shift+P
, type "Install 'code' command in PATH" then press enter, restart your terminal and now it should work
- Open a terminal and run the following:
sudo apt-get install git -y
git clone https://github.com/BaReinhard/CPP-VSCode-Template
cd CPP-VSCode-Template
code .
- Navigate to Git For Windows and download their git utility, or if you would like to download GitHub Desktop it will automatically install git to your commandline.
- Next open your command line and type the following
cd "TO/WHATEVER/FOLDER/LOCATION/YOU/WANT"
git clone https://github.com/BaReinhard/CPP-VSCode-Template
cd CPP-VSCode-Template
code .
Thanks for this. The various ${file} variables may be used to specify the current open file without having to modify the task.json file each time. Useful for compiling simple programs with a single keystroke.