0

I'm trying to make a program. That's printing error from C code through gcc compiler. When Compile error happens, it makes text file which has error messages in. But when runtime error happens. For example, 'segmentation fault'. The file is empty. It shows segmentation fault well on terminal, but it doesn't show the errors in the file.

I tried to type below few commands but it still doesn't work.

gcc new.c &> myFile
gcc new.c > myFile 2>&1
MANOJ GOPI
  • 1,279
  • 10
  • 31
Chase Choi
  • 882
  • 11
  • 25

3 Answers3

0

this should put the error in a file properly :

gcc new.c -o new.x >& error.log
E.C
  • 27
  • 6
0

I think you need core dump file but not catch the run-time error of gcc

I show you how to get core dump under Linux, I wrote a test program, 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;
}

Normally, I will do following step before compile:

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)

after this, it will generate core dump file for you:

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

and you can know use gdb or whatever debug tool you like to see:

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)

you can find where you program trap.

How Chen
  • 1,340
  • 2
  • 17
  • 37
  • Thank you for your answer. This is a good way! One more question, can you extract the result of 'gdb ./a.out ./core' to file? Thanks a lot!! – Chase Choi Jan 14 '15 at 04:15
  • @user3422425, i did not try this, because when I run `gdb ./a.out ./core`, `gdb1 is keep runing. I try `gdb ./a.out ./core > log` but shell pending on waiting input for `gdb`, I can input a `q` to quit `gdb`, and then you can find above log in your log file. However, the `core` file already contain all the message you need and I think it is the LOG:), but you need `gdb` to decode. – How Chen Jan 14 '15 at 05:46
  • Awesome! I think the pending problem, we could just kill the task. Thank you again. – Chase Choi Jan 14 '15 at 06:00
  • @user3422425, no need to use `kill`, try to find a way, like write in shell script to automatic input the `Q` for `gdb`, can can ask in linux form, I just show you some idea:) or you may find `gdb` have some options to output this log file:) – How Chen Jan 14 '15 at 06:01
0

gcc compiles the program. Redirecting gcc's output will not help you with any errors that occur when your program runs after it was successfully compiled.

To run your program and redirect your program's stderr to a file, you can write ./yourprogramname 2>file.txt.

However, the Segmentation fault message is not generated by your program either. It is generated by the operating system and it is printed on the shell's stderr (not your program's stderr). To redirect this message it depends on your shell, see this question.

Community
  • 1
  • 1
M.M
  • 138,810
  • 21
  • 208
  • 365