{"id":78,"date":"2019-06-19T17:39:31","date_gmt":"2019-06-20T00:39:31","guid":{"rendered":"http:\/\/visualgdb.com\/w\/?p=78"},"modified":"2019-06-20T18:13:48","modified_gmt":"2019-06-21T01:13:48","slug":"mingw","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/tutorials\/mingw\/","title":{"rendered":"Using Visual Studio to debug MinGW applications"},"content":{"rendered":"<p>This tutorial shows how to build and debug a simple MinGW-based application using VisualGDB.<\/p>\n<p>Before you begin, please download and install <a href=\"https:\/\/visualgdb.com\/download\/\">VisualGDB<\/a>.<\/p>\n<ol>\n<li>To create your first project, select File-&gt;New-&gt;Project in Visual Studio and then choose VisualGDB -&gt; MinGW\/Cygwin Project Wizard:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/01-newprj-4.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4994\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/01-newprj-4.png\" alt=\"\" width=\"835\" height=\"612\" \/><\/a><span class=\"warning\">Please avoid spaces in directory names, as GDB does not support it.<\/span><\/li>\n<li>Once VisualGDB wizard is started, &#8220;Create a new project -&gt; Application&#8221; and pick the build subsystem type. VisualGDB supports both GNU Make and CMake for MinGW projects. In this tutorial we will show the simpler GNU Make option, so select it in the &#8220;Build the project using&#8221; field:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/02-gnu.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4995\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/02-gnu.png\" alt=\"\" width=\"786\" height=\"593\" \/><\/a><\/li>\n<li>On the next page of the wizard select the toolchain that will be used to build the project. If you haven&#8217;t installed the MinGW toolchain yet, VisualGDB can install it for you automatically:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/03-getmingw.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4996\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/03-getmingw.png\" alt=\"\" width=\"786\" height=\"593\" \/><\/a><\/li>\n<li>Press &#8220;Finish&#8221; to generate the project. VisualGDB will created a sample source file, a generic Makefile and a file containing all GNU Make flags. Press Ctrl-Shift-Build to build the generated project:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/04-built.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4997\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/04-built.png\" alt=\"\" width=\"1061\" height=\"774\" \/><\/a>The generated makefile will disable optimization for the Debug configuration and set it to maximum for Release. To change this behavior, you can modify the flags.mak file.<\/li>\n<li>Now we will show how to add new sources to the project. Right-click in Solution Explorer and select Add-&gt;New Item: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/05-newsource-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5006\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/05-newsource-1.png\" alt=\"\" width=\"1061\" height=\"774\" \/><\/a><\/li>\n<li>Add a new source file and try building the project. Note how VisualGDB has automatically updated the Makefile to reflect the changes:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/06-list-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5007\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/06-list-1.png\" alt=\"\" width=\"1061\" height=\"774\" \/><\/a><\/li>\n<li>Now we will show how to use the create-from-use feature of the Advanced Clang IntelliSense. Modify the <strong>main()<\/strong> function to call another non-existing function as shown below, then press Ctrl-. and click on &#8220;Create function\/method stub&#8221;:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/07-newfunc.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5000\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/07-newfunc.png\" alt=\"\" width=\"1061\" height=\"774\" \/><\/a><\/li>\n<li>VisualGDB will automatically detect the types of the arguments and suggest creating a new method. Add a basic body to the function and press Ctrl-Enter to generate it:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/08-body.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5001\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/08-body.png\" alt=\"\" width=\"1061\" height=\"774\" \/><\/a><\/li>\n<li>Call the function from main() and use the CodeJumps link on NewFunction() to explore the call tree: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/09-calls.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5002\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/09-calls.png\" alt=\"\" width=\"1061\" height=\"774\" \/><\/a><\/li>\n<li>Now we will show how to use C++17 features with MinGW projects. Open VisualGDB Project Properties and add &#8220;-std=c+=17&#8221; to CXXFLAGS: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/10-flags.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5003\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/10-flags.png\" alt=\"\" width=\"1002\" height=\"625\" \/><\/a><\/li>\n<li>Replace <strong>NewFunction()<\/strong> with a function adding its arguments using the C++17 pack folding syntax:\n<pre class=\"\">template &lt;typename ...Args&gt; void NewFunction(Args ...args)\r\n{\r\n\tcout &lt;&lt; (args + ... + 0) &lt;&lt; endl;\r\n}<\/pre>\n<p>Then call the new function from <strong>main()<\/strong>:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/11-folding.png\"> <img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5004\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/11-folding.png\" alt=\"\" width=\"1061\" height=\"774\" \/><\/a><\/li>\n<li>Build the project, set a breakpoint after the call to <strong>NewFunction()<\/strong> and start debugging it. Observe how the correct sum of the arguments is printed to the console:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/12-folded.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5005\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2014\/02\/12-folded.png\" alt=\"\" width=\"1061\" height=\"774\" \/><\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to build and debug a simple MinGW-based application using VisualGDB. Before you begin, please download and<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29],"tags":[40],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/78"}],"collection":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/comments?post=78"}],"version-history":[{"count":2,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/78\/revisions"}],"predecessor-version":[{"id":5008,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/78\/revisions\/5008"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=78"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=78"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=78"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}