{"id":6722,"date":"2020-10-05T10:33:50","date_gmt":"2020-10-05T17:33:50","guid":{"rendered":"https:\/\/visualgdb.com\/w\/?p=6722"},"modified":"2020-10-21T10:34:41","modified_gmt":"2020-10-21T17:34:41","slug":"creating-embedded-cmake-projects-with-visualgdb","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/tutorials\/arm\/cmake\/","title":{"rendered":"Creating Embedded CMake Projects with VisualGDB"},"content":{"rendered":"<p>This tutorial shows how to create, build, debug and edit Embedded CMake projects with VisualGDB. We will create a basic FreeRTOS-based project for the STM32F4Discovery board, and will then show how to create additional library targets and change various project properties. Before you begin, install VisualGDB 5.5 or later.<\/p>\n<ol>\n<li>Start Visual Studio and open the VisualGDB Embedded Project Wizard:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/01-emb.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6723\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/01-emb.png\" alt=\"\" width=\"1024\" height=\"680\" \/><\/a><\/li>\n<li>Enter the name and location of the project you would like to create: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/02-name.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6724\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/02-name.png\" alt=\"\" width=\"1024\" height=\"680\" \/><\/a><\/li>\n<li>On the first page of the VisualGDB-specific portion of the wizard, select &#8220;Create a new project -&gt; Embedded Binary -&gt; Advanced CMake&#8221; and click &#8220;Next&#8221;:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/03-cmake.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6725\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/03-cmake.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>On the next page select the ARM toolchain and pick your device. In this tutorial we will target the STM32F4Discovery board that uses the STM32F407VG device:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/04-dev.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6726\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/04-dev.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a>Although this tutorial uses the GCC compiler to build the project, the Advanced Embedded Project Subsystem also supports the Keil (both ARMCC and ARMClang) and IAR compilers.<\/li>\n<li>The next page of the wizard allows selecting a sample project that will be used as a template. You can choose between a few generic VisualGDB-supplied samples and a huge selection of board-specific samples imported from the STM32 SDK. In this tutorial we will use the generic FreeRTOS LEDBlink sample with the default parameters:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/05-sample.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6727\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/05-sample.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>Finally, choose the debug method that works with your setup. In most cases, you can simply plug in the board and let VisualGDB automatically detect the parameters:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/06-debug.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6728\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/06-debug.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a> Once the debug settings are selected, click &#8220;Finish&#8221; to create a project.<\/li>\n<li>VisualGDB will generate a basic Embedded CMake project consisting of the main application (terminal icon) and the BSP that includes several frameworks (such as FreeRTOS). Press Ctrl-Shift-B to build the application:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/07-built.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6729\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/07-built.png\" alt=\"\" width=\"1193\" height=\"768\" \/><\/a><\/li>\n<li>Set a breakpoint inside the <strong>main()<\/strong> function and press F5 to begin debugging. VisualGDB will automatically program the FLASH memory and start the application. Once the breakpoint triggers, you will be able to view and change the variable values, and also step through the program:\u00a0 <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/08-bkpt.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6730\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/08-bkpt.png\" alt=\"\" width=\"1193\" height=\"768\" \/><\/a><\/li>\n<li>Press F5 to resume the application. While it is running, use the <strong>Debug-&gt;Windows-&gt;Live Watch<\/strong> command to examine its state in a non-intrusive way. If you are using the Custom edition or higher, you can see the FreeRTOS threads, their state, stack usage, and other parameters in real time using the FreeRTOS view: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/09-live.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6731\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/09-live.png\" alt=\"\" width=\"1193\" height=\"768\" \/><\/a>You can read more about Live Watch in <a href=\"https:\/\/visualgdb.com\/documentation\/livevars\/\">VisualGDB Documentation<\/a>.<\/li>\n<li>Press Shift-F5 to stop debugging. We will now show how to change the embedded frameworks used by the project. Right-click on the BSP node in Solution Explorer and select &#8220;Edit Embedded Frameworks&#8221;:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/10-fw.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6732\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/10-fw.png\" alt=\"\" width=\"1193\" height=\"768\" \/><\/a><\/li>\n<li>VisualGDB will display a list of frameworks included in the STM32 BSP, letting you select and customize specific ones. Try switching the <strong>Heap Implementation<\/strong> to <strong>Heap1<\/strong>:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/11-heap.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6733\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/11-heap.png\" alt=\"\" width=\"821\" height=\"593\" \/><\/a><\/li>\n<li>Open <strong>View-&gt;Embedded Memory Explorer<\/strong> and switch to Details view. Then build the project (applying the heap implementation change) and open Memory Explorer again. VisualGDB will show the functions affected by the recent change. You can use the &#8220;Show Only Differences&#8221; button to hide the symbols that were not affected by the change:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/12-diff.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6734\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/12-diff.png\" alt=\"\" width=\"1193\" height=\"768\" \/><\/a>See our <a href=\"https:\/\/visualgdb.com\/documentation\/embedded\/memoryexplorer\/\">Embedded Memory Explorer documentation<\/a> for a detailed listing of Memory Explorer features and modes.<\/li>\n<li>Now we will show how to add a static library to the project and reference it from the main application. Right-click on the project node in Solution Explorer (CMake icon) and select <strong>Add-&gt;New Item<\/strong>:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/13-new.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6735\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/13-new.png\" alt=\"\" width=\"1193\" height=\"768\" \/><\/a><\/li>\n<li>Select &#8220;<strong>Embedded Static Library<\/strong>&#8221; and click &#8220;<strong>Add<\/strong>&#8220;: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/14-lib.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6736\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/14-lib.png\" alt=\"\" width=\"812\" height=\"420\" \/><\/a><\/li>\n<li>Once the library is added, reference it from the main application via <strong>Add-&gt;Add Reference<\/strong>:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/15-ref.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6737\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/15-ref.png\" alt=\"\" width=\"1193\" height=\"768\" \/><\/a><\/li>\n<li>To demonstrate the use of libraries, move the <strong>LED_Thread1()<\/strong> function to the library and mark it with <strong>extern &#8220;C&#8221;<\/strong> (you can find the exact code <a href=\"https:\/\/github.com\/sysprogs\/tutorials\/blob\/master\/visualgdb\/ARM\/CMake\/EmbeddedCMakeDemo\/StaticLibrary.cpp\">here<\/a>):<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/16-moved.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6738\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/16-moved.png\" alt=\"\" width=\"1193\" height=\"768\" \/><\/a>Make sure the project builds successfully by pressing Ctrl-Shift-B.<\/li>\n<li>The structure of the Embedded CMake-based projects is stored in <strong>CMakeLists.txt<\/strong> files and is not duplicated to any other files (such as <strong>.vcxproj<\/strong>). Hence editing the project structure via Solution Explorer will always have the same effect as editing <strong>CMakeLists.txt<\/strong> files directly (see our <a href=\"https:\/\/visualgdb.com\/reference\/cmake\/\">Embedded CMake Framework Reference<\/a> for an exhaustive list of supported statements). Right-click on the <strong>StaticLibrary<\/strong> in Solution Explorer and select &#8220;<strong>Go to Definition<\/strong>&#8221; to open the <strong>CMakeLists.txt<\/strong> file declaring the library:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/17-gotodef.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6739\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/17-gotodef.png\" alt=\"\" width=\"1193\" height=\"768\" \/><\/a><\/li>\n<li>VisualGDB includes a CMake debugger that allows stepping through <strong>CMakeLists.txt<\/strong> files. Right-click on the project node in Solution Explorer and select &#8220;<strong>Launch CMake Debugger<\/strong>&#8220;:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/18-dbg.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6740\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/18-dbg.png\" alt=\"\" width=\"1193\" height=\"768\" \/><\/a><\/li>\n<li>You will now be able to step through functions like <a href=\"https:\/\/visualgdb.com\/reference\/cmake\/add_bsp_based_library\"><strong>add_bsp_based_library()<\/strong><\/a>,\u00a0 set breakpoints and evaluate variables: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/19-debugged.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6741\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/19-debugged.png\" alt=\"\" width=\"1193\" height=\"768\" \/><\/a><\/li>\n<li>Embedded CMake projects allow cloning BSP-supplied files (e.g. FreeRTOS heap implementation) so that you introduce project-specific patches to them. Right-click on the <strong>heap_1.c<\/strong> file and select &#8220;Copy to Project Directory&#8221;: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/20-copy.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6742\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/20-copy.png\" alt=\"\" width=\"1193\" height=\"768\" \/><\/a><\/li>\n<li>VisualGDB will copy the <strong>heap_1.c<\/strong> file into the project directory, directly reference it from the <strong>EmbeddedCMakeDemo<\/strong> application, and will also mark the original shared file as &#8220;excluded from build&#8221;. You can now patch <strong>heap_1.c<\/strong> to add any project-specific modifications to the heap implementation:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/21-copied.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6743\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/21-copied.png\" alt=\"\" width=\"1193\" height=\"768\" \/><\/a><strong>WARNING!<\/strong> If you clone and patch individual source files from the BSP, <strong>DO NOT<\/strong> switch the project to a newer version of that BSP, unless you are prepared to merge the changes. Device vendor-supplied SDKs often add new functionality and change function implementations across versions. Combining the new BSP with the files cloned from the old BSP will very likely cause undefined behavior.<\/li>\n<\/ol>\n<p>You can find the code shown in this tutorial in our <a href=\"https:\/\/github.com\/sysprogs\/tutorials\/tree\/master\/visualgdb\/ARM\/CMake\/EmbeddedCMakeDemo\">tutorials repository<\/a> on GitHub. A detailed reference of VisualGDB-specific CMake statements can be found <a href=\"https:\/\/visualgdb.com\/reference\/cmake\/\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to create, build, debug and edit Embedded CMake projects with VisualGDB. We will create a basic<\/p>\n","protected":false},"author":1,"featured_media":6745,"comment_status":"closed","ping_status":"closed","sticky":true,"template":"","format":"standard","meta":{"footnotes":""},"categories":[222],"tags":[53,77,223,61],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/6722"}],"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=6722"}],"version-history":[{"count":4,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/6722\/revisions"}],"predecessor-version":[{"id":7000,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/6722\/revisions\/7000"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media\/6745"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=6722"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=6722"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=6722"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}