【问题标题】:How to print gcc runtime error to text file?如何将 gcc 运行时错误打印到文本文件?
【发布时间】:2015-01-13 08:14:43
【问题描述】:

我正在尝试制作一个程序。那是通过 gcc 编译器从 C 代码打印错误。 当发生编译错误时,它会生成包含错误消息的文本文件。但是当发生运行时错误时。例如,“分段错误”。该文件为空。 它在终端上很好地显示了分段错误,但没有显示文件中的错误。

我尝试在下面键入几个命令,但它仍然不起作用。

gcc new.c &> myFile
gcc new.c > myFile 2>&1

【问题讨论】:

  • 您说的是 GCC 本身的运行时错误?
  • 我认为,gcc 为编译错误创建了一个文件。但是,运行时错误....
  • 好吧,我不知道如何让 GCC 抛出一个 seg-fault,所以恐怕这很难重现!
  • 你需要什么错误?你的意思是gcc在编译期间崩溃?或者你的程序有segmentation fault?在gcc 编译期间遇到segmentation fault 时,您能举一些例子吗?
  • 我想通过 gcc 在编译期间或之后捕获运行时错误和记录。例如,当我们访问错误的数组索引时,编译器不会捕获错误访问。

标签: c gcc


【解决方案1】:

我认为您需要核心转储文件,但没有捕捉到gcc 的运行时错误

我教你如何在Linux下获取core dump,我写了一个测试程序,test_coredump.c

#include <stdlib.h>

int main(void){
    int *ptr = NULL;
    *ptr = 10;  //Segmentation fault will happen since you write to a null memory

    return 0;
}

通常,我会在编译前执行以下步骤:

how@ubuntu-sw:~/Work/c/test_coredump
-> ulimit -c
0
how@ubuntu-sw:~/Work/c/test_coredump
-> ulimit -c unlimited
how@ubuntu-sw:~/Work/c/test_coredump
-> ulimit -c
unlimited
how@ubuntu-sw:~/Work/c/test_coredump
-> gcc -g ./test_coredump.c
how@ubuntu-sw:~/Work/c/test_coredump
-> ls
a.out  test_coredump.c
how@ubuntu-sw:~/Work/c/test_coredump
-> ./a.out 
Segmentation fault (core dumped)

之后,它会为你生成核心转储文件:

how@ubuntu-sw:~/Work/c/test_coredump
-> ls
a.out  core  test_coredump.c

你可以知道使用gdb 或任何你喜欢的调试工具:

how@ubuntu-sw:~/Work/c/test_coredump
-> gdb ./a.out ./core 
GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./a.out...done.
[New LWP 6421]
Core was generated by `./a.out'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x080483fd in main () at test_coredump.c:5
5       *ptr = 10;  //Segmentation fault will happen since you write to a null memory
(gdb)

你可以找到你编写陷阱的地方。

【讨论】:

  • 感谢您的回答。这是个好方法!还有一个问题,你能把'gdb ./a.out ./core'的结果提取到文件中吗?非常感谢!!
  • @user3422425,我没有尝试这个,因为当我运行gdb ./a.out ./coregdb1 is keep runing. I try gdb ./a.out ./core > log`但shell等待gdb的输入,我可以输入q退出gdb,然后你可以在你的日志文件中找到上面的日志。但是,core 文件已经包含您需要的所有消息,我认为它是 LOG:),但您需要 gdb 来解码。
  • 太棒了!我认为悬而未决的问题,我们可以直接终止任务。再次感谢您。
  • @user3422425,不用kill,想办法,比如写shell脚本自动输入Qgdb,可以用linux的形式问,我只是告诉你一些想法:) 或者你可能会发现gdb 有一些选项可以输出这个日志文件:)
【解决方案2】:

这应该将错误正确地放入文件中:

gcc new.c -o new.x >& error.log

【讨论】:

    【解决方案3】:

    gcc 编译程序。重定向 gcc 的输出不会帮助您解决程序在成功编译后运行时发生的任何错误。

    要运行您的程序并将程序的stderr 重定向到一个文件,您可以编写./yourprogramname 2&gt;file.txt

    但是,Segmentation fault 消息也不是由您的程序生成的。它由操作系统生成,并打印在 shell 的 stderr(不是您的程序的标准错误)上。要重定向此消息,它取决于您的 shell,请参阅 this question

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-07
      • 2018-08-13
      • 2021-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-25
      • 1970-01-01
      相关资源
      最近更新 更多