Special Topics
What if it doesn't even compile or load?
Phantom line numbers in syntax error messages
Sometimes, the line number from the compiler error message is not very informative. Typically, the problem is a missing closing brace or semicolon. To locate it, you can start by commenting out the function where you suspect the problem to be. Don't be surprised by linker errors if you do this, but stay cool and use binary search principles to locate the location of the problem.
Missing Libraries
If you have a program that uses functions from another .o file, you can get 'undefined reference to f' errors from the linker.
A library can be static (functions become part of the resulting executable file) or dynamic (the functions are not physically attached to the calling code until the program is actually executed).
To create a static library and compile your main program with it:
gcc -g -c b.c ar rc lib88.a b.o gcc -g a.c -l88 -LzWith the -L option, you tell the linker in what directories other than the current one it should look for the functions.
To create a dynamic library (to avoid space-wasting separate copies of the same library) and then link to it, use:
gcc -fPIC -c b.c gcc -shared -o lib88.so b.o gcc -g a.c -l88 -LzThe resulting program executable will merely contain a notation that this program makes use of the library lib88.so. The linking itself will now occur at runtime. With the
ldd
command, you can check which libraries a program needs,
and where the OS finds them:
ldd a.outTo add a library to the OS's normal library search path, in bash you can do:
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/Debug/z export LD_LIBRARY_PATH
For missing library problems, ofteh the root of the problem lies in a program called pkgconfig. The default directory that pkgconfig searches for .pc files depends on the location of pkgconfig itself, and must sometimes be adapted by setting the environment variable PKG_CONFIG_PATH.
Debugging GUI programs
The example will be for the curses library, but the applied principles are valid for other GUI libraries as well.
Debugging Curses Programs
For debugging curses programs, you must tell GDB to have the program execute in
a different terminal window than the one that GDB is running in with the
tty
command. First, go to another window and run the
tty
command there to determine the ID for that window. Then, in
the GDB window you type:
tty /dev/pts/8You must also type something like
sleep 10000in the execution window, so that your keyboard input to that window wil go to that program rather than to the shell.
In DDD, you can arrange a separate window for execution of the program by choosing 'View | Execution Window'. Don't type the sleep command into that window, as DDD does that for you.
In Eclipse, you can use the option 'C/C++ Attach to Local Application'. First start your program in a separate shel window, then select Run | Open Debug Dialog and from the list, choose the process you wish GDB to attach.
No comments:
Post a Comment