Simple Solutions: Coding C and C++ with Visual Studio Code

C++ developers tend to be partial to their editors and might consider Visual Studio Code (VSCode) as a lightweight tool not worthy of their attention. Having said that, they could be wrong! More importantly, people looking to learn C or C++ from ground zero might find many editors to be so overloaded with features that it distracts from learning the core language. Although a full-blown version of Visual Studio, such as Visual Studio Community Edition, can be great tool for doing C and C++ coding, there are many times when it can be overkill. With the addition of the C/C++ extension to Visual Studio Code, you might have what is needed in a small, cross-platform editor.

Visual Studio Code is a lightweight editor for coding on Windows, Linux, or the MacOS. More specifically, it targets Windows 7 or later, Debian, Ubuntu, Red Hat, Fedora, SUSE, and Mac OS 10.9 or later. Back in 2015, Codeguru did a slideshow, “Top 10 Visual Studio Code How-To-Stories” that presented some of the key features of Visual Studio Code. The slideshow indicated some of the things that developers wanted, ranging from a lightweight editor to cross-platform support. In addition to being cross platform, Visual Studio Code also can support a number of programming languages, including JavaScript, Java, PHP, Python, Go, C#, and, as indicated by this article, C and C++.

Even though support for JavaScript, HTML, and a few other languages are included with the basic download, others require that you add an extension. There is an extension for Visual Code for C/C++ that can be installed from the Visual Studio Marketplace.

The extension includes support for code formatting, auto-completion, debugging, and a lot more. You can get many of the details either from the marketplace or from the Microsoft C/C++ extension for VSCode documentation page.

Figure 1 presents what is seen at this URL. As you can see in the figure, a simple click of Install should start the installation of the C/C++ extension.

The Marketplace listing for the C++ Extension
Figure 1: The Marketplace listing for the C++ Extension

The install process likely will open your copy of Visual Studio Code, as shown in Figure 2. You might then need to click another Install button, also shown in Figure 2.

Installing the C++ Extension in Visual Studio Code
Figure 2: Installing the C++ Extension in Visual Studio Code

Once you’ve added this extension, you’ll be able to enter C/C++ programs in VSCode. You’ll also see that the editor supports many of the features mentioned earlier. Figure 3 shows the Visual Studio Code with a code listing included.

Colored C++ Code in VS Code
Figure 3: Colored C++ Code in VS Code

Other C++ Extensions for VSCode

In addition to the basic C/C++ coding extension for Visual Studio Code, there are several other C++ extensions that can be used with the editor. These include:

  • C/C++ Clang Command Adapter: Includes completion and diagnostic for C, C++, and Objective-C using Clang Commands
  • C/C++ Snips: Support for C/C++ code snippets
  • C++ IntelliSense: IntelliSense with the help of GNU Global tags for C/C++
  • C++ Algorithm Mnemonics: C++ Algorithm Mnemonics as code snippets for VSCode
  • C++ Advanced Lint: Analysis extensions for C/C++ that support a number of back-end analyzing programs
  • CrossBuild: A build system for embedded development

You can find extensions that can be added by clicking the extension’s icon on the left and then entering C++ in the search box. This will list the extensions, similar to what is shown in Figure 4.

C++-related extensions for Visual Studio Code
Figure 4: C++-related extensions for Visual Studio Code

Clicking the extension name will present additional information on the right side of the editor window. The description also should include a link to install, reload, or disable the extension.

Compiling C++ in Visual Studio Code

Visual Studio Code is primarily an editor that can be extended. As such, even though it easily can be used to run JavaScript or display HTML, it is a bit more difficult to run code that requires a build process. To execute your C++ code, you need to compile the source code you enter into an executable file containing native code. Native code is dependent upon your specific operating system. This means that, although you can use VSCode on a variety of platforms, you’ll need to find a compiler that works on the operating system you are targeting. Compilers such as GCC are often the choice used with VSCode. Installing GCC for use with VSCode is a topic for a future article.

Conclusion

Even though Microsoft offers Visual Studio Community at no cost, there are times when that is overkill or more than is needed. Visual Studio Code offers an open source, cross-platform editor that has a relatively small footprint. With the addition of extensions, VSCode is a great tool for quickly accessing and editing C++ code, whether you are a beginner or an advanced developer. With the addition of an open source compiler that is native to your platform, you can use VSCode as a complete tool for building your C++ solutions.

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read