{"id":16385,"date":"2017-07-19T06:00:26","date_gmt":"2017-07-19T13:00:26","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/vcblog\/?p=16385"},"modified":"2022-08-11T20:59:47","modified_gmt":"2022-08-11T20:59:47","slug":"using-mingw-and-cygwin-with-visual-cpp-and-open-folder","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cppblog\/using-mingw-and-cygwin-with-visual-cpp-and-open-folder\/","title":{"rendered":"Using MinGW and Cygwin with Visual C++ and Open Folder"},"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 SCXW58491640 BCX8\" href=\"https:\/\/docs.microsoft.com\/en-us\/cpp\/build\/open-folder-projects-cpp?view=msvc-170\" target=\"_blank\" rel=\"noreferrer noopener\"><span class=\"TextRun Underlined SCXW58491640 BCX8\" lang=\"EN-GB\" xml:lang=\"EN-GB\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW58491640 BCX8\" data-ccp-charstyle=\"Hyperlink\">Open Folder support for C++ build systems in Visual Studio<\/span><\/span><\/a>.<\/div>Building cross-platform C and C++ code is easier than ever with <a href=\"https:\/\/www.visualstudio.com\/vs\/preview\/\">Visual Studio 15.3 Preview 4<\/a>.\u00a0 The latest preview improves support for alternative compilers and build environments such as MinGW and Cygwin.\u00a0 MinGW (<a href=\"https:\/\/mingw-w64.org\/doku.php\">Minimalist GNU for Windows<\/a>), in case you are not familiar with it, is a compiler in the <a href=\"https:\/\/gcc.gnu.org\/\">GCC<\/a> family designed to run natively on Windows.\u00a0 If you are interested in a quick rundown of this new functionality, check out our <a href=\"https:\/\/channel9.msdn.com\/Shows\/C9-GoingNative\/GoingNative-59-MinGW-in-Visual-Studio\">latest GoingNative episode on Channel 9<\/a>.<\/p>\n<p>Most MinGW installations, however, include much more than just a compiler.\u00a0 Most distributions of MinGW include a whole host of other tools that can be used to build and assemble software on Windows using the familiar GNU toolset.\u00a0 MinGW build environments often contain an entire POSIX development environment that can be used to develop both native Windows software using POSIX build tools and POSIX software that runs on Windows with the help of an emulation layer.<\/p>\n<p>Please <a href=\"https:\/\/www.visualstudio.com\/vs\/preview\/\">download the preview<\/a> and try out the latest C++ features. \u00a0You will need to also make sure you install the Linux Tools for C++ workload in addition to Desktop C++ to use these new features. \u00a0You can learn more about <a href=\"https:\/\/aka.ms\/openfolder\">Open Folder on the Microsoft docs site<\/a>.\u00a0 We are looking forward to your feedback.<\/p>\n<h3>Why MinGW<\/h3>\n<p>There are three reasons why you might want to use MinGW on Windows.\u00a0 The first is simply compiler compatibility.\u00a0 We all love MSVC, but the reality is that some codebases are designed from the ground up under the expectation that they will be built with GCC.\u00a0 This is especially true for many cross-platform projects.\u00a0 Porting these codebases to support MSVC can be a costly endeavor when all you want to do is get up and running with your code.\u00a0 With MinGW you can be up and running in minutes instead of days.<\/p>\n<p>There is, however, an even more compelling reason to use MinGW than source compatibility with GCC.\u00a0 MinGW environments are not just a compiler, but include entire POSIX build environments.\u00a0 These development environments allow cross-platform projects to build on Windows with few if any modifications to their build logic.\u00a0 Need Make or AutoTools, it\u2019s already installed \u2013 if you are using an environment like <a href=\"http:\/\/www.msys2.org\/\">MSYS2<\/a> nearly every tool you might need is only a single package management command away.<\/p>\n<p>Finally, some MinGW distributions such as MSYS2 allow you to build native POSIX software on Windows without porting the code to Win32.\u00a0 This is accomplished by an emulation layer that allows native POSIX tools to run on Windows.\u00a0 This same layer is what allows all your favorite POSIX build tools to run in the MinGW environment.<\/p>\n<h3>Install MinGW on Windows<\/h3>\n<p>Getting started with MinGW is simple once you have installed the <a href=\"https:\/\/www.visualstudio.com\/vs\/preview\/\">latest Visual Studio Preview<\/a>.\u00a0 First, you will need to make sure that you select the C++ workload when you install Visual Studio.\u00a0 Next, you will need to download MinGW itself.\u00a0 There are actually many ways to install MinGW.\u00a0 If you are particularly adventurous you can build it from source, but it is much easier to install any of the popular binary distributions.\u00a0 One of the more popular distributions is <a href=\"http:\/\/www.msys2.org\/\">MSYS2<\/a> (<a href=\"https:\/\/github.com\/orlp\/dev-on-windows\/wiki\/Installing-GCC--&amp;-MSYS2\">getting started guide<\/a>).\u00a0 A standard MSYS2 installation installs three build environments: POSIX build environments to natively target 32-bit and 64-bit Windows, and an emulation layer to build POSIX software that targets Windows.<\/p>\n<p>If you have a particular project in mind that you are working with, it is also worth checking out if they have any project-specific instructions on how to install MinGW.\u00a0 Some projects even include their own custom tailored MinGW distributions.<\/p>\n<h3>Use MinGW with Visual Studio<\/h3>\n<p>It has been possible to <a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/use-any-c-compiler-with-visual-studio\/\">use GCC based compilers with Visual Studio projects<\/a> for some time already, but many cross-platform projects that build with MinGW on Windows are not organized into Solution and Visual C++ project files.\u00a0 Creating these assets for a real-world project can be time consuming.\u00a0 To streamline onboarding cross-platform projects into Visual Studio, we now support opening folders directly, and in the latest preview it is easier than ever to use alternative compilers and build environments such as MinGW.<\/p>\n<p>With \u201cOpen Folder\u201d you can edit your code with full IntelliSense, build, and debug with the same fidelity as is available with full Solution and C++ project files.\u00a0 However, instead of authoring hundreds of lines of XML you just need to write a small amount of JSON.\u00a0 Even better, you only need to write the JSON for the features you need.\u00a0 For instance, if you only want to edit code in the IDE and stick to the command line for everything else, you only need to write a few lines of JSON to enable IntelliSense.<\/p>\n<h4>Edit with Accurate IntelliSense<\/h4>\n<p>To get the most out of IntelliSense when using \u201cOpen Folder\u201d you will need to create a CppProperties.json file.\u00a0 You can create this file by selecting \u201cProject-&gt;Edit Settings-&gt;CppProperties.json\u2026\u201d from the main menu.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/Create-CppPropertiesJSON.png\"><img decoding=\"async\" class=\"size-full wp-image-16395 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/Create-CppPropertiesJSON.png\" alt=\"Create CppProperties.json\" width=\"566\" height=\"95\" \/><\/a><\/p>\n<p>This file is automatically populated with four configurations: \u201cDebug\u201d and \u201cRelease\u201d for the x86 and x64 architectures.\u00a0 If you don\u2019t need all of these configurations you can remove them to reduce clutter. \u00a0Next, you will need to configure a few options to get IntelliSense that is consistent with your MinGW build.<\/p>\n<p>In each configuration, you may notice an &#8220;intelliSenseMode&#8221; key.\u00a0 For MinGW and other GCC based compilers you should set this to &#8220;windows-clang-x86&#8221; or &#8220;windows-clang-x64&#8221; depending on your architecture.<\/p>\n<p>Next, you will need to add the include paths for your project.\u00a0 Configuration specific include paths can be added to the &#8220;includePath&#8221; array under each configuration.\u00a0 You can add project-wide include paths here as well but it may be easier to add them to the \u201cINCLUDE\u201d environment variable, by adding an &#8220;environment&#8221; block to your configuration file as a peer to &#8220;configurations&#8221;:<\/p>\n<pre class=\"prettyprint js\">\"environments\": [\r\n  {\r\n    \"MINGW_PREFIX\": \"C:\/msys64\/mingw64\",\r\n    \"MINGW_CHOST \": \"x86_64-w64-mingw32\",\r\n    \"MINGW_PACKAGE_PREFIX\": \"mingw-w64-x86_64\",\r\n    \"MSYSTEM\": \"MINGW64\",\r\n    \"MSYSTEM_CARCH\": \"x64_64\",\r\n    \"MSYSTEM_PREFIX\": \"${env.MINGW_PREFIX}\",\r\n    \"SHELL\": \"${env.MINGW_PREFIX}\/..\/usr\/bin\/bash\",\r\n    \"TEMP\": \"${env.MINGW_PREFIX}\/..\/tmp\",\r\n    \"TMP\": \"${env.TEMP}\",\r\n    \"PATH\": \"${env.MINGW_PREFIX}\/bin;${env.MINGW_PREFIX}\/..\/usr\/local\/bin;${env.MINGW_PREFIX}\/..\/usr\/bin;${env.PATH}\",\r\n    \"INCLUDE\": \"project\/lib\/include;${env.MINGW_PREFIX}\/mingw\/include\"\r\n  }\r\n],\r\n<\/pre>\n<p><em>Note: MINGW_PREFIX should point to your MinGW installation, if you installed MSYS2 with default settings this directory will be \u201cC:\/msys64\/mingw64\u201d.<\/em><\/p>\n<p>Most GCC based compilers, including MinGW, automatically include certain system header directories.\u00a0 For full IntelliSense, you will need to add these to the include path list.\u00a0 You can get a list of these standard includes by running:<\/p>\n<pre class=\"prettyprint disable-colors\">echo | gcc -Wp,-v -x c++ - -fsyntax-only\r\n<\/pre>\n<h3>Build with MinGW<\/h3>\n<p>Once you have set up IntelliSense (with \u201cMINGW_PREFIX\u201d and its related environment variables defined and added to \u201cPATH\u201d), building with MinGW is quite simple.\u00a0 First, you will need to create a build task.\u00a0 Right click your build script (or any other file you would like to associate with the task) and select \u201cConfigure Tasks\u201d:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/Configure-Tasks.png\"><img decoding=\"async\" class=\"size-full wp-image-16405 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/Configure-Tasks.png\" alt=\"Configure Tasks\" width=\"360\" height=\"333\" \/><\/a><\/p>\n<p>This will create a \u201ctasks.vs.json\u201d file if you don\u2019t already have one and create a stub.\u00a0 Since MinGW and its tools are already on the PATH, as configured in CppProperties.json, you can configure the task to use them like this:<\/p>\n<pre class=\"prettyprint js\">{\r\n  \"version\": \"0.2.1\",\r\n  \"tasks\": [\r\n    {\r\n      \"taskName\": \"build-all\",\r\n      \"appliesTo\": \"hello.cpp\",\r\n      \"contextType\": \"build\",\r\n      \"type\": \"launch\",\r\n      \"command\": \"${env.comspec}\",\r\n      \"args\": [\r\n        \"g++ -o helloworld.exe -g hello.cpp\"\r\n      ]\r\n    }\r\n  ]\r\n}\r\n<\/pre>\n<p>This example is simple \u2013 it only builds one file \u2013 but this can call into Make or any other build tools supported by MinGW as well.\u00a0 Once you save this file, you should have a \u201cBuild\u201d option available for any files matched by the &#8220;appliesTo&#8221; tag.<\/p>\n<p>Selecting \u201cBuild\u201d from the context menu will run the command and stream any console output to the Output Window.<\/p>\n<h4>Debug MinGW Applications<\/h4>\n<p>Once you have configured your project to build, debugging can be configured with by selecting a template.\u00a0 Once you have built an executable, find it in the folder view and select \u201cDebug and Launch Settings\u201d from the executable\u2019s context menu in the Solution Explorer:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/Debug-Exe.png\"><img decoding=\"async\" class=\"size-full wp-image-16406 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/Debug-Exe.png\" alt=\"Debug and Launch Settings\" width=\"360\" height=\"398\" \/><\/a><\/p>\n<p>This will allow you to select a debugger template for this executable:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/Select-Debugger.png\"><img decoding=\"async\" class=\"size-full wp-image-16415 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/Select-Debugger.png\" alt=\"Select a Debugger\" width=\"436\" height=\"411\" \/><\/a><\/p>\n<p>This will create an entry in the \u201claunch.vs.json\u201d file that configures the debugger:<\/p>\n<pre class=\"prettyprint js\">{\r\n  \"version\": \"0.2.1\",\r\n  \"defaults\": {},\r\n  \"configurations\": [\r\n    {\r\n      \"type\": \"cppdbg\",\r\n      \"name\": \"helloworld.exe\",\r\n      \"project\": \"helloworld.exe\",\r\n      \"cwd\": \"${workspaceRoot}\",\r\n      \"program\": \"${debugInfo.target}\",\r\n      \"MIMode\": \"gdb\",\r\n      \"miDebuggerPath\": \"${env.MINGW_PREFIX}\\\\bin\\\\gdb.exe\",\r\n      \"externalConsole\": true\r\n    }\r\n  ]\r\n}\r\n<\/pre>\n<p>In most cases, you won\u2019t have to modify this at all, as long as you defined \u201cMINGW_PREFIX\u201d in the \u201cCppProperties.json\u201d file.\u00a0 Now you are ready to edit, build, and debug the code.\u00a0 You can right click the executable and select \u201cDebug\u201d in the context menu or select the newly added launch configuration in the debug dropdown menu to use F5.\u00a0 You should be able to use breakpoints and other debugging features like the Locals, Watch, and Autos Windows.\u00a0 If you are having trouble hitting breakpoints, make sure your build command was configured to emit GDB compatible symbols and debugging information.<\/p>\n<p>Finally, to complete the full inner loop, you can add an &#8220;output&#8221; tag to your build task in \u201ctasks.vs.json\u201d.\u00a0 For instance:<\/p>\n<pre class=\"prettyprint js\">\"output\": \"${workspaceRoot}\\\\helloworld.exe\",\r\n<\/pre>\n<h3>What About Cygwin and Other Compilers in the GCC Family<\/h3>\n<p>While we have discussed MinGW specifically in this post, it is worth keeping in mind that very little of the content above is specific to MinGW.\u00a0 These topics apply equally well to other compilers and build environments so long as they can produce binaries that can be debugged with GDB.\u00a0 With minimal modification, the instructions under the \u201cUse MinGW with Visual Studio\u201d section above should work fine with other environments such as <a href=\"https:\/\/www.cygwin.com\/\">Cygwin<\/a> and even other compilers like Clang.\u00a0 In the future, Open Folder will support an even greater variety of compilers and debuggers &#8211; some out of the box, some with a little bit of extra configuration that may be specific to your projects.<\/p>\n<h3>Send Us Feedback<\/h3>\n<p>To try out the latest and greatest C++ features and give us some early feedback, please download and install the latest\u00a0<a href=\"https:\/\/www.visualstudio.com\/vs\/preview\/\">Visual Studio 2017 Preview<\/a>.\u00a0 As always, we welcome your feedback.\u00a0 Feel free to send any comments through e-mail at <a href=\"mailto:visualcpp@microsoft.com\">visualcpp@microsoft.com<\/a>, through\u00a0<a href=\"https:\/\/twitter.com\/visualc\">Twitter @visualc<\/a>, or Facebook at\u00a0<a href=\"https:\/\/www.facebook.com\/Microsoft-Visual-Cpp-222043184527264\/\">Microsoft Visual Cpp<\/a>.<\/p>\n<p>If you encounter other problems with Visual Studio 2017 please let us know via\u00a0<a href=\"https:\/\/docs.microsoft.com\/en-us\/visualstudio\/ide\/how-to-report-a-problem-with-visual-studio-2017\">Report a Problem<\/a>, which is available in both the installer and the IDE itself. \u00a0For suggestions, let us know through\u00a0<a href=\"https:\/\/visualstudio.uservoice.com\/forums\/121579-visual-studio-2015\/category\/30937-languages-c\">UserVoice<\/a>. We look forward to your feedback!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Building cross-platform C and C++ code is easier than ever with Visual Studio 15.3 Preview 4.\u00a0 The latest preview improves support for alternative compilers and build environments such as MinGW and Cygwin.\u00a0 MinGW (Minimalist GNU for Windows), in case you are not familiar with it, is a compiler in the GCC family designed to run [&hellip;]<\/p>\n","protected":false},"author":326,"featured_media":35994,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[270,269],"tags":[8,140,233,308,6,309,268,185],"class_list":["post-16385","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-announcement","category-openfolder","tag-announcement","tag-c","tag-cross-platform","tag-existing-code","tag-ide","tag-import","tag-openfolder","tag-visual-studio"],"acf":[],"blog_post_summary":"<p>Building cross-platform C and C++ code is easier than ever with Visual Studio 15.3 Preview 4.\u00a0 The latest preview improves support for alternative compilers and build environments such as MinGW and Cygwin.\u00a0 MinGW (Minimalist GNU for Windows), in case you are not familiar with it, is a compiler in the GCC family designed to run [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/16385","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\/326"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/comments?post=16385"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/16385\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media\/35994"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media?parent=16385"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/categories?post=16385"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/tags?post=16385"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}