我试图用IAR嵌入式工作台编译libvpx库(由google编写的webm解码器),用于ARM-A7 (裸金属应用程序)。
我成功地输入了所有必需的文件,并进行了编译,但是一些变量的数据对齐存在问题。
在库中,有一个宏DATA_ALIGNMENT(),它扩展到GNUC __attribute__(对齐(N))预处理指令。我想我设法让这个宏与IAR版本的数据对齐(实用化数据对齐)一起工作,但是我得到了以下警告
“警告Pe609:这种实用主义在这里可能不会被使用”
当我运行代码时,我的变量没有对齐!
当在互联网上搜索警告时,他们说你不能在定义变量时使用语用,但只有在创建某种变量时才能使用。然而,对于数据对齐,在定义结构时需要这样做( GCC确实允许这样做,那么为什么不允许IAR呢?)
任何帮助都将不胜感激!
码
宏观定义:
#if (defined(__GNUC__) && __GNUC__) || defined(__SUNPRO_C)
#define DECLARE_ALIGNED(n, typ, val) typ val __attribute__((aligned(n)))
#elif defined(__ICCARM__)
#define CONCAT(a,b) a##b
#define DECLARE_ALIGNED(n, typ, val) CONCAT(DECLARE_ALIGNED_,n) (typ,val)
#define DECLARE_ALIGNED_1(typ, val) _Pragma("data_alignment=1") typ val
#define DECLARE_ALIGNED_8(typ, val) _Pragma("data_alignment=8") typ val
#define DECLARE_ALIGNED_16(typ, val) _Pragma("data_alignment=16") typ val
#define DECLARE_ALIGNED_32(typ, val) _Pragma("data_alignment=32") typ val
#define DECLARE_ALIGNED_256(typ, val) _Pragma("data_alignment=256") typ val
#else
#warning No alignment directives known for this compiler.
#define DECLARE_ALIGNED(n, typ, val) typ val
#endif使用的示例:
typedef struct VP9Decoder {
DECLARE_ALIGNED(16, MACROBLOCKD, mb);
DECLARE_ALIGNED(16, VP9_COMMON, common);
int ready_for_new_data;
int refresh_frame_flags;
...
} VP9Decoder;发布于 2017-03-15 08:55:13
我已经在我的IAR编译器(7.40.6)中直接尝试过这一点,它工作得很好:
#define CONCAT(a,b) a##b
#define DECLARE_ALIGNED(n, typ, val) CONCAT(DECLARE_ALIGNED_,n) (typ,val)
#define DECLARE_ALIGNED_8(typ, val) _Pragma("data_alignment=8") typ val
typedef struct
{
int a;
char b;
char pad1;
char pad3;
char pad4;
int c;
char d;
} myType;
void main( void)
{
DELCARE_ALIGNED_4( myType, data);
// So data.a will be aligned to a 4 byte boundary
// data.b will be aligned to four bytes
// data.pad, pad1, pad2 are wasted space.
// data.c will be aligned to four bytes
// data.d will be aligned to four bytes
}除非您需要您的结构在特定的顺序,例如映射到某物,然后仔细排序您的结构可以减少其大小。例如,我在本例中插入的填充将由编译器插入。int a, int c, char b, char d.的顺序会更好,因为由于填充和对齐,原来的结构可能有16个字节长。而它只能是12。
https://stackoverflow.com/questions/42722565
复制相似问题