打印

IAR EWARM 7.8 使用 __attribute__ 示例

[复制链接]
3044|5
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
wangjiahao88|  楼主 | 2016-11-25 20:49 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include <stdint.h>
uint32_t VarInSection __attribute__ ( (section (".Var")) );
uint8_t AlignedArray[ 128 ] __attribute__( ( aligned( 128 ) ) );
typedef struct
{
  uint8_t Var8a;
  uint16_t Var16a __attribute__( ( aligned( 8 ) ) );
  uint32_t Text[ 4 ];
} AlignedMember_TypeDef;
AlignedMember_TypeDef AlignedMember;
typedef struct __attribute__( (packed) )
{
  uint8_t Var8a;
  uint16_t Var16a;
  uint32_t Text[ 4 ];
} PackedStruct_TypeDef;
PackedStruct_TypeDef PackedStruct;
typedef struct
{
  uint8_t Var8a;
  uint16_t Var16a;
  uint32_t Text[ 4 ];
} __attribute__( (packed) ) PackedStruct2_TypeDef;
PackedStruct2_TypeDef PackedStruct2;
typedef struct __attribute__( (packed,  aligned( 128 ) ) )
{
  uint8_t Var8a;
  uint16_t Var16a;
  uint32_t Text[ 4 ];
} AlignedPackedStruct_TypeDef;
AlignedPackedStruct_TypeDef AlignedPackedStruct;
void Foo( void ) __attribute__ ( (section (".Foo")) );
void Foo( void )
{
  VarInSection = 0;
  AlignedArray[ 0 ] = 0;
  PackedStruct.Text[ 0 ] = 1;
  PackedStruct2.Text[ 0 ] = 1;
  AlignedMember.Var16a = 0;
  AlignedPackedStruct.Text[ 0 ] = 1;
}
// .textrw and .text
__root __ramfunc void ramfunc0(void)
{
}
// No effect : .ramfunc
__root void ramfunc1(void) __attribute__ ( (section(".ramfunc") ) );
__root void ramfunc1(void)
{
  ramfunc0();
}

相关帖子

沙发
wangjiahao88|  楼主 | 2016-11-25 20:50 | 只看该作者
*******************************************************************************
*** PLACEMENT SUMMARY
***
"A0":  place at 0x00000000 { ro section .intvec };
"P1":  place in [from 0x00000000 to 0x0007ffff] { ro };
"P3":  place in [from 0x00000000 to 0x00000000] |
                [from 0x20000000 to 0x2000ffff] {
          rw, block CSTACK, block PROC_STACK, block HEAP };
initialize by copy { rw };
  Section               Kind        Address    Size  Object
  -------               ----        -------    ----  ------
"P1":                                        0x13a8
  .ramfunc              ro code  0x0000133c     0x4  __attribute__.o [1]
  .Foo                  ro code  0x00001218    0x2c  __attribute__.o [1]
"P3", part 1 of 3:                             0xb0
    .textrw             inited   0x200000ac     0x2  __attribute__.o [1]
"P3", part 2 of 3:                           0x142c
  .bss                  zero     0x20001280    0x80  __attribute__.o [1]
  .bss                  zero     0x20001300    0x80  __attribute__.o [1]
  .bss                  zero     0x200014c4    0x14  __attribute__.o [1]
  .Var                  zero     0x200014d8     0x4  __attribute__.o [1]
                               - 0x200014dc  0x142c
*******************************************************************************
*** ENTRY LIST
***
Entry                      Address    Size  Type      Object
ramfunc1                0x0000133d     0x4  Code  Gb  __attribute__.o [1]
ramfunc0                0x200000ad     0x2  Code  Gb  __attribute__.o [1]
AlignedArray            0x20001280    0x80  Data  Gb  __attribute__.o [1]
AlignedPackedStruct     0x20001300    0x80  Data  Gb  __attribute__.o [1]
Foo                     0x00001219    0x2c  Code  Gb  __attribute__.o [1]
PackedStruct            0x200014c4    0x14  Data  Gb  __attribute__.o [1]
VarInSection            0x200014d8     0x4  Data  Gb  __attribute__.o [1]

使用特权

评论回复
板凳
原味_郭| | 2016-11-27 14:32 | 只看该作者
__attribute__ 可以用来干嘛呢,这点有点不懂

使用特权

评论回复
地板
ahhsyf| | 2016-11-27 14:43 | 只看该作者
__attribute__ 好像是断言的意思,不过从没在程序中用过。

使用特权

评论回复
5
wangjiahao88|  楼主 | 2016-11-27 20:38 | 只看该作者
一般 都写在 main函数里面

我之前也从来没遇到过

使用特权

评论回复
6
enginezhong| | 2016-11-28 09:08 | 只看该作者
ahhsyf 发表于 2016-11-27 14:43
__attribute__ 好像是断言的意思,不过从没在程序中用过。

GNU C 的一大特色就是__attribute__ 机制。__attribute__ 可以设置函数属性(Function Attribute )、变量属性(Variable Attribute )和类型属性(Type Attribute )。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

473

主题

7517

帖子

30

粉丝