问题描述
我正在将一个项目从 CubeIDE (GCC) 迁移到针对基于 Arm® 的微控制器的最全面的软件开发解决方案 uVision(ARM 编译器 5)并且在使用 __align
关键字时遇到困难.
在 CubeIDE 中编译良好的 CubeIDE 代码:
#include <stdalign.h>
typedef struct {
volatile alignas(uint32_t) uint16_t imageDark[320];
} test_results;
第一个问题是 uVision 无法定位
..\Src\device_tests.c(3): error: #5: cannot open source input file "stdalign.h": No such file or directory
所以我删除了
typedef struct {
volatile __align(__ALIGNOF__(uint32_t)) uint16_t imageDark[320];
} test_results;
现在我收到以下错误:
..\Src\device_tests.c(46): error: #80: a storage class may not be specified here
volatile __align(__ALIGNOF__(uint32_t)) uint16_t imageDark[320];
..\Src\device_tests.c(46): error: #328: invalid storage class for a class member
volatile __align(__ALIGNOF__(uint32_t)) uint16_t imageDark[320];
任何有关如何在 uVision 中进行对齐工作的帮助将不胜感激。
解决方法
我发现问题在于我试图在结构内对齐。上述语法适用于结构外的变量。但是为了让它在 struct 内工作,我使用了以下语法:
typedef struct {
volatile uint16_t imageDark[320] __attribute__((aligned (4)));
} test_results;