cjones wrote:I'm compiling with gcc and with -O2 optimisation.
Hi cjones,
You can debug your program by compiling with the debug flag and stepping through it with GDB. I wouldn't use -O2 optimization until the program is working, it will strip out data that would be useful for the debugger.
Like:
gcc -o roombacontrol -g roombacontrol.c
Then run gdb with:
gdb roombacontrol
This will load in your executable and the source file so you can go through the program step by step to see where it's faulting.
Then at the gdb command prompt type:
break main
run
You will see something like this:
(gdb) break main
Breakpoint 1 at 0x2a30: file proc_sonar_sample.c, line 86.
(gdb) run
Starting program: /Users/armadilo/projects/rklabs/roomba/proc_sonar_sample
Reading symbols for shared libraries . done
Breakpoint 1, main (argc=1, argv=0xbffffb30) at proc_sonar_sample.c:86
86 if (argc < 2) {
(gdb) next
87 printf("proc_sonar:Please supply a coordinate list file\n");
(gdb)
(This example is from another program I've been working on but you should get the idea)
So just keep typing "next" until you get to the point where the segfault happens. Your bug is right there.
GDB is a pretty useful tool for seeing where bugs are in a program and you can learn a lot from stepping through your program and seeing how it works.
HTH
RK