要想快速定位问题,掌握一些编译器的小技巧是很必要的。在此收集了一些常用的。
主要参考了https://github.com/hellogcc/100-gcc-tips/blob/master/src/index.md。
详情可见官网。
1.打印gcc预定义的宏信息
gcc -dM -E - < /dev/null
…
输出列表很长,没有显示全,可配合grep 查找想要的预定义值。
2.打印头文件搜索路径
gcc -v test.cpp
此命令可显示gcc的版本号、头文件路径等一些组件的版本号。也可使用gcc -### test.cpp .
3.添加预定义宏
gcc -D DEBUG test.cpp或者gcc -DDEBUG test.cpp
常用做编译宏调试分支代码。
test.cpp
#include<stdio.h>
int main()
{
#ifdef DEBUG
printf("this is debug!\n");
#endif // DEBUG
printf("hello world!\n");
return 0;
}
gcc -o test -D DEBUG test.cpp
./test输出:
this is debug!
hello world!
同理gcc -o test -U DEBUG test.cpp可用来取消预定义宏。
未完待续。。。