how to set breakpoint on function in a shared library which has not been loaded in gdb
Actually gdb should tell you that it's able to resolve the symbol in the future, when new libraries are loaded:
(gdb) b test
Function "test" not defined.
Make breakpoint pending on future shared library load? (y or [n]) y
Breakpoint 1 (test) pending.
(gdb) r
And later on once the .so object is loaded, it will resolve the breakpoint, e.g.:
Reading symbols for shared libraries . done
Breakpoint 1 at 0xcafebebe
Pending breakpoint 1 - "test" resolved
Actually, this method won't always work.
Suppose I have several shared libraries which each have a function named "Init". If I have loaded a different library then "b Init" is going to set the breakpoint to the wrong instance of the function "Init". So I have to specify the breakpoint like this:
(gdb) b object5.c:66
No source file named object5.c.
Another way is to specify the filename and der function, e.g.:
b object5.c:test
This should be unique. Maybe you want also to specify the path to the source code (as already suggested) by:
set directories path_of_object5.c