Question

In the project I am working on, I use gdb to debug - I would like to be able to call make from gdb - but I can't because that would mean the working directory would be incorrect. Here is my directory layout and the two options I have tried:

Project
 ├─bin
 │ ┗━Option 1
 ├─build
 │ ├─debug.sh
 │ ┟─makefile
 │ ┗━Option 2
 ├─include
 └─src

Here is the contents of debug.sh using option 1

#!/bin/bash
gdb --cd=../bin ../bin/ProjectBinary -ex run

Option1:

When I invoke ./debug, the program runs correctly - the working directory is correctly set to Project/bin. But I cannot call make from gdb because the makefile isn't in the current directory.

Option2 (debug.sh not shown):

When I invoke ./debug, the program fails because the working directory is set incorrectly - but I can successfully call make from gdb.

How do I get the best of both worlds and allow it so I can call make from gdb, as well as the working directory to be correct for the executable - ie how I have 2 working directories, one for the program being debugged, and one for gdb itsself?

Was it helpful?

Solution

Stay in bin/ but pass the "-c" option to make so that it switches to ../build first

$ cd ./bin
$ gdb ProjectBinary
(gdb) make -C ../build

You can also create bin/Makefile that calls the build/Makefile:

bin/Makefile

.PHONY: all
all:
        make -C ../build

(Change spaces to tab at the start of the line that calls make)

Then run:

$ gdb ProjectBinary
(gdb) make
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top