Home » Testing and Debugging » Using GDB to debug functions defined in another file in C

Using GDB to debug functions defined in another file in C

In our previous post “How to use gdb for debugging application programs ( On Ubuntu Linux using C program )” we seen how we can debug the C program where all of the code is written in single C file.

In this post, we will show you how you can debug the C function written in another file which is linked with main program and debug the function in that another file.

Lets first create one addition program,

$ vim add.c
int add_two_numbers(int a, int b) {
        return (a+b);
}

Now, we will write the main program which calls the “add_two_numbers” function as,

$ vim helloworld.c
#include <stdio.h>

extern int add_two_numbers(int, int);

int main(int argc, char **argv) {
        int num = 0;
        printf("helloworld\n");
        num = add_two_numbers(3, 2);
        printf("addition: %d\n", num);
        return 0;
}

Now, lets compile this two program and link to create a executable as,

$ gcc -g -c  add.c
$ gcc -g -o helloworld helloworld.c add.o

As you can see above, we have added “-g” so that the debug symbols are added with executable.

Now, we will run this program with gdb,

$ gdb ./helloworld

Reading symbols from ./helloworld...

Now, we will check the lines in this program using “li” command on gdb console as,

(gdb) li
1	#include <stdio.h>
2	
3	extern int add_two_numbers(int, int);
4	
5	int main(int argc, char **argv) {
6		int num = 0;
7		printf("helloworld\n");
8		num = add_two_numbers(3, 2);
9		printf("addition: %d\n", num);
10		return 0;
(gdb) 
11	}

AS you can see above, “add_two_numbers” function which is defined in “add.c” is called from line number 8 in main program, so we can set the breakpoint at this function using line number as,

$ br 8

once the breakpoint is set, we need to run this program so we can stop at breakpoint as, ( To run the program you can type “run” or only “r” on gdb console )

(gdb) r
Starting program: /home/devlab/Desktop/helloworld/helloworld 
helloworld

Breakpoint 1, main (argc=1, argv=0x7fffffffdf78) at helloworld.c:8
8		num = add_two_numbers(3, 2);

As you can see, our program stopped when breakpoint occured at line 8, now we need to use different command “step” or “s” to single step inside the function which is written in another file as,

(gdb) s
add_two_numbers (a=32767, b=-8336) at add.c:1
1	int add_two_numbers(int a, int b) {

As you can see, now, our debugging control is inside “add.c” at line number 1, now we can simply continue to type “s” to go step by step, or just press enter where GDB considers it as last entered command,

(gdb) 
2		return (a+b);
(gdb) 
3	}

We just continued to press enter, and we now come to end of “add.c” , one more enter should take us back to main program and it can be seen as,

(gdb) 
main (argc=1, argv=0x7fffffffdf78) at helloworld.c:9
9		printf("addition: %d\n", num);

Here, now if you want to go inside printf which is library function, you can press “s” or enter, but since normally our intention was not to debug library function, we can just press “c” so program can continue its execution till end.

(gdb) c
Continuing.
addition: 5
[Inferior 1 (process 16740) exited normally]
Do-More Designer Debug
Do-More Designer Debug

Leave a Comment