嵌入式c语言调试开关的技巧
在调试程序时,经常会用到assert和printf之类的函数,我最近做的这个工程里就有几百个assert,在你自认为程序已经没有bug的时候,就要除去这些调试代码,应为系统在正常运行时这些用于调试的信息是无用的,而且会占用时间和空间。怎么删除呢,以下仅供参考!
下面给出最简单的一种方法:
#define DEBUG
#ifdef DEBUG
#define PRINTF(x) printf x
#else
#define PRINTF(x) ((void)0)
#endif
使用时,PRINTF(( "Hello World!" ));
注意这里是两个括号,一个会报错的'
不使用时,直接将"#define DEBUG"屏蔽掉
另外一个调试时常用的方法是assert,还是在一个头文件里,这里用的是STM32函数库的例子
#ifdef DEBUG 1
/************************************************************
* Macro Name : assert_param
* Description : The assert_param macro is used for function's parameters check.
* It is used only if the library is compiled in DEBUG mode.
* Input : - expr: If expr is false, it calls assert_failed function
* which reports the name of the source file and the source
* line number of the call that failed.
* If expr is true, it returns no value.
* Return : None
************************************************************/
#define assert_param(expr) ((expr) ? (void)0 : assert_failed((u8 *)__FILE__, __LINE__))
/* Exported functions -------------------------------------*/
void assert_failed(u8* file, u32 line);
#else
#define assert_param(expr) ((void)0)
#endif/* DEBUG */
//assert_failed此函数要自己定义
#ifdef DEBUG
/************************************************************
* Function Name : assert_failed
* Description : Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* Input : - file: pointer to the source file name
* - line: assert_param error line source number
* Output : None
* Return : None
************************************************************/
void assert_failed(u8* file, u32 line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d", file, line) */
/* Infinite loop */
while (1){
}
}
#endif
-
C语言中friend友元函数详细解析
友元函数是可以直接访问类的私有成员的非成员函数。它是定义在类外的普通函数,它不属于任何类,但需要在类的定义中加以声明,声明时只需在友元的名称前加上关键字friend。我们已知道类具有封装和信息隐藏的特性。只有类的成员函数才能访问类的私有成员,程序中的其他...
-
C语言入门知识:位运算
导语:C语言作为一门非常适合编程入门的语言,打好基础的重要性不言而喻。下面是其位运算的相关知识,欢迎阅读:一、位运算符在计算机中,数据都是以二进制数形式存放的,位运算就是指对存储单元中二进制位的运算。C语言提供6种位运算符。二、位运算位运算符&|~<<>>∧...
-
C语言猴子吃桃问题
猴子第一天摘了若干个桃子,当即吃了一半,还不解馋,又多吃了一个;第二天,吃剩下的桃子的一半,还不过瘾,又多吃了一个;以后每天都吃前一天剩下的一半多一个,到第10天想再吃时,只剩下一个桃子了。问第一天共摘了多少个桃子?分析:1)设x1为前一天桃子数,设x2为第二天桃子数,则:x...
-
Swift与C语言指针结合使用实例
引导语:Swift可与Object-C共同运行于MACOS和iOS平台,用于搭建基于苹果平台的应用程序。以下是本站小编分享给大家的Swift与C语言指针结合使用实例,欢迎阅读!####用以输入/输出的参数指针C和Objective-C并不支持多返回值,所以CocoaAPI中常常将指针作为一种在方法间...