Linux development with C++ in Visual Studio



If you are new to using Visual Studio for C++ development a great starting point is our quick guide to Getting Started with Visual Studio. In this blog post we will dive into the Linux Development with C++ workload. You will learn

  • how to acquire this as part of installing Visual Studio 2017,
  • how to create a 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.

Install Workload for Linux development with C++

Visual Studio 2017 introduces 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.

Linux Workload

Opening projects

You will need a Linux machine, of course, or you can use the Windows Subsystem for Linux with Visual Studio. You can use any Linux distribution that has SSH, gdbserver, and a compiler installed. In your Linux environment, this is as easy as:

sudo apt install -y build-essential gdbserver

To create a new Linux Console Application in Visual Studio, select that project type under New Project > Visual C++ > Cross Platform > Linux.

Linux Projects

This project will open a readme with some instructions about its capabilities. There is also a main.cpp file which outputs text to the console. Go ahead and compile it using the menu Build > Build Solution. Since this is your first Linux project you will be prompted by the connection manager dialog to add a connection. You can add new connections with either password or private key authentication.

Linux Connection Manager

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++ 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.

Linux Project Properties

Of course, you probably already have some existing sources, and probably a build system as well. If that is the case, our makefile project is what you want. You can create one from the New Project dialog, then import your sources, and then specify the build commands to use on the remote Linux system. If your project is particularly large, you may find it easier to auto-generate the project files instead of configuring everything manually with the property pages. You can find some example scripts on GitHub that show you how to generate Linux makefile projects for Visual Studio based on an existing source base. These examples should be easy to modify for your own requirements.

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

IntelliSense is provided out of the box for GCC and libstdc++. It is easy to configure your project to use headers from your own Linux system to enable IntelliSense for everything you need. 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 example in the screenshot, Visual Studio is showing the list of accessible members of the SimpleServer object and the declaration of the open method, making writing code a lot easier.

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, to boost your productivity.

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, making reading and writing OpenCV code more efficient.

Linux Peek Definition

You can learn much more about editing and navigating C++ code in Visual Studio here.

Debugging and diagnosing issues

Visual Studio excels at helping you solve your development problems, and now you can use those capabilities with your C++ code on Linux. You can set breakpoints in your C++ code and press F5 to launch the debugger, which will 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. You can also set actions, for example 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.

Working with others

Developing an application usually involves working with others. When it comes to source code storing and sharing and cloud build, Visual Studio Team Services have you covered. Collaborating with other team members is as easy as signing up for a free Visual Studio Team Services account, checking in your source code, and specifying who has access to it. Now everyone on your team can check out source code, edit it, and check it back in.

Visual Studio Team Services also simplifies continuous integrations for your code. Create and manage build processes that automatically compile and test your games in the cloud. Wondering if a bug was fixed in this build? By associating work items with code, the work items are listed in the build summary along with code changes and test results.

We have more information available about developing C++ applications in a team.

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

Learn in depth how to use the Visual Studio for C/C++ Linux Development in our announcement post and on Please provide feedback from within Visual Studio by selecting Help > Send Feedback. If you have any questions you can send us email at VC++ Linux Support.


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

  • 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.

  • 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!