Using IAR ARM Compiler with Visual Studio and VisualGDB

This tutorial shows how to configure VisualGDB to use the IAR ARM compiler instead of the GCC compiler. We will import the demonstration project for the STM32F746-Discovery board using the VisualGDB IAR project importer and then edit the generated Makefiles to use the original IAR compiler instead of the GCC compiler.

This method is only recommended if you prefer the higher optimization provided by the IAR compiler (or if your code contains IAR-specific constructs), as it introduces additional complexity due the necessity to modify Makefiles and maintain 2 separate sets of header files (IAR-specific files for compilation and GCC-specific files for IntelliSense). We will explain in detail which files will be used at what stage and how to change various settings.

Before you begin, install VisualGDB 5.3 or later.

  1. Start Visual Studio and open the VisualGDB Embedded Project Wizard:01-newprj
  2. Select “Automatically import a project in a different format” -> “Import an existing IAR Project”, then specify the path to the project you are importing:02-iarAs we will be modifying the generated Makefiles to run the IAR compiler instead of the GCC compiler, ensure that “GNU Make” is selected instead of MSBuild.
  3. On the next page select the ARM toolchain and pick your device from the list. Although we will be using IAR to build the project, the GCC-based toolchain is still needed to provide IntelliSense-compatible header files:03-device
  4. Finally plug in your JTAG/SWD programmer and select it on the Debug Method page:04-debug
  5. Click “Finish” to generate the project. Locate the debug.mak file containing the debug configuration settings, stm32.mak file containing common settings and Makefile containing the build rules:05-project
  6. Before we proceed with modifying the build rules to switch to the IAR compiler, open the .vcxproj file in a text editor and search for “gcc”. As VisualGDB would normally use GCC to build imported projects, it automatically substitutes IAR-specific files for their GCC equivalents when possible. Revert this by replacing “GCC” back with “IAR”:06-gccNote that you don’t need to change the paths in NMakeIncludeSearchPath as it is only used by IntelliSense.
  7. VisualGDB will also normally use its own startup file instead of the IAR-specific startup file. Remove it from the project and reference the startup_<device>.s file used by your IAR project instead:07-startup
  8. Now we are ready to update the build rules to use the IAR compiler instead of GCC. Before we can do that, we need to ensure that IAR can be reached via a path with no spaces (otherwise GNU Make won’t handle the build rules properly). The easiest way to fix this is to run “mklink /d <path without spaces> <IAR path>” from the Administrator command prompt to create a symbolic link:08-symlink
  9. In order to patch the Makefile to invoke the IAR compiler we will need to know the exact command lines used to build the project. You can find them out by building it from command line with full logging, e.g.

    Once the build is complete, open the log file and locate 3 types of command lines:

    1. Assembler command line (iasmarm.exe)
    2. C/C++ compiler command line (iccarm.exe)
    3. Linker command line (ilinkarm.exe)

    In this example the command lines look as follows:

  10. Makefile-based projects store the compiler paths and arguments in the <mcu type>.mak file (e.g. stm32.mak). You can switch the project from GCC to the IAR compiler by modifying this file as follows:
  11. Copy the assembler, compiler and linker flags from the build log to the corresponding lines in stm32.mak:

    Note that we do not need to copy the project-specific include directories, as VisualGDB automatically imported them from the IAR project (we will explain this in the next step) and the optimization setting (that will be specified differently for debug and release configurations).
    Warning! After we modified the stm32.mak file, do not change the MCU or MCU settings on the first page of VisualGDB Project Properties, as this would overwrite the stm32.mak file. 
  12. Open the debug.mak file:makThis file contains the project-specific settings imported from the IAR project (and also the default GCC settings). Unlike the stm32.mak file, it won’t be overwritten when you use the VisualGDB settings GUI; instead VisualGDB will automatically edit the settings in it. For simplicity, in this tutorial we will edit the file directly, although using the Makefile Settings page of VisualGDB Project Properties would have the same effect.
  13. Clear the contents of CFLAGS, CXXFLAGS, LDFLAGS, START_GROUP and END_GROUP. Then create variables called IAR_CFLAGS and IAR_CXXFLAGS containing the “-On” setting:flags
  14. We use separate variables for IAR-specific flags (that would cause gcc to fail) to avoid breaking the VisualGDB GUI for editing the makefiles. Each time you modify the build settings via VisualGDB Project Properties, VisualGDB will automatically update IntelliSense:dirsThis process involves running gcc with the flags specified via the VisualGDB GUI to check them and get the exact directories and macros used by GCC in this configurations. IAR_CFLAGS and IAR_CXXFLAGS will be ignored by the toolchain testing logic and hence won’t break the IntelliSense setup (we will modify the Makefile in the next step to actually take them into account during building).
  15. Now we will update the Makefile to handle IAR-specific settings. First, replace the syntax for the linker script:

    Then add IAR_CFLAGS to CFLAGS (and similar to CXXFLAGS) before the line adding INCLUDE_DIRS:
  16. Finally change the syntax for generating dependency files and ensure that $(ASM) instead of $(CC) is called for assembly files:

    Note that the build commands (indented lines) should start with <TAB> and not spaces.
    Each time you modify the FileSpecificTemplates section, clear the contents of the “SOURCEFILES :=” assignment in the Makefile. This will force VisualGDB to regenerate file-specific rules at the bottom of the makefile.
  17. Now you can build the project by pressing Ctrl-Shift-B. The build should succeed as if it was a regular GCC-based project:buildGNU Make will automatically handle file dependencies and VisualGDB will allow conveniently editing various settings (e.g. include directories or preprocessor macros) via the VisualGDB Project Properties window.
  18. Press F5 to start debugging the project. Note that the STM32F7-Discovery project contains a resource section that should be loaded into the QSPI FLASH. Unless you configure VisualGDB to program it during the FLASH loading process (see this tutorial), the screen will display garbage instead of most icons. This won’t interfere with debugging though, so you can still quickly check that the project actually runs and can be debugged:debug
  19. One last step would be to tweak VisualGDB to recognize the error messages produced by IAR. You can do this by downloading the BuildMessageRegexes.xml file to your project directory and modifying the .vgdbsettings file as shown below:

    This will let VisualGDB properly recognize the IAR error/warning messages and display them in the Errors window:errorNote as IAR shows the error text on a separate line, it won’t be captured by VisualGDB, so you can either view the build log in the Output window (error locations will be clickable), or rely on the Clang IntelliSense to provide details on the errors.
  20. You can use the regular VisualGDB Project Properties window to edit various build and debug settings (e.g. preprocessor macros):settingsWarning: in order to avoid overwriting of your IAR-specific customizations (and conflicts with GCC-specific code), do not change any settings on the Embedded Project page and exercise caution when referencing embedded frameworks shipped with VisualGDB. Those frameworks were only tested with GCC and may require customizations in order to work with the IAR compiler.