Search code examples
debugginggccmakefilegdbdebug-symbols

How to write Makefiles to get a relative path in debug symbols?


I have a structure of code like this:

project_dir/
    source1.c
    subdir/
        source2.c

The Makefile calls subdir/Makefile, so that the file subdir/source2.c is compiled in this way:

gcc -g -someoptions source2.c

and symbols in GDB link to source2.c instead of subdir/source2.c (with the result that GDB can not find symbols in source files). How should I write a Makefile or what options to use in gcc to get symbols using the relative path to the project main directory (or eventually the absolute path)?

I can not use:

cd .. && gcc -g -someoptions ../subdir/source2.c

because I would have to change references to header files in all files in subdir.


Solution

  • Your question is platform-specific (e.g. on Linux GDB should just work(TM), so I assume you are not on Linux).

    One option is to build like this:

    gcc -g ${PWD}/source2.c -o ...
    

    Another option is to use GDB dir command to add ${TOP}/project_dir/subdir to the list of directories that GDB will search for sources.