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.
- Start Visual Studio and open the VisualGDB Embedded Project Wizard:
- Select “Automatically import a project in a different format” -> “Import an existing IAR Project”, then specify the path to the project you are importing:As 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.
- 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:
- Finally plug in your JTAG/SWD programmer and select it on the Debug Method page:
- 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:
- 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”:Note that you don’t need to change the paths in NMakeIncludeSearchPath as it is only used by IntelliSense.
- 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:
- 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:
- 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.
1"C:\Program Files (x86)\IAR Systems\Embedded Workbench 8.0\common\bin\IarBuild.exe" n:\demo\Project.ewp -build STM32F7-DISCO -log all > log.txt
Once the build is complete, open the log file and locate 3 types of command lines:
- Assembler command line (iasmarm.exe)
- C/C++ compiler command line (iccarm.exe)
- Linker command line (ilinkarm.exe)
In this example the command lines look as follows:
123456789101112131415iasmarm.exe <file.s> -O<output> -s+ -M<> -w+ -r --cpu Cortex-M7 --fpu VFPv5_sp -In:\demo\..\Config\iccarm.exe <file.c> -D STM32F746xx -D USE_HAL_DRIVER -D USE_STM32746G_DISCOVERY-o <output directory> --debug --endian=little --cpu=Cortex-M7 -e --fpu=VFPv5_sp--dlib_config <IAR directory>\INC\c\DLib_Config_Full.h<include paths from project properties>-Ohz --use_c++_inline-I <IAR directory>\CMSIS\Core\Include\-I <IAR directory>\CMSIS\DSP\Include\ilinkarm.exe <object files> --redirect _Printf=_PrintfFull-o <ELF file>--map <map file>--config n:\demo\stm32f746g.icf--semihosting --entry __iar_program_start --vfe --text_out locale
- 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:
1234567IAR_ROOT := c:\iar\armCC := $(IAR_ROOT)/bin/iccarm.exeASM := $(IAR_ROOT)/bin/iasmarm.exeCXX := $(CC)LD := $(IAR_ROOT)/bin/ilinkarm.exeAR := $(TOOLCHAIN_ROOT)/bin/arm-eabi-ar.exeOBJCOPY := $(TOOLCHAIN_ROOT)/bin/arm-eabi-objcopy.exe
- Copy the assembler, compiler and linker flags from the build log to the corresponding lines in stm32.mak:
123456789IARFLAGS := --debug --endian=little --cpu=Cortex-M7 -e --fpu=VFPv5_sp --dlib_config $(IAR_ROOT)\INC\c\DLib_Config_Full.h --use_c++_inlineINCLUDE_DIRS += $(IAR_ROOT)/CMSIS/Core/Include $(IAR_ROOT)/CMSIS/DSP/IncludeCFLAGS += $(IARFLAGS)CXXFLAGS += $(IARFLAGS)ASFLAGS += -s+ "-M<>" -w+ -r --cpu Cortex-M7 --fpu VFPv5_sp -In:\demo\..\ConfigLDFLAGS += --redirect _Printf=_PrintfFull --semihosting --entry __iar_program_start --vfe --text_out localeCOMMONFLAGS +=LINKER_SCRIPT := n:\demo\stm32f746g.icf
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.
- Open the debug.mak file:This 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.
- 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:
- 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:This 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).
- Now we will update the Makefile to handle IAR-specific settings. First, replace the syntax for the linker script:
123ifneq ($(LINKER_SCRIPT),)LDFLAGS += --config $(LINKER_SCRIPT)endif
Then add IAR_CFLAGS to CFLAGS (and similar to CXXFLAGS) before the line adding INCLUDE_DIRS:
12CFLAGS += $(IAR_CFLAGS)CXXFLAGS += $(IAR_CXXFLAGS)
- Finally change the syntax for generating dependency files and ensure that $(ASM) instead of $(CC) is called for assembly files:
123456789101112131415161718#VisualGDB: FileSpecificTemplates #<--- VisualGDB will use the following lines to define rules for source files in subdirectories$(BINARYDIR)/%.o : %.cpp $(all_make_files) |$(BINARYDIR)$(CXX) $(CXXFLAGS) -c $< -o $@ --dependencies=m $(@:.o=.dep)$(BINARYDIR)/%.o : %.c $(all_make_files) |$(BINARYDIR)$(CC) $(CFLAGS) -c $< -o $@ --dependencies=m $(@:.o=.dep)$(BINARYDIR)/%.o : %.S $(all_make_files) |$(BINARYDIR)$(ASM) $(ASFLAGS) -c $< -o $@$(BINARYDIR)/%.o : %.s $(all_make_files) |$(BINARYDIR)$(ASM) $(ASFLAGS) -c $< -o $@$(BINARYDIR)/%.o : %.cc $(all_make_files) |$(BINARYDIR)$(CC) $(CFLAGS) $(CXXFLAGS) -c $< -o $@ --dependencies=m $(@:.o=.dep)$(BINARYDIR)/%.o : %.cxx $(all_make_files) |$(BINARYDIR)$(CC) $(CFLAGS) $(CXXFLAGS) -c $< -o $@ --dependencies=m $(@:.o=.dep)
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.
- Now you can build the project by pressing Ctrl-Shift-B. The build should succeed as if it was a regular GCC-based project:GNU 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.
- 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:
- One last step would be to tweak VisualGDB to recognize the error messages produced by IAR. You can do this globally by editing the GCCMessage regular expression in the Rules\RegularExpressions.xml file in the VisualGDB directory:
1<GCCMessage>^"([^:]+:[^:]+)",([0-9]+)[ ]+(Error|Warning|Fatal error)(.*)$</GCCMessage>
Alternatively you can override the rules for a specific project by copying the regexes into the BuildMessageRegexes.xml file in your project directory:
1234<?xml version="1.0" encoding="UTF-8"?><BuildMessageRegexes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><!-- Copy the children of the BuildMessages element in Rules\RegularExpressions.xml here --></BuildMessageRegexes>
Then reference the BuildMessageRegexes.xml file from your .vgdbsettings file:
12345<?xml version="1.0"?><VisualGDBProjectSettings2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><BuildMessageTemplateFile>BuildMessageRegexes.xml</BuildMessageTemplateFile>...</VisualGDBProjectSettings2>
This will let VisualGDB properly recognize the IAR error/warning messages and display them in the Errors window:Note 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.
- You can use the regular VisualGDB Project Properties window to edit various build and debug settings (e.g. preprocessor macros):Warning: 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.