Skip to content

Instantly share code, notes, and snippets.

@BaReinhard
Last active March 7, 2024 12:26
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save BaReinhard/c8dc7feb8b0882d13a0cac9ab0319547 to your computer and use it in GitHub Desktop.
Save BaReinhard/c8dc7feb8b0882d13a0cac9ab0319547 to your computer and use it in GitHub Desktop.
A Basic Setup for using VS Code with C++ 11 for CSIT Data Structures

Setting Up VS Code for C++ 11

This is for students who are running on a Non-Microsoft OS, or students who would rather use a lighter weight IDE

  1. First download VS Code and install it.

  2. Install a C++ extension in VS Code VS Code Extensions The extension button is the square logo on the left side bar

  3. 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 named c_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
}
  1. 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...

  2. Next we will create a task runner by pressing CMD+Shift+P on OSX or CTRL+Shift+P on Windows, then type configure task runner. This will create a tasks.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
            }
  }
  1. Now you can run this simple command by pressing CMD+Shift+P or CTRL+Shift+P type in Run Task, then press enter and navigate to the task named g++ 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 type Run Task continue on...

  2. 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 KeyBoard ShortCuts. This will open up the Keyboard 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 called keybindings.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

Linux/Ubuntu

  • 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 .

Windows / OSX

  • 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 .
@Mvrs
Copy link

Mvrs commented Dec 6, 2017

"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

What if I want to compile multiple files in one directory, how should I do this? Without enter the files manual like...
"main.cpp",
"cat.cpp",
"test.cpp"

@inventorinventor351
Copy link

inventorinventor351 commented Apr 7, 2018

I can't seem to get pass the tasks.json part without getting strange errors. As well as, I don't know where to paste what you provide. Here is what it is by default:
{ // See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.json format "version": "2.0.0", "tasks": [ { "label": "build", "type": "shell", "command": "msbuild", "args": [ // Ask msbuild to generate full paths for file names. "/property:GenerateFullPaths=true", "/t:build" ], "group": "build", "presentation": { // Reveal the output only if unrecognized errors occur. "reveal": "silent" }, // Use the standard MS compiler pattern to detect errors, warnings and infos "problemMatcher": "$msCompile" } ] }
I get these errors when I replace from the command line to the problemMatcher line with what you provide:
screenshot 8
Could it be this guide is outdated? I hope not because I'm still having trouble getting c++ set up for vs code!

@inventorinventor351
Copy link

Never mind, it turns out you're missing a parathesis that is supposed to close up the problem matcher. However, that still leaves me with those warnings.

@sblinkhorn
Copy link

sblinkhorn commented Aug 4, 2018

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.

{
"args": [
    "-Wall",
    "-o${fileBasenameNoExtension}.out",
    "${relativeFile}",
    "-std=c++1z",
    "&&",
    "./${fileBasenameNoExtension}.out"
]
}

@gbowne1
Copy link

gbowne1 commented Nov 1, 2021

Currently, in tasks.json file example,

     "showOutput": "always",```

are now deprecated.

@Vadbeg
Copy link

Vadbeg commented Aug 1, 2022

@Mvrs

You can modify your tasks.json to build multiple C++ files by using an argument like "${workspaceFolder}/*.cpp" instead of ${file}

From here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment