{"id":24241,"date":"2019-05-06T16:08:35","date_gmt":"2019-05-06T16:08:35","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/cppblog\/?p=24241"},"modified":"2022-08-23T18:56:36","modified_gmt":"2022-08-23T18:56:36","slug":"c-with-visual-studio-2019-and-windows-subsystem-for-linux-wsl","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cppblog\/c-with-visual-studio-2019-and-windows-subsystem-for-linux-wsl\/","title":{"rendered":"C++ with Visual Studio 2019 and Windows Subsystem for Linux (WSL)"},"content":{"rendered":"<p><div class=\"alert alert-info\"><p class=\"alert-divider\"><i class=\"fabric-icon fabric-icon--Info\"><\/i><strong>Content outdated<\/strong><\/p> For up-to-date documentation see <a class=\"Hyperlink SCXW204925438 BCX8\" href=\"https:\/\/docs.microsoft.com\/en-us\/cpp\/build\/walkthrough-build-debug-wsl2?view=msvc-170\" target=\"_blank\" rel=\"noreferrer noopener\"><span class=\"TextRun Underlined SCXW204925438 BCX8\" lang=\"EN-GB\" xml:lang=\"EN-GB\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW204925438 BCX8\" data-ccp-charstyle=\"Hyperlink\">Walkthrough: Build and Debug C++ with Microsoft Windows Subsystem for Linux 2 (WSL 2) and Visual Studio 2022<\/span><\/span><\/a>.<\/p>\n<p>For an overview of the Visual Studio capabilities described in this article, see <a href=\"https:\/\/visualstudio.microsoft.com\/vs\/features\/cplusplus\/\">Develop C and C++ applications<\/a>.<\/div>This post was updated on December 11, 2020<\/p>\n<p><a href=\"https:\/\/visualstudio.microsoft.com\/downloads\/\">Visual Studio 2019 version 16.1<\/a> added native support for using C++ with the <a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/wsl\/about\">Windows Subsystem for Linux (WSL)<\/a>. WSL lets you run a lightweight Linux environment directly on Windows, including most command-line tools, utilities, and applications. In Visual Studio you no longer need to <a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/targeting-windows-subsystem-for-linux-from-visual-studio\/\">add a remote connection or configure SSH<\/a>\u00a0in order to build and debug on your local WSL installation. This will save you time getting up and running in a Linux environment and eliminates the need to copy and maintain sources on a remote machine.\u00a0In order to use our native support for WSL you will need to install the <a href=\"https:\/\/docs.microsoft.com\/en-us\/cpp\/linux\/download-install-and-setup-the-linux-development-workload?view=vs-2019\">Linux Development with C++<\/a> workload in Visual Studio.<\/p>\n<p>In this blog post, we\u2019ll first look at how to set up WSL. We will then walk-through how to use it with a <a href=\"https:\/\/docs.microsoft.com\/en-us\/cpp\/linux\/cmake-linux-project?view=vs-2019\">CMake project<\/a> and a <a href=\"https:\/\/docs.microsoft.com\/en-us\/cpp\/linux\/create-a-new-linux-project?view=msvc-160\">MSBuild-based Linux project<\/a>. We recommend using CMake for all C++ cross-platform development. If you are just getting started with our native support for CMake, be sure to check out our <a href=\"https:\/\/docs.microsoft.com\/en-us\/cpp\/build\/cmake-projects-in-visual-studio?view=vs-2019\">CMake Support in Visual Studio<\/a> introductory page too.<\/p>\n<h3>WSL1 versus WSL2<\/h3>\n<p>Our native support for WSL works best with WSL1. Our support executes all commands locally through wsl.exe and relies on Windows drives mounted under the \/mnt folder (e.g. \/mnt\/c\/Users\u2026) to access local source files from WSL. These commands are slower with WSL2 because performance across the Windows and Linux operating systems is faster in WSL1 than WSL2. Learn more about the differences between WSL1 and WSL2 <a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/wsl\/compare-versions#exceptions-for-using-wsl-1-rather-than-wsl-2\">here<\/a>.<\/p>\n<p>To avoid these slowdowns, you can install and run SSH on your local WSL2 installation and <a href=\"https:\/\/docs.microsoft.com\/en-us\/cpp\/linux\/connect-to-your-remote-linux-computer?view=msvc-160&amp;viewFallbackFrom=vs-2019\">add a new SSH connection<\/a> to WSL2 as if it were a remote machine. Stay tuned to the <a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/\">C++ Team Blog<\/a> for more information on our WSL2 support.<\/p>\n<h3>Setting up WSL<\/h3>\n<p>You can find details on <a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/wsl\/install-win10\">how to install WSL<\/a> here, but the easiest way is to download your distro of choice (Ubuntu, Debian, etc.) from the <a href=\"https:\/\/www.microsoft.com\/en-us\/p\/ubuntu\/9nblggh4msv6?activetab=pivot:overviewtab\">Microsoft Store<\/a>.<\/p>\n<p>To configure your WSL installation to work with Visual Studio you need the following tools installed: a compiler (GCC or Clang), gdb, rsync, and zip. If you are using CMake, then you will also need to install an underlying build system (Ninja or Unix Makefiles). You can install them on distros that use apt with the following commands:<\/p>\n<pre class=\"prettyprint\">sudo apt update\r\nsudo apt install g++ gdb make ninja-build rsync zip<\/pre>\n<p>In this tutorial, I&#8217;ll use GCC and Unix Makefiles.<\/p>\n<p>The inclusion of rsync and zip allows Visual Studio to extract header files from your WSL instance to the Windows filesystem to use for IntelliSense. Due to the lack of visibility into the root file system of WSL from Windows, a local rsync copy is done inside WSL to copy the headers to a Windows visible location. This is a one-time operation that Visual Studio performs to <a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/intellisense-for-remote-linux-headers\/\">configure IntelliSense for Linux connections<\/a>.<\/p>\n<h3>Visual Studio CMake projects and WSL<\/h3>\n<p>Let\u2019s start by looking at a simple CMake project.<\/p>\n<p>1. Start Visual Studio 2019 (version 16.1 or later) and create a new CMake project using the \u201cCMake Project\u201d template or open an existing one.<\/p>\n<p>2. Navigate to the configuration drop-down menu and select \u201cManage Configurations&#8230;\u201d This will open the CMake Settings Editor.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/05\/Linux_ManageConfig-1.png\"><img decoding=\"async\" class=\"aligncenter wp-image-27239\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/05\/Linux_ManageConfig-1.png\" alt=\"A drop-down menu in Visual Studio with the option to &quot;Manage Configurations...&quot; selected.\" width=\"450\" height=\"141\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/05\/Linux_ManageConfig-1.png 579w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/05\/Linux_ManageConfig-1-300x94.png 300w\" sizes=\"(max-width: 450px) 100vw, 450px\" \/><\/a>\n3. Visual Studio creates an x64-Debug configuration by default. You can add a new WSL configuration by clicking on the green plus sign above the configuration manager on the left-hand side of the editor.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/05\/Linux_Configs.png\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-27240\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/05\/Linux_Configs.png\" alt=\"Image Linux Configs\" width=\"283\" height=\"161\" \/><\/a>\n4. Select a WSL-GCC-Debug configuration. If you are building with Clang, then you can select a WSL-Clang-Debug or WSL-Clang-Release configuration.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/05\/Linux_NewConfig.png\"><img decoding=\"async\" class=\"aligncenter wp-image-27241\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/05\/Linux_NewConfig.png\" alt=\"Image Linux NewConfig\" width=\"450\" height=\"549\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/05\/Linux_NewConfig.png 671w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/05\/Linux_NewConfig-246x300.png 246w\" sizes=\"(max-width: 450px) 100vw, 450px\" \/><\/a>\n5. By default, Visual Studio will pick up on your <a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/wsl\/wsl-config#set-a-default-distribution\">default WSL configuration<\/a>. If you have side-by-side installations of WSL, then you can specify which WSL executable Visual Studio should use by setting the \u201cPath to WSL executable\u201d property under the \u201cGeneral\u201d section of the CMake Settings Editor.<\/p>\n<p>6. Select Unix Makefiles as your generator. If you are using Visual Studio 2019 version 16.6 or later, then Visual Studio will attempt to use Ninja by default. Scroll down to the bottom of the CMake Settings Editor and click the link to \u201cShow advanced settings\u201d. Set the \u201cGenerator\u201d to Unix Makefiles.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/05\/Linux_Generator.png\"><img decoding=\"async\" class=\"aligncenter wp-image-27249\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/05\/Linux_Generator.png\" alt=\"The CMake Settings Editor is open. The Generator option is set to Unix Makefiles.\" width=\"650\" height=\"95\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/05\/Linux_Generator.png 1090w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/05\/Linux_Generator-300x44.png 300w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/05\/Linux_Generator-1024x149.png 1024w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/05\/Linux_Generator-768x112.png 768w\" sizes=\"(max-width: 650px) 100vw, 650px\" \/><\/a><\/p>\n<p>7. Save the editor (Ctrl + S) and select your WSL-GCC-Debug configuration as your active configuration using the configuration drop-down menu at the top of the page. This will start the cache generation of your WSL configuration.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/05\/Linux_SelectConfig.png\"><img decoding=\"async\" class=\"aligncenter wp-image-27242\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/05\/Linux_SelectConfig.png\" alt=\"Image Linux SelectConfig\" width=\"450\" height=\"152\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/05\/Linux_SelectConfig.png 592w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/05\/Linux_SelectConfig-300x101.png 300w\" sizes=\"(max-width: 450px) 100vw, 450px\" \/><\/a>\n8. If you don\u2019t have CMake on your WSL installation, then you will be prompted to automatically deploy a recent version of CMake from Visual Studio. If you are missing any other dependencies (gcc, gdb, make, rsync, zip) then see above for Setting up WSL.<\/p>\n<p>9. In the Solution Explorer expand the project subfolder and in the .cpp file set a breakpoint in <span class=\"lang:default highlight:0 decode:true crayon-inline\">main()<\/span>\u00a0.<\/p>\n<p>10. In the launch bar change the launch target from \u201cCurrent Document\u201d to your project name.<\/p>\n<p>11. Now click \u201cStart\u201d (Debug &gt; Start) or press F5. Your project will build, the executable will launch, and you will hit your breakpoint. You can see the output of your program (in this case, \u201cHello CMake\u201d) in the Linux Console Window.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/05\/Linux_Debug.png\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-27243\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/05\/Linux_Debug.png\" alt=\"A sample CMake project is running under the debugger and hitting a breakpoint. The Linux Console Window displays the program output.\" width=\"1764\" height=\"1137\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/05\/Linux_Debug.png 1764w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/05\/Linux_Debug-300x193.png 300w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/05\/Linux_Debug-1024x660.png 1024w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/05\/Linux_Debug-768x495.png 768w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/05\/Linux_Debug-1536x990.png 1536w\" sizes=\"(max-width: 1764px) 100vw, 1764px\" \/><\/a><\/p>\n<h3>Visual Studio MSBuild-based projects and WSL<\/h3>\n<p>We also support using WSL from our MSBuild-based Linux projects in Visual Studio. Here are the steps for getting started.<\/p>\n<p>1. Create a new Linux Console Application (you can filter platform by Linux and look for \u201cConsole App\u201d) in Visual Studio 2019 version 16.1 or later or open an existing one.<\/p>\n<p>2. Right-click on the project in the Solution Explorer and select \u201cProperties\u201d to open the Project Property Pages.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-24247\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/05\/word-image-5.png\" alt=\"The Solution Explorer in Visual Studio. &quot;Properties&quot; is selected from the project's context menu.\" width=\"500\" height=\"587\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/05\/word-image-5.png 671w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/05\/word-image-5-255x300.png 255w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/p>\n<p>3. In the dialog that opens you will see the \u201cGeneral\u201d property page. On this page, there is an option for \u201cPlatform Toolset.\u201d Change this from \u201cGCC for Remote Linux\u201d to \u201cGCC for Windows Subsystem for Linux.\u201d If you are building with Clang, then you can select the \u201cClang for Windows Subsystem for Linux\u201d toolset.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/05\/Linux_WSLToolset.png\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-27246\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/05\/Linux_WSLToolset.png\" alt=\"Image Linux WSLToolset\" width=\"843\" height=\"145\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/05\/Linux_WSLToolset.png 843w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/05\/Linux_WSLToolset-300x52.png 300w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/05\/Linux_WSLToolset-768x132.png 768w\" sizes=\"(max-width: 843px) 100vw, 843px\" \/><\/a><\/p>\n<p>4. By default, Visual Studio will target the WSL installation that is <a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/wsl\/wsl-config#set-a-default-distribution\">set as the default through wslconfig<\/a>. If you have side-by-side installations of WSL, then you can specify which WSL executable Visual Studio should use by setting the \u201cWSL *.exe full path\u201d option directly below \u201cPlatform Toolset.\u201d Press OK.<\/p>\n<p>5. Set a breakpoint in main.cpp and click \u201cStart Debugging\u201d (Debug &gt; Start Debugging). If you are missing any dependencies on your WSL installation (gcc, gdb, rsync, zip) then see above for Setting up WSL. You can see the output of your program in the Linux Console Window.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/05\/Linux_MSBuildDebug.png\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-27247\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/05\/Linux_MSBuildDebug.png\" alt=\"A C++ Linux project is running under the debugger. The Linux Console Windows displays the output of the application.\" width=\"1769\" height=\"1123\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/05\/Linux_MSBuildDebug.png 1769w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/05\/Linux_MSBuildDebug-300x190.png 300w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/05\/Linux_MSBuildDebug-1024x650.png 1024w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/05\/Linux_MSBuildDebug-768x488.png 768w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/05\/Linux_MSBuildDebug-1536x975.png 1536w\" sizes=\"(max-width: 1769px) 100vw, 1769px\" \/><\/a><\/p>\n<h3>Give us your feedback!<\/h3>\n<p>If you have feedback on WSL or anything regarding our Linux support in Visual Studio, we would love to hear from you. We can be reached via the comments below or Twitter (<a href=\"https:\/\/twitter.com\/visualc\">@VisualC<\/a>). If you encounter other problems with Visual Studio or MSVC or have a\u00a0suggestion, you can use the\u00a0<a href=\"https:\/\/docs.microsoft.com\/en-us\/visualstudio\/ide\/how-to-report-a-problem-with-visual-studio?view=vs-2019\">Report a Problem<\/a>\u00a0tool in Visual Studio or head over to\u00a0<a href=\"https:\/\/developercommunity.visualstudio.com\/spaces\/8\/index.html\">Visual Studio Developer Community<\/a>.<\/p>\n<p><a name=\"comingsoon\"><\/a><\/p>\n<hr \/>\n<h4>Coming soon&#8230;<\/h4>\n<p>More announcements about AddressSanitizer integration for Linux projects, Code Analysis quick fixes, and Quick Info improvements are coming to the C++ Team Blog later this week. Stay tuned!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post was updated on December 11, 2020 Visual Studio 2019 version 16.1 added native support for using C++ with the Windows Subsystem for Linux (WSL). WSL lets you run a lightweight Linux environment directly on Windows, including most command-line tools, utilities, and applications. In Visual Studio you no longer need to add a remote [&hellip;]<\/p>\n","protected":false},"author":2953,"featured_media":27241,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[270,266,279],"tags":[],"class_list":["post-24241","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-announcement","category-cmake","category-linux"],"acf":[],"blog_post_summary":"<p>This post was updated on December 11, 2020 Visual Studio 2019 version 16.1 added native support for using C++ with the Windows Subsystem for Linux (WSL). WSL lets you run a lightweight Linux environment directly on Windows, including most command-line tools, utilities, and applications. In Visual Studio you no longer need to add a remote [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/24241","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/users\/2953"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/comments?post=24241"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/24241\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media\/27241"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media?parent=24241"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/categories?post=24241"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/tags?post=24241"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}