C语言 百分网手机站

嵌入式c语言调试开关的技巧

时间:2020-09-28 17:47:32 C语言 我要投稿

嵌入式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语言调试开关的技巧】相关文章:

嵌入式C语言优化小技巧11-28

在C语言中用ASSERT调试的八个技巧11-12

C语言调试器是如何工作的10-04

轻松学习C语言的技巧11-16

c语言之符号的技巧10-31

关于C语言宏定义的技巧11-23

嵌入式Linux开发的C语言学习方法10-20

嵌入式C语言重点知识点11-25

C语言中do...while(0)技巧11-22