简体   繁体   English

C编程:如何将gdb与Makefile和命令行参数一起使用?

[英]C programming: How to use gdb with Makefile and command line arguments?

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. 我甚至尝试使用up命令向上移动堆栈,但仍然没有运气。 I can't tell which line in my program is giving me the seg fault. 我不知道我的程序中的哪一行给了我seg错误。 gdb works with my other programs that do not involve a Makefile and command arguments, so I'm wondering if my commands are incorrect. gdb与我的其他不涉及Makefile和命令参数的程序一起工作,所以我想知道我的命令是否不正确。

Summarizing the comments (before anyone else does :). 总结评论(在别人做之前:)。

Your executable file is missing the symbolic information that gdb needs to display the relevant source code. 您的可执行文件缺少gdb需要显示相关源代码的符号信息。 You need to add the -g option to the compile command and produce a new executable. 您需要将-g选项添加到compile命令并生成新的可执行文件。 Then re-run your failing test to produce a new core file. 然后重新运行失败的测试以生成新的核心文件。 gdb with this executable and core will be able to show you the stack of function calls using backtrace . 具有此可执行文件和核心的gdb将能够使用backtrace向您显示函数调用的堆栈。

In a makefile, the easiest way to do this is to add (to) the CFLAGS variable which is used with the implicit .oc rule. 在makefile中,最简单的方法是添加(to)与隐式.oc规则一起使用的CFLAGS变量。

CFLAGS= -g -Wall -Wextra

You can also add this directly to the command-line (assuming a decent shell :). 您也可以将它直接添加到命令行(假设一个体面的shell :)。 This sets the value as an environment variable during the execution of the make command (and sub-commands). 这会在执行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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM