宏的高级使用--##,__VA_ARGS__, __FILE__, __FUNCTION__等

本文详细解释了C语言中宏变量如__VA_ARGS__、__FILE__、__LINE__、__FUNCTION__的用途及作用原理,并通过实例展示了宏连接符##的用法。同时介绍了编译过程的各个阶段,以及宏与C语言的不同处理方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



先说一下本文中会提到的内容:##,__VA_ARGS__, __FILE__, __LINE__ , __FUNCTION__等
宏变量:
先举一个例子,会用到上面这些宏:

[cpp]  view plain copy
  1. #define myprintf(...) printk("[lch]:File:%s, Line:%d, Function:%s," \  
  2.      __VA_ARGS__, __FILE__, __LINE__ ,__FUNCTION__);  

此处的 #define 的作用是将 myprintf( )换成后面那一大串的内容,而括号内 ... 的内容原样抄写在 __VA_ARGS__ 的位置。最终输出如下:

[lch]:File:arch/arm/mach-omap2/board-omap3wscec-camera.c, Line:163, Function:beagle_cam_init,camera init!

解析:

1)__VA_ARGS__:总体来说就是将左边宏中 ... 的内容原样抄写在右边 __VA_ARGS__ 所在的位置。它是一个可变参数的宏,是新的C99规范中新增的,目前似乎只有gcc支持(VC从VC2005开始支持)。要注意的是,printf 的输出格式是括号内左边是字符串,右边是变量,而且右变量与左输出格式是一一对应的。所以在上面那个例子中, __VA_ARGS__只能是一些不含任何变量的字符串常量。因为上面的例子中若__VA_ARGS__含有变量,整个printf的输出与变量便不能一一对应,输出会出错。

如果仅仅是替换函数名,可用如下方式,此时对__VA_ARGS__无任何特殊要求:#define myprintf(...) printk( __VA_ARGS__),在调试程序时可以这样用:

[cpp]  view plain copy
  1. #ifndef LOG_NDEBUG_FUNCTION  
  2. #define LOGFUNC(...) ((void)0)  
  3. #else  
  4. #define LOGFUNC(...) (printk(__VA_ARGS__))  
  5. #endif  
2) __FILE__ :宏在预编译时会替换成当前的源文件名
3) __LINE__:宏在预编译时会替换成当前的行号
4) __FUNCTION__:宏在预编译时会替换成当前的函数名称
5)类似的宏还有 __TIME__,__STDC__, __TIMESTAMP__等,就完全当一个变量来使用即可。

宏连接符##:
举个例子:宏定义为#define XNAME(n) x##n,代码为:XNAME(4),则在预编译时,宏发现XNAME(4)与XNAME(n)匹配,则令 n 为 4,然后将右边的n的内容也变为4,然后将整个XNAME(4)替换为 x##n,亦即 x4,故 最终结果为 XNAME(4) 变为 x4.
代码如下:
[cpp]  view plain copy
  1. #include <stdio.h>  
  2. #define XNAME(n) x ## n  
  3. #define PRINT_XN(n) printf("x" #n " = %d/n", x ## n);  
  4. int main(void)  
  5. {  
  6. int XNAME(1) = 14; // becomes int x1 = 14;  
  7. int XNAME(2) = 20; // becomes int x2 = 20;  
  8. PRINT_XN(1); // becomes printf("x1 = %d,", x1);  
  9. PRINT_XN(2); // becomes printf("x2 = %d/n", x2);  
  10. return 0;  
  11. }  
输出为:x1 = 14, x2 = 20


PS:编译过程:
1,扫描解析文件
2,预处理(宏在此时处理,该替换的文字会被替换)
3,对处理过的源代码进行汇编,输出汇编语言的代码(C语言的控制流程被处理)
4,编译为二进制目标文件
5,与程序库进行链接,输出最终的程序文件
(宏 和 C语言在不同的阶段处理执行)



http://blog.youkuaiyun.com/yiya1989/article/details/7849588



Rebuild started: Project: Project *** Using Compiler &#39;V6.22&#39;, folder: &#39;E:\Keil_v5\ARM\ARMCLANG\Bin&#39; Rebuild target &#39;Target 1&#39; assembling startup_stm32f10x_md.s... Start/core_cm3.c(445): error: non-ASM statement in naked function is not supported 445 | uint32_t result=0; | ^ Start/core_cm3.c(442): note: attribute is here 442 | uint32_t __get_PSP(void) __attribute__( ( naked ) ); | ^ Start/core_cm3.c(465): error: parameter references not allowed in naked functions 465 | "BX lr \n\t" : : "r" (topOfProcStack) ); | ^ Start/core_cm3.c(461): note: attribute is here 461 | void __set_PSP(uint32_t topOfProcStack) __attribute__( ( naked ) ); | ^ Start/core_cm3.c(479): error: non-ASM statement in naked function is not supported 479 | uint32_t result=0; | ^ Start/core_cm3.c(476): note: attribute is here 476 | uint32_t __get_MSP(void) __attribute__( ( naked ) ); | ^ Start/core_cm3.c(499): error: parameter references not allowed in naked functions 499 | "BX lr \n\t" : : "r" (topOfMainStack) ); | ^ Start/core_cm3.c(495): note: attribute is here 495 | void __set_MSP(uint32_t topOfMainStack) __attribute__( ( naked ) ); | ^ 4 errors generated. compiling core_cm3.c... compiling misc.c... compiling system_stm32f10x.c... compiling stm32f10x_adc.c... compiling stm32f10x_dac.c... compiling stm32f10x_exti.c... compiling stm32f10x_dbgmcu.c... compiling stm32f10x_dma.c... compiling stm32f10x_crc.c... compiling stm32f10x_cec.c... compiling stm32f10x_bkp.c... compiling stm32f10x_can.c... compiling stm32f10x_flash.c... compiling stm32f10x_pwr.c... compiling stm32f10x_fsmc.c... compiling stm32f10x_
03-31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值