C MEX Bug: String Preprocessor Concatenation Not Working?
Show older comments
I am working on an STM32 Simulink library for a host of projects. The user #defines the chipset they are working on when they reference the library, and the library #includes the right files. There are a number of S-Functions that have been created, and they rely on the STM32 firmware packages to access the microcontroller's various peripherals. Let me give an example:
In stm32_config.h:
#define STM32_CHIPSET stm32l4
#define STM32_CHIP stm32l433
In our library's S-Function wrapper, say "ADC_StartConversion_wrapper.c":
#include "stm32_config.h"
#include "../STM32_Drivers/" #STM32_CHIPSET "/HAL_Driver/Inc/" #STM32_CHIPSET "xx_hal_adc.h"
The above is perfectly legal C code. The STM32_Drivers folder referenced contains every chipset we have implemented for our library, and everything underneath it is the same folder structure exactly as the files are downloaded from ST Microelectronics' website. The ability to use macros as part of the #include directive is called "Computed Includes" and is written about in the gcc online manual. The C preprocessor takes adjacent strings and concatenates them at compile time, and the #[macro] stringizes it. In a regular C compiler, this would automatically get translated to:
#include "../STM32_Drivers/stm32l4/HAL_Driver/Inc/stm32l4xx_hal_adc.h"
But when I try to build the mex file
mex ADC_StartConversion.c ADC_StartConversion_wrapper.c
I get the error "fatal error C1083: Cannot open include file: '../STM32_Drivers/': No such file or directory".
If I try to either use a helper macro so the string gets expanded early, or break the string manually into multiple quotes, like this:
#define ADC_INC_HELPER "../STM32_Drivers/" #STM32_CHIPSET "/HAL_Driver/Inc/" #STM32_CHIPSET "xx_hal_adc.h"
#define ADC_INC ADC_INC_HELPER
#include ADC_INC
...
#include "../STM32_Drivers/" "stm32l4" "/HAL_Driver/Inc/" "stm32l4" "xx_hal_adc.h"
same error. Only when I make it a single quote
#include "../STM32_Drivers/stm32l4/HAL_Driver/Inc/stm32l4xx_hal_adc.h"
does it work.
So basically, is this a bug or a feature oversight by MATLAB? Is there a way to accomplish what I want other than to make a separate header file that has a million #ifdef switches for every STM32 chip we support?
Accepted Answer
More Answers (0)
Categories
Find more on Simulink Coder in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!