Setting up Visual Studio Code¶
Visual Studio Code (VS Code) is a source-code editor that can be extended with plugins for different programming languages and development tools. In this course, you can use it to write, compile, run and debug C programs.
This page explains how to set up a basic C programming environment in VS Code, mainly for compiling and running programs from the command line.
After installing VS Code, you still need to:
- Install the C/C++ extension.
- If you use Windows, install a C compiler and debugger.
- If you want to run and debug programs directly from the VS Code user interface, see Running and debugging programs from VS Code UI.
Install VS Code and the C/C++ extension¶
Download and install Visual Studio Code.
Install the C/C++ extension by Microsoft. This extension adds support for C and C++, including code completion, error checking and integration with debuggers.
Extensions can be installed directly from VS Code:
- Open the Extensions view from the left-hand toolbar, or press
Ctrl+Shift+X. - Search for C/C++.
- Select the extension published by Microsoft and press Install.
You can find more information in the official VS Code instructions for installing extensions.
Note: The C/C++ extension does not include a C compiler. VS Code uses a compiler and debugger installed separately on your computer.
Useful optional extensions¶
The following extensions are not required, but can make working with C code more convenient:
Code Runner: lets you quickly run the currently open source file from VS Code.
Error Lens: displays compiler and editor diagnostics directly next to the corresponding lines of code.
Hex Editor: allows you to inspect files in hexadecimal form, which can be useful when studying binary data and executable files.
Note: Code Runner is convenient for small exercises, but it does not replace a proper build and debug configuration for larger projects.
Installing a C compiler in Windows¶
In Windows, you need to install a C compiler and debugger separately.
Follow the official VS Code instructions in Installing the MinGW-w64 toolchain.
The instructions install MinGW-w64 through MSYS2 and include
gcc, g++ and gdb. They also explain how to add the compiler tools to the Windows PATH.The tutorial is written for C++, but the installation instructions are also valid for this course. When programming in this course, use
gcc to compile your .c files.After the installation, you can check that the compiler and debugger are available by opening a new terminal in VS Code. Select Terminal > New Terminal from the top menu, or use the shortcut
Ctrl+`. The terminal opens at the bottom of the VS Code window.Then run:
gcc --version
gdb --version
Other ways of installing C compilers, including options for Linux and macOS, can be found in Other development environments.
Running and debugging programs from VS Code UI¶
This page only explains how to set up the basic C programming environment and how to use it mainly from the command line.
VS Code can also be configured to compile, run and debug programs directly from its user interface. For more information, see the debugging section in Lovelace or the official VS Code tutorial for running a program with MinGW.
Module 2¶
For programming the embedded device in Module 2, you will install additional VS Code extensions. The required extensions and setup will be introduced later in the laboratory exercise.