IAR中常用的 #pragma 命令和扩展关键字

pragma 命令

1、#pragma message(“ ”) 编译器编译到此处,在Build窗口中打印相应文本信息。

2、#pragma error “” 编译器编译到此处,在Build窗口中产生错误并打印其文本信息。

3、#pragma inline [=forced | never] 用这个指令是建议编译将这条指令后面的函数内联到调用它的函数的函数体中去。 当#pragma inline = forced,则强制让编译器对函数内联,,如果内联不成功,会发出警告消息。

4、#pragma location = {address | register | NAME} 该指令用处 1)定位该指令之后的全局或静态变量到指定的absolute address上。其中变量必须定义为__no_init============address(地址) pragma location = address 等价于 @ address,其中变量也必须定义为__no_init 。 示例: __no_init volatile char alpha @ 0xFF2000 运用绝对地址定位,还需注意地址的对齐问题。

2)定位该指令之后的变量到指定寄存器中,其中该变量必须声明为__no_init,同时该变量的作用域为整个文件。 =============register(寄存器R4-R11) pragma location = register 等价于 @ register,其中变量也必须定义为__no_init

3)将该指令之后的函数或变量放置到一个指定的section中。其中,不要试图将那些通常放在不同section的变量放置在同一section中。 =============NAME(A user-defined section name; cannot be a section name predefined for use by the compiler and linker. pragma location = section 等价于 @ section

示例 变量放置在自定义的section中。 __no_init int alpha @ “MY_NOINIT”;/* OK */ #pragma location=”MY_CONSTANTS” const int beta;/* OK */

函数放置在自定义section中。 void f(void) @ “MY_FUNCTIONS”; void g(void) @ “MY_FUNCTIONS” { } #pragma location=”MY_FUNCTIONS” void h(void);

5、#pragma required = symbol #pragma required确保链接输出中包括某个符号所需的另一个符号。该指令必须放在紧邻第二个符号的前边。如果符号在应用中不可见,使用该指令。例如,如果仅通过某个变量所在的段对其进行间接引用,必须使用#pragma required。 eg: const char copyright[] = “Copyright by me”; #pragma required=copyright int main() { /* Do something here. */ } Even if the copyright string is not used by the application, it will still be included by the linker and available in the output.

扩展关键字 __no_init 正常情况下,应用程序启动时,IAR运行时环境将全部全局和静态变量初始化为0。IAR C编译器支持声明不初始化的变量,使用__no_init类型限定符。声明__no_init的变量不需初始化。一些关键数据在系统复位(如看门狗复位或其他原因造成的复位)时的数值是不能改变的!在这种情况下可用__no_init限定。

__root __root限定的函数和变量在即是没有被任何函数引用的情况下,它依然存在于目标代码中而不会被优化掉。

__task 被该关键字限定的函数在被调用时不会做寄存器保护,即没有寄存器入栈出栈操作。通常用在RTOS的启动函数中。 By default, functions save the contents of used preserved registers on the stack upon entry, and restore them at exit. Functions that are declared __taskdo not save all registers, and therefore require less stack space. Because a function declared __taskcan corrupt registers that are needed by the calling function, you should only use __taskon functions that do not return or call such a function from assembler code. The function maincan be declared __task, unless it is explicitly called from the application. In real-time applications with more than one task, the root function of each task can be declared __task. 在IAR中实验了一下,发现编译器还是将寄存器进行了压栈操作,不知为何?

往往教导我们大家要好好学习天天向上,要永不言弃坚持到底百折不挠宁死不屈,

IAR中常用的 #pragma 命令和扩展关键字

相关文章:

你感兴趣的文章:

标签云: