Getting started with vscode

Download vscode eBook

Remarks

This section provides an overview of what vscode is, and why a developer might want to use it.

It should also mention any large subjects within vscode, and link out to the related topics. Since the Documentation for vscode is new, you may need to create initial versions of those related topics.

Versions

VersionRelease date
0.10.1-extensionbuilders2015-11-13
0.10.12015-11-17
0.10.22015-11-24
0.10.32015-11-26
0.10.52015-12-17
0.10.62015-12-19
0.10.7-insiders2016-01-29
0.10.82016-02-05
0.10.8-insiders2016-02-08
0.10.92016-02-17
0.10.10-insiders2016-02-26
0.10.102016-03-11
0.10.112016-03-11
0.10.11-insiders2016-03-11
0.10.12-insiders2016-03-20
0.10.13-insiders2016-03-29
0.10.14-insiders2016-04-04
0.10.15-insiders2016-04-11
1.0.02016-04-14
1.1.0-insider2016-05-02
1.1.02016-05-09
1.1.12016-05-16
1.2.02016-06-01
1.2.12016-06-14
1.3.02016-07-07
1.3.12016-07-12
1.4.02016-08-03
translation/20160817.012016-08-17
translation/20160826.012016-08-26
translation/20160902.012016-09-02
1.5.02016-09-08
1.5.12016-09-08
1.5.22016-09-14
1.6.02016-10-10
1.6.12016-10-13
translation/20161014.012016-10-14
translation/20161028.012016-10-28
1.7.02016-11-01
1.7.12016-11-03
translation/20161111.012016-11-12
translation/20161118.012016-11-19
1.7.22016-11-22
translation/20161125.012016-11-26
translation/20161209.012016-12-09
1.8.02016-12-14
1.8.12016-12-20
translation/20170123.012017-01-23
translation/20172701.012017-01-27
1.9.02017-02-02
translation/20170127.012017-02-03
translation/20170203.012017-02-03
1.9.12017-02-09
translation/20170217.012017-02-18
translation/20170227.012017-02-27
1.10.02017-03-01
1.10.12017-03-02
1.10.22017-03-08
translation/20170311.012017-03-11
translation/20170317.012017-03-18
translation/20170324.012017-03-25
translation/20170331.012017-03-31
1.11.02017-04-06
1.11.12017-04-06
translation/20170407.012017-04-07
1.11.22017-04-13

First program (C++): Hello World.cpp

This example introduces you to the basic functionality of VS Code by demonstrating how to write a "hello world" program in C++. Before continuing, make sure you have the "ms-vscode.cpptools" extension installed.

Initialize the Project

The first step is to create a new project. To do this, load the VS Code program. You should be greeted with the typical welcome screen: enter image description here

To create the first program, select "Start" > "New file" from the welcome screen. This will open a new file window. Go ahead and save the file ("File" > "Save") into a new directory. You can name the directory anything you want, but this example will call the directory "VSC_HelloWorld" and the file "HelloWorld.cpp".

Now write the actual program (feel free to copy the below text):

#include <iostream>

int main()
{
    // Output the hello world text
    std::cout << "Hello world!" << std::endl;
    return 0;
}
 

Great! You'll also notice that because you've installed the "ms-vscode.cpptools" extension you also have pretty code-highlighting. Now let's move on to running the code.

Running the Script (basic)

We can run "HelloWorld.cpp" from within VS Code itself. The simplest way to run such a program is to open the integrated terminal ("View" > "Integrated Terminal"). This opens a terminal window in the lower portion of the view. From inside this terminal we can navigate to our created directory, build, and execute the script we've written. enter image description here Here we've used the following commands to compile and run the code:

$ g++ HelloWorld.cpp -o hellowold
$ ./hellowold
 

Notice that we get the expected Hello World! output.

Running the Script (slightly more advanced)

Great, but we can use VS Code directly to build and execute the code as well. For this, we first need to turn the "VSC_HelloWorld" directory into a workspace. This can be done by:

  1. Opening the Explorer menu (top most item on the vertical menu on the far left)
  2. Select the Open Folder button
  3. Select the "VSC_HelloWorld" directory we've been working in. enter image description here Note: If you open a directory within VS Code (using "File" > "Open..." for example) you will already be in a workspace.

The Explorer menu now displays the contents of the directory.

Next we want to define the actual tasks which we want VS Code to run. To do this, select "Tasks" > "Configure Default Build Task". In the drop down menu, select "Other". This opens a new file called "tasks.json" which contains some default values for a task. We need to change these values. Update this file to contain the following and save it:

{
    "version": "2.0.0",
    "tasks": [
        {
            "taskName": "build",
            "type": "shell",
            "command": "g++ HelloWorld.cpp -o helloworld"
        },
        {
            "taskName": "run",
            "type": "shell",
            "command": "${workspaceRoot}/helloworld"
        }
    ]
}
 

Note that the above also creates a hidden .vscode directory within our working directory. This is where VS Code puts configuration files including any project specific settings files. You can find out more about Tasks here.

In the above example, ${workspaceRoot} references the top level directory of our workspace, which is our "VSC_HelloWorld" directory. Now, to build the project from inside the method select "Tasks" > "Run Build Task..." and select our created "build" task and "Continue without scanning the task output" from the drop down menus that show up. Then we can run the executable using "Tasks" > "Run Task..." and selecting the "run" task we created. If you have the integrated terminal open, you'll notice that the "Hello World!" text will be printed there.

It is possible that the terminal may close before you are able to view the output. If this happens you can insert a line of code like this int i; std::cin >> i; just before the return statement at the end of the main() function. You can then end the script by typing any number and pressing <Enter>.

And that's it! You can now start writing and running your C++ scripts from within VS Code.

First Steps (C++): HelloWorld.cpp

The first program one typically writes in any language is the "hello world" script. This example demonstrates how to write this program and debug it using Visual Studio Code (I'll refer to Visual Studio Code as VS Code from now on).

Create The Project

Step 1 will be to create a new project. This can be done in a number of ways. The first way is directly from the user interface.

  1. Open VS Code program. You will be greeted with the standard welcome screen (note the images are taken while working on a Mac, but they should be similar to your installation): VS Code welcome screen
  1. From the Start menu, select New file. This will open a new editing window where we can begin constructing our script. Go ahead and save this file (you can use the menu File > Save to do this). For this example we will call the file HelloWorld.cpp and place it in a new directory which we will call VSC_HelloWorld/.

  2. Write the program. This should be fairly straight forward, but feel free to copy the following into the file:

#include <iostream>

int main() {
   std::cout << "Hello world!" << std::endl;
   return 0;
}
 

Run the Code

Next we want to run the script and check its output. There are a number of ways to do this. The simplest is to open a terminal, and navigate to the directory that we created. You can now compile the script and run it with gcc by typing:

$ g++ HelloWorld.cpp -o helloworld
$ ./helloworld
Hello World!
 

Yay, the program worked! But this isn't really what we want. It would be much better if we could run the program from within VSCode itself. We're in luck though! VSCode has a built in terminal which we can access via the menu "View" > "Integrated Terminal". This will open a terminal in the bottom half of the window from which you can navigate to the VSC_HelloWorld directory and run the above commands.

Typically we do this by executing a Run Task. From the menu select "Tasks" > "Run Task...". You'll notice you get a small popup near the top of the window with an error message (something along the lines of

Installation or Setup

On Windows

By default, VS Code is installed under C:\Program Files (x86)\Microsoft VS Code for a 64-bit machine.

Note: .NET Framework 4.5.2 is required for VS Code. If you are using Windows 7, please make sure .NET Framework 4.5.2 is installed.

Tip: Setup will optionally add Visual Studio Code to your %PATH%, so from the console you can type 'code .' to open VS Code on that folder. You will need to restart your console after the installation for the change to the %PATH% environmental variable to take effect.

On Mac

  • Download Visual Studio Code for Mac.
  • Double-click on the downloaded archive to expand the contents.
  • Drag Visual Studio Code.app to the Applications folder, making it available in the Launchpad.
  • Add VS Code to your Dock by right-clicking on the icon and choosing Options, Keep in Dock.

You can also run VS Code from the terminal by typing 'code' after adding it to the path:

  • Launch VS Code.
  • Open the Command Palette (Ctrl+Shift+P) and type 'shell command' to find the Shell Command: Install 'code' command in PATH command.

Restart the terminal for the new $PATH value to take effect. You'll be able to type 'code .' in any folder to start editing files in that folder.

Note: If you still have the old code alias in your .bash_profile (or equivalent) from an early VS Code version, remove it and replace it by executing the Shell Command: Install 'code' command in PATH command.

On Linux

Debian and Ubuntu based distributions

The easiest way to install for Debian/Ubuntu based distributions is to download and install the .deb package (64-bit) either through the graphical software center if it's available or through the command line with:

sudo dpkg -i <file>.deb
sudo apt-get install -f # Install dependencies
 

Installing the .deb package will automatically install the apt repository and signing key to enable auto-updating using the regular system mechanism. Note that 32-bit and .tar.gz binaries are also available on the download page.

The repository and key can also be installed manually with the following script:

curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.gpg
sudo mv microsoft.gpg /etc/apt/trusted.gpg.d/microsoft.gpg
sudo sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/vscode stable main" > /etc/apt/sources.list.d/vscode.list'
 

Then update the package cache and install the package using:

sudo apt-get update
sudo apt-get install code # or code-insiders for insiders build
 

RHEL, Fedora and CentOS based distributions

We currently ship the stable 64-bit VS Code in a yum repository, the following script will install the key and repository:

sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc
sudo sh -c 'echo -e "[code]\nname=Visual Studio Code\nbaseurl=https://packages.microsoft.com/yumrepos/vscode\nenabled=1\ngpgcheck=1\ngpgkey=https://packages.microsoft.com/keys/microsoft.asc" > /etc/yum.repos.d/vscode.repo'
 

Then update the package cache and install the package using dnf (Fedora 22 and above):

dnf check-update
sudo dnf install code
 

Or on older versions using yum:

yum check-update
sudo yum install code
 

openSUSE and SLE based distributions

The yum repository above also works for openSUSE and SLE based systems, the following script will install the key and repository:

sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc
sudo sh -c 'echo -e "[code]\nname=Visual Studio Code\nbaseurl=https://packages.microsoft.com/yumrepos/vscode\nenabled=1\ntype=rpm-md\ngpgcheck=1\ngpgkey=https://packages.microsoft.com/keys/microsoft.asc" > /etc/zypp/repos.d/vscode.repo'
 

Then update the package cache and install the package using:

sudo zypper refresh
sudo zypper install code
 

AUR package for Arch Linux

There is a community maintained Arch User Repository (AUR) package for VS Code.

Installing .rpm package manually The .rpm package (64-bit) can also be manually downloaded and installed, however auto-updating won't work unless the repository above is installed. Once downloaded it can be installed using your package manager, for example with dnf:

sudo dnf install <file>.rpm
 

Note that 32-bit and .tar.gz binaries are are also available on the download page.

Stats

99 Contributors: 6
Friday, August 4, 2017
Licensed under: CC-BY-SA

Not affiliated with Stack Overflow
Rip Tutorial: info@zzzprojects.com

Download eBook