调试与纠错
///////////////////debug.c //////////////////
#include<stdio.h>
#include<stdlib.h>
int main(){
#ifdef DEBUG
printf("compiled " __DATE__ " at " __TIME__ "\n");
#endif
printf("Hello World\n");
exit(0);
}
编译时,如果不用DEBUG,则gcc -o debug debug.c
如果要用DEBUG,就得在命令中define,方法就是gcc -o debug -DDEBUG debug.c
使用强大的gdb
1.启动 首先在gcc时加入选项-g
然后 gdb filename
进入gdb的命令行
2.有很多命令,比如
run,list,print,break,
使用assert宏
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include<math.h>
double my_sqrt(double x){
assert(x>=0);
return sqrt(x);
}
int main(){
#ifdef DEBUG
printf("compiled " __DATE__ " at " __TIME__ "\n");
#endif
printf("Hello World\n");
printf("sqrt of +2 = %g\n",my_sqrt(2.0));
printf("sqrt of -2 = %g\n",my_sqrt(-2.0));
exit(0);
}
没有评论:
发表评论