VisualStudio 2019 now provides comprehensive support for CMake projects with extensive integration. VS2019 utilizes Ninja CMake generator to get the job done.
Upon opening a CMake project in VisualStudio 2019, the default behavior is to attempt a CMake run using x86-Debug or x64-Debug (depending on your host-system). VS2019 generates a CMakeSettings.json to configure the CMake cached variables. To make things easy, VS2019 provides a UI binding to the CMakeSettings.json to provide a table of the cached variables and their default values. You can either directly edit the JSON or use the UI to provide CMake your system configurations.
- In the 'Configuration' drop box at the top, select 'Manage Configurations...'. Click on the plus sign to add a new configuration, and click on 'IoT-Debug'.
- Specify the toolchain file (navigate to /build/sam_gcc.cmake)
- VS2019 provides default CMAKE_C_FLAGS. Ensure this field is empty to start.
- Fill out the fields in the CMakeSetting.json according to your system and as defined by Building section.
Using SAMD51J20A
{
"configurations": [
{
"name": "IoT-Debug",
"generator": "Ninja",
"configurationType": "Debug",
"buildRoot": "${projectDir}\\out\\build\\${name}",
"installRoot": "${projectDir}\\out\\install\\${name}",
"cmakeCommandArgs": "",
"buildCommandArgs": "-v",
"ctestCommandArgs": "",
"inheritEnvironments": [ "gcc-arm" ],
"variables": [
{
"name": "CMAKE_C_COMPILER",
"value": "arm-none-eabi-gcc.exe",
"type": "STRING"
},
{
"name": "CMAKE_CXX_COMPILER",
"value": "arm-none-eabi-g++.exe",
"type": "STRING"
},
{
"name": "CMAKE_C_FLAGS",
"value": "",
"type": "STRING"
},
{
"name": "CMAKE_CXX_FLAGS",
"value": "-nostartfiles -fno-rtti -fno-exceptions",
"type": "STRING"
},
{
"name": "CMAKE_CXX_STANDARD",
"value": "14",
"type": "STRING"
},
{
"name": "CMAKE_SYSTEM_NAME",
"value": "Generic",
"type": "STRING"
},
{
"name": "CMAKE_SYSTEM_PROCESSOR",
"value": "arm",
"type": "STRING"
},
{
"name": "ARM_CPU",
"value": "cortex-m4",
"type": "STRING"
},
{
"name": "ATMEL_ARCH",
"value": "samd51",
"type": "STRING"
},
{
"name": "SAM_MCU",
"value": "samd51j20a",
"type": "STRING"
}
],
"intelliSenseMode": "linux-gcc-arm",
"cmakeToolchain": "C:/Users/mille/Github/jensen-miller/sam-cmake-template/build/sam_gcc.cmake"
}
]
}
VS2019 will generate the projects in the CMake Targets View. Switch to this view using the Solution Explorer. Thus far, the projects generated are:
- Flash_(project-name)
- Upload_(project-name)
- *.bin, *.elf, *.uf2 Executables
-
Cannot find 'IoT-Debug' option
- Ensure you have the feature installed Embedded and IoT development tools under Individual Components in the VS2019 installer.
-
Getting the error: '.init' is not defined (generated by the linker)
- Typically, this means you may have missed clearing the CMAKE_C_FLAGS field in the CMakeSettings.json. By default, CMAKE_C_FLAGS contains the flag '-nostartfiles' which causes this error.