Creating Debug-Ready Code

来源:互联网 发布:c语言多个文件编写 编辑:程序博客网 时间:2024/05/09 07:46
 

Normally, when we write a program, we want to be able to debug it - that is, test it using a debugger that allows running it step by step, setting a break point before a given command is executed, looking at contents of variables during program execution, and so on. In order for the debugger to be able to relate between the executable program and the original source code, we need to tell the compiler to insert information to the resulting executable program that'll help the debugger. This information is called "debug information". In order to add that to our program, lets compile it differently:

cc -g single_main.c -o single_main

The '-g' flag tells the compiler to use debug info, and is recognized by mostly any compiler out there. You will note that the resulting file is much larger than that created without usage of the '-g' flag. The difference in size is due to the debug information. We may still remove this debug information using the strip command, like this:

strip single_main

You'll note that the size of the file now is even smaller than if we didn't use the '-g' flag in the first place. This is because even a program compiled without the '-g' flag contains some symbol information (function names, for instance), that thestrip command removes. You may want to read strip's manual page (man strip) to understand more about what this command does.


原创粉丝点击