Linux development with C++ in Visual Studio

Erika Sweet

Content outdated

For up-to-date documentation see Linux development with C++.

For an overview of the Visual Studio capabilities described in this article, see Develop C and C++ applications.

This post was updated on December 11, 2020

Visual Studio 2017 introduced the Linux Development with C++ workload. We generally recommend Visual Studio’s native support for CMake for all C++ Linux development. Visual Studio’s CMake support allows you to target multiple platforms (Windows, a remote Linux system, the Windows Subsystem for Linux, embedded targets…) with a single CMake project. You can learn more about CMake projects in our documentation and on our C++ Team Blog.

This post describes how to use Visual Studio’s MSBuild-based support to build and debug on a remote Linux system with Visual Studio 2019. If you are looking to target both Windows and Linux with the same project, our recommendation is to use CMake.

In this post, you will learn:

  • How to acquire the Linux Development with C++ workload,
  • How to create a MSBuild-based Linux C++ project,
  • How to establish your first connection to a Linux machine from Visual Studio,
  • How sources are managed between Visual Studio and Linux,
  • What capabilities the Linux project system provides, and
  • How to use Visual Studio diagnostic tools to find and resolve issues.

If you are new to using Visual Studio for C++ development, a great starting point is our guide to Getting Started with Visual Studio.

Install Workload for Linux development with C++

Visual Studio 2017 and 2019 include the C/C++ Linux Development workload. To install it, start the Visual Studio installer and choose to either install or modify an existing installation. Scroll to the bottom. Under the section “Other Toolsets” you will find Linux Development with C++. The workload installs in under 10 minutes. Learn more about installing the Linux development with C++ workload in our documentation.

Linux Workload

Opening projects

You will need a Linux machine, or you can use the Windows Subsystem for Linux. You can use any Linux distribution that has SSH, gdb, rsync, zip, and a compiler (clang or GCC) installed. In a Debian-based Linux environment, this is as easy as:

sudo apt update
sudo apt install -y build-essential gdb rsync zip openssh-server

Start SSH with the following command:

sudo service start ssh

Learn more about options for creating a Linux environment in our documentation.

To create a new MSBuild-based Linux Console Application in Visual Studio, filter the language to C++, the platform to Linux, and select “Console Application”.

Image Linux NPD

This project will open a readme with some instructions about its capabilities. We will first select the project’s platform toolset. Right-click on the project in the Solution Explorer and select Properties.

Visual Studio's C++ Property Pages. The option Platform Toolset is selected from the General page.There are four platform toolsets to choose from: GCC for Remote Linux, Clang for Remote Linux, GCC for Windows Subsystem for Linux, and Clang for Windows Subsystem for Linux. Select the toolset that matches your compilers and Linux environment and press OK. The “Remote Linux” toolsets require a SSH connection in the Connection Manager. The “Windows Subsystem for Linux” toolsets use Visual Studio’s native support for WSL and do not require SSH. In this tutorial, I’ll use the GCC for Remote Linux toolset. Check out this tutorial for more information on our native support for WSL.

Now we can add a new remote connection to the Connection Manager. Select Tools > Options > Cross Platform > Connection Manager to open the Connection Manager. You can add new connections with either password or private key authentication. Learn more about connecting to a remote Linux system in our documentation.

The Visual Studio Connection Manager is open.

After you enter your information, Visual Studio manages the connection to your Linux system where builds are performed.  If there are any problems, the build output points you directly to issues found in your code.

The project system synchronizes your sources between Windows and Linux, and provides you with extensive control to manage this yourself if you need it. Right click on the project in Solution Explorer and choose Properties.

  • The General property page allows you to set options like what folders to use on the remote system and what the Configuration Type is for your output (an executable, static or dynamic library).
  • The Debugging page provides additional control over execution; for example, you can run additional commands before launching a program such as exporting the display to debug desktop apps.
  • The VC++ Directories page provides options for controlling IntelliSense by providing additional directories to include or exclude.
  • The Copy Sources property page allows you to specify whether to copy sources to the remote Linux system. You may not want to copy sources if you are working with a share or are managing your own synchronization through other means.
  • The C/C++ and Linker property page groups provide many options for controlling what flags are passed to the compiler. They also enable you to override the default setting to use g++ or clang++ and specify your own.
  • Finally, the Build Events property page group provides the capability to run additional commands locally or remotely as part of the build process.

Learn more about configuring a MSBuild-based Linux project in our documentation.

C++ Property Pages. The "All Options" page of the Linker section is selected.

Use the full power of Visual Studio productivity features with your Linux C++ code

IntelliSense is provided out of the box for GCC, Clang, and libstdc++. Visual Studio automatically copies the headers referenced by your project from your Linux system to Windows to provide IntelliSense. Once you get going with your own code you can really see Visual Studio’s productivity features in action.

Member List and Quick Info, shown in the screenshot below, are just two examples of the powerful IntelliSense features that make writing code easier and faster. Member List shows you a list of valid members from a type or namespace. Typing in “->” following an object instance in the C++ code will display a list of members, and you can insert the selected member into your code by pressing TAB or by typing a space or a period. Quick Info displays the complete declaration for any identifier in your code. In the screenshot below, Visual Studio is showing the list of accessible members of the SimpleServer object and the declaration of the open method.

Linux Code Editing

Refactoring, autocomplete, squiggles, reference highlighting, syntax colorization, and code snippets are some of the other useful productivity features that are helpful when you are writing and editing your code.

Navigating in large codebases and jumping between multiple code files can be a tiring task. Visual Studio offers many great code navigation features, including Go To Definition, Go To Line/Symbols/Members/Types, Find All References, View Call Hierarchy, Object Browser, and many more.

The Peek Definition feature, as shown in the screenshot below, shows the definition inline without switching away from the code that you’re currently editing. You can find Peek Definition by placing the insertion point on a method that you want to explore and then right-clicking or pressing Alt+F12. In the screenshot below, the definition of the OpenCV face detection detectMultiScale method, in objdetect.hpp, is shown in an embedded window in the current .cpp file.

Linux Peek Definition

Our Linux support also integrates native Linux tooling like AddressSanitizer. Learn more about enabling ASAN in our documentation.

Debugging and diagnosing issues

Visual Studio excels at helping you solve your development problems, and you can now use those capabilities with your C++ code on Linux. You can set breakpoints in your C++ code and press F5 to launch the debugger and run your code on your Linux machine. When a breakpoint is hit, you can watch the value of variables and complex expressions in the Autos and Watch tool windows as well as in the data tips on mouse hovering, view the call stack in the Call Stack window, and step in and step out of your code easily. You can also use conditions in your breakpoints to narrow in on specific problems. Likewise, you can set actions to record variable values to the output window. You can also inspect application threads or view disassembly.

If you need to interact with your programs on Linux you can use the Linux Console window from within Visual Studio. To activate this window, use the menu Debug > Linux Console. In the screenshot below you can see input being provided to the scanf call on line 24.

Linux Console Window

You can even attach to processes on your Linux machines to debug problems live. Open the Debug menu and select Attach to Process. As shown in the screenshot below select the SSH Connection type. You can then use the drop-down target menu to select a Linux machine. This will then enumerate the remote connections you have previously created.

Linux Attach to Process

See our debugging and diagnostics with C++ page to learn more about our general capabilities.

Get started with Visual Studio Linux C/C++ Development today

Learn more about cross-platform CMake projects and MSBuild-based Linux projects in our documentation. Please provide feedback from within Visual Studio by selecting the “Send Feedback” icon in the top right-hand corner of the IDE.