{"id":8861,"date":"2024-05-01T10:05:23","date_gmt":"2024-05-01T17:05:23","guid":{"rendered":"https:\/\/visualgdb.com\/w\/?p=8861"},"modified":"2024-05-01T10:06:21","modified_gmt":"2024-05-01T17:06:21","slug":"importing-cmake-projects-using-presets","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/tutorials\/linux\/cmake\/import\/presets\/","title":{"rendered":"Importing CMake Projects using Presets"},"content":{"rendered":"<p>This tutorial shows how to import preset-based CMake projects into VisualGDB so that they can be developed in multiple IDEs simultaneously. We will create a basic C++ project for Raspberry Pi using CMake Presets, build it with VS code, and then import it into VisualGDB, showing how preset changes made in one IDE will immediately affect the other one.<\/p>\n<p>Before you begin, install VisualGDB 6.0R2 or later, our <a href=\"https:\/\/gnutoolchains.com\/raspberry\/\">Raspberry Pi toolchain<\/a> and create a new folder with the following files:<\/p>\n<p><strong>CMakeLists.txt<\/strong><\/p>\n<pre lang=\"cmake\">cmake_minimum_required(VERSION 3.5)\r\nproject(CMakePresetDemo)\r\nadd_executable(CMakePresetDemo CMakePresetDemo.cpp)<\/pre>\n<p><strong>CMakePresetDemo.cpp<\/strong><\/p>\n<pre lang=\"cpp\">#include &lt;iostream&gt;\r\n\r\nint main(int argc, char *argv[])\r\n{\r\n    std::cout &lt;&lt; \"Hello, World!\" &lt;&lt; std::endl;\r\n    return 0;\r\n}<\/pre>\n<p><strong>CMakePresets.json<\/strong><\/p>\n<pre lang=\"json\">{\r\n    \"version\": 4,\r\n    \"configurePresets\": [\r\n        {\r\n            \"name\": \"RaspberryPi\",\r\n            \"cacheVariables\": {\r\n                \"CMAKE_SYSTEM_NAME\": \"Linux\",\r\n                \"VISUALGDB_TOOLCHAIN_TYPE\": \"Linux\",\r\n                \"VISUALGDB_TOOLCHAIN_SUBTYPE\": \"GCC\",\r\n                \"CMAKE_C_COMPILER\": \"$env{TOOLCHAIN_ROOT}\/bin\/arm-linux-gnueabihf-gcc.exe\",\r\n                \"CMAKE_BUILD_TYPE\": \"Debug\",\r\n                \"CMAKE_SYSROOT\": \"$env{TOOLCHAIN_ROOT}\/arm-linux-gnueabihf\/sysroot\",\r\n                \"CMAKE_MAKE_PROGRAM\": \"$penv{VISUALGDB_DIR}\/ninja.exe\"\r\n            },\r\n            \"environment\": {\r\n                \"TOOLCHAIN_ROOT\": \"C:\/SysGCC\/raspberry\",\r\n                \"PATH\": \"C:\/SysGCC\/raspberry\/bin;$penv{PATH}\"\r\n            },\r\n            \"binaryDir\": \"${sourceDir}\/build\/${presetName}\",\r\n            \"generator\": \"Ninja\"\r\n        }\r\n    ]\r\n}<\/pre>\n<p>Note how the <strong>CMakeLists.txt<\/strong> file specifies the project structure (application consisting of 1 C++ file), but the actual toolchain and the location of various tools (e.g. Ninja) comes from the preset file.<\/p>\n<ol>\n<li>Open the project folder in Visual Studio Code. Press Ctrl-Shift-P and locate the &#8220;<strong>CMake:Configure<\/strong>&#8221; command:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2024\/04\/01-conf.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8862\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2024\/04\/01-conf.png\" alt=\"\" width=\"1135\" height=\"665\" \/><\/a><\/li>\n<li>Pick the <strong>RaspberryPi<\/strong> preset:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2024\/04\/02-raspi.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8863\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2024\/04\/02-raspi.png\" alt=\"\" width=\"1135\" height=\"665\" \/><\/a><\/li>\n<li>VS Code will automatically configure the project based on the preset file, and will allow building it:<br \/>\n<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2024\/04\/04-demo.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8865\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2024\/04\/04-demo.png\" alt=\"\" width=\"1135\" height=\"665\" \/><\/a><\/li>\n<li>Now we will show how to import that project into VisualGDB reusing the same preset file. Start Visual Studio and locate the VisualGDB Linux Project Wizard:<br \/>\n<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2024\/04\/05-linux.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8866\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2024\/04\/05-linux.png\" alt=\"\" width=\"1014\" height=\"660\" \/><\/a><\/li>\n<li>Enter the name and location for the imported project file:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2024\/04\/06-name.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8867\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2024\/04\/06-name.png\" alt=\"\" width=\"1014\" height=\"660\" \/><\/a>Note that the actual build settings are stored in the preset file shared with VS code. The imported project file will only contain the debug settings.<\/li>\n<li>On the first page of the wizard, select <strong>Import a project -&gt; Import a CMake Project<\/strong>:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2024\/04\/07-import.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8868\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2024\/04\/07-import.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>Select&#8221;Build the project locally with a cross-compiler&#8221; and pick the Raspberry Pi toolchain:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2024\/04\/08-toolchain.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8869\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2024\/04\/08-toolchain.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a>The toolchain selected in the wizard will be only used to locate the GDB debugger. Actual build settings and IntelliSense configuration will come from the preset file. If you do not want to import your actual toolchain into VisualGDB, you can pick any toolchain here and simply override the GDB path via <strong>VisualGDB Project Properties<\/strong> later.<\/li>\n<li>On the last page of the wizard select the location of the <strong>CMakeLists.txt<\/strong> file and ensure that the &#8220;Use existing presets&#8221; checkbox is checked:<br \/>\n<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2024\/04\/09-presets-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8870\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2024\/04\/09-presets-1.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>Press <strong>&#8220;Finish&#8221;<\/strong> to import the project into Visual Studio. Note how the RaspberryPi preset got translated into a Solution-level configuration: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2024\/04\/10-raspi.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8871\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2024\/04\/10-raspi.png\" alt=\"\" width=\"1152\" height=\"805\" \/><\/a><\/li>\n<li>Open VisualGDB Project Properties and click the &#8220;Manage&#8221; button near the configuration selector. In the preset editor click &#8220;Create a new configuration preset&#8221;:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2024\/04\/11-imported.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8872\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2024\/04\/11-imported.png\" alt=\"\" width=\"1152\" height=\"805\" \/><\/a><\/li>\n<li>Create a new preset called <strong>RaspberryPi-Release<\/strong> inheriting the regular <strong>RaspberryPi<\/strong> preset:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2024\/04\/12-inherit.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8873\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2024\/04\/12-inherit.png\" alt=\"\" width=\"1152\" height=\"805\" \/><\/a><\/li>\n<li>Change the build type for the new preset to <strong>RelWithDebInfo <\/strong>(you would need to click the edit button first):<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2024\/04\/13-relwithdeb.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8874\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2024\/04\/13-relwithdeb.png\" alt=\"\" width=\"949\" height=\"674\" \/><\/a><\/li>\n<li>Press OK to commit the changes. Note how VisualGDB created a new solution configuration matching the new preset:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2024\/04\/14-configured.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8875\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2024\/04\/14-configured.png\" alt=\"\" width=\"1152\" height=\"805\" \/><\/a><\/li>\n<li>Reopen the folder in VS Code. Note how it picked up the new preset allowing you to configure and build the project with it:<br \/>\n<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2024\/04\/15-preset2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8876\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2024\/04\/15-preset2.png\" alt=\"\" width=\"1152\" height=\"805\" \/><\/a><\/li>\n<li>Add another preset on the VS Code side, inheriting from either of the presets: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2024\/04\/16-addnew.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8877\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2024\/04\/16-addnew.png\" alt=\"\" width=\"1152\" height=\"805\" \/><\/a><\/li>\n<li>Enter a different name for it and commit it by pressing Enter:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2024\/04\/17-another.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8878\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2024\/04\/17-another.png\" alt=\"\" width=\"1152\" height=\"805\" \/><\/a><\/li>\n<li>VS Code will automatically edit the <strong>CMakePresets.json<\/strong> file, adding the new preset:<br \/>\n<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2024\/04\/18-presets.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8879\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2024\/04\/18-presets.png\" alt=\"\" width=\"1152\" height=\"805\" \/><\/a><\/li>\n<li>Go back to Visual Studio and reopen the solution. VisualGDB will detect the new preset, suggesting to create a configuration for it:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2024\/04\/19-fix.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8880\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2024\/04\/19-fix.png\" alt=\"\" width=\"1152\" height=\"805\" \/><\/a><\/li>\n<li>Once you select &#8220;Create&#8221;, VisualGDB will create another solution configuration matching the preset created in VS Code:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2024\/04\/20-another.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8881\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2024\/04\/20-another.png\" alt=\"\" width=\"1152\" height=\"805\" \/><\/a><\/li>\n<\/ol>\n<p>Because both IDEs are using CMake to manage the actual build, you can edit various project settings (e.g. add new files, change include directories), etc. in either of them, and the other IDE will pick up the changes as long as you reload the project.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to import preset-based CMake projects into VisualGDB so that they can be developed in multiple IDEs<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[77,41],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/8861"}],"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=8861"}],"version-history":[{"count":2,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/8861\/revisions"}],"predecessor-version":[{"id":8883,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/8861\/revisions\/8883"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=8861"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=8861"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=8861"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}