Linux development with C++ in Visual Studio

Erika

Erika

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.

7 comments

Comments are closed. Login to edit/delete your existing comments

  • Avatar
    tim brown

    Please can you link to a non-trivial remote debug example with linking to e.g. pthread etc. Thanks ,

  • Jędrzej Dudkiewicz
    Jędrzej Dudkiewicz

    Hi, is it possible to cross-compile on one machine (in my case Debian/x86_64) and debug on different machine (Debian/armhf)? I cross-compile on rather powerful VM and debug resulting binary on Beagle Bone Black board. It isn’t possible to run on QEmu since it lacks physical outputs required for my project, and compiling on BBone is out of question, as it lacks resources – compilation takes half an hour, which is unacceptable.

  • Avatar
    吴 小

    HI,How can I compile cuda program(.cu file)in vs2015 for linux ?
    I use this cross-compile tool, but I can’t add nvcc to compile .cu file.
    It work well in vscode for ubuntu and vs2015 for win10.
    thanks!

  • Jianjun Fang
    Jianjun Fang

    Hi, I would like to configure a build step with the Linux C++ Dev workload in Visual Studio to run a command on the remote Linux server after the soruce code is copied to the remote server but before the compile starts.
    What I did was to put this additional build step as the Remote Pre-Build event under the Build Events. The comand was indeed run on the remote Linux server but before the source code was copied to the remote server. Therefore, this step failed
    My second attempt was to put this step as the Custom Build Step and I configured it to be executed after “CopyFileToFolders” but before “ClCompile”. I could tell that the command was indeed run between the source code was copied to the remote server and the compiling started. However, the command was run in the local Windows machine, not on the remote Linux server. Therfore, this attempt failed as well.
    Any suggestion for me to accomplish my task? thanks

  • Avatar
    ROMANO Giovanni

    Hello, I am trying it in a complex scenario…it works but suddenly using step by step, VS asks for raise.c to be located in order to performthe step. Where should I find it?!?

  • ngougni carelle stella
    ngougni carelle stella

    Hello, i’m trying to creat a program for the raspberry pi using Visual studio under windows.
    I have followed the steps you indicaded but when i try to buid the consoleApplication i recieve the following errors and the program is not executed

    Error 1 : the source could not be accessed / opened
    Error 2 : “printf” ist not defined
    I know the second error is due to the first one.
    My question is : what can i do to be able to access/ open the libraires when i’m building my programm.

    Thanks in advance

  • Avatar
    Ovidiu Craciun

    I have Microsoft Visual Studio Professional 2019 Version 16.4.2
    I want to build and debug a c++ project for Linux. My whole setup used to work great, I was able to build my project (c++) and debug it as well.
    One day it stopped working (I am not 100% sure if it was before or after I installed latest VS update).

    Problem: When I start debugging the VS builds the project and hangs forever with the dialog showing the message “Initializing debugger”.

    I have my bash setup correctly, ssh server started and tested (VS has a connection opened and tested to it), the MS cmake 3.16.3311152-MSVC_2, build-essential, gdb, zip, rsync, gcc.

    The “Debug->Linux Console” window is empty, not showing anything.
    On the output window there’s no error or warning printed/shown and no other popup either.

    How can I investigate, understand what is going on? I can provide any detail needed to move on with this matter which is blocking my progress.

    Thank you!