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 docs.microsoft.com. 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.