C语言 C 编程:如何使用带有 Makefile 和命令行参数的 gdb?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15260630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C programming: How to use gdb with Makefile and command line arguments?
提问by Bonnie
To create the .out executable, I have to enter:
要创建 .out 可执行文件,我必须输入:
$: make
$: myprogram.out name.ged
My program incorporates a command line argument, thus the "name.ged".
我的程序包含一个命令行参数,因此是“name.ged”。
Whenever I run gdb after getting a segmentation fault (core dumped), I enter:
每当我在遇到分段错误(核心转储)后运行 gdb 时,我都会输入:
$: gdb a.out core
(gdb): bt
I then use the back trace command, and gdb returns:
然后我使用 back trace 命令,gdb 返回:
#0 0x4a145155 in ?? ()
#1 0x08a16ce0 in ?? ()
I even tried using the up command t move up the stack, but still no luck. I can't tell which line in my program is giving me the seg fault. gdb works with my other programs that do not involve a Makefile and command arguments, so I'm wondering if my commands are incorrect.
我什至尝试使用 up 命令 t 向上移动堆栈,但仍然没有运气。我不知道程序中的哪一行给了我段错误。gdb 与我的其他不涉及 Makefile 和命令参数的程序一起工作,所以我想知道我的命令是否不正确。
回答by luser droog
Summarizing the comments (before anyone else does :).
总结评论(在其他人之前:)。
Your executable file is missing the symbolic information that gdb needs to display the relevant source code. You need to add the -goption to the compile command and produce a new executable. Then re-run your failing test to produce a new core file. gdbwith this executable and core will be able to show you the stack of function calls using backtrace.
您的可执行文件缺少 gdb 显示相关源代码所需的符号信息。您需要将-g选项添加到编译命令并生成一个新的可执行文件。然后重新运行失败的测试以生成新的核心文件。带有此可执行文件和核心的gdb将能够使用backtrace.
In a makefile, the easiest way to do this is to add (to) the CFLAGSvariable which is used with the implicit .o.c rule.
在 makefile 中,最简单的方法是添加(到)CFLAGS与隐式 .oc 规则一起使用的变量。
CFLAGS= -g -Wall -Wextra
You can also add this directly to the command-line (assuming a decent shell :). This sets the value as an environment variable during the execution of the makecommand (and sub-commands).
您也可以将其直接添加到命令行(假设有一个不错的 shell :)。这会在make命令(和子命令)执行期间将该值设置为环境变量。
$ CFLAGS='-g -Wall -Wextra' make
I'd actually recommend you add this to your bash .profile, so you always get the most information from the compiler.
我实际上建议您将其添加到 bash .profile 中,以便您始终从编译器中获得最多的信息。
CFLAGS='-Wall -Wextra'
Then, when you need it, put this in the makefile to make a debuggable executable:
然后,当你需要它时,把它放在 makefile 中以制作一个可调试的可执行文件:
CFLAGS+= -g

