Caleb A Rapp
Caleb A Rapp

Reputation: 13

Breakpoints not hit while debugging c++ in Visual Studio Code

System: POP OS IDE: Visual Studio Code

I am attempting to debug a simple c++ project. When I initially set up the project, the debugger would function as expected. When I added a makefile, breakpoints were no longer hit. Before adding the makefile I compiled from a task.json file with g++ -g -o main.o main.cpp. This command obviously didn't do what the makefile is doing, but debugging DID work in my environment.

Setting launch.json to stopAtEntry also does not work. So my assumption is that I am compiling incorrectly.

Makefile output:

g++ -Wall -I./header -L./lib -c -o source/MqttManager.o source/MqttManager.cpp

g++ -g -o bin/debug/GenericMqttClient source/main.o source/MqttManager.o -Wall -I./header -L./lib -lpaho-mqtt3c

rm -r ./source/*.o

Here are the relevant files:

launch.json

   {
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++ - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/bin/debug/GenericMqttClient",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++ build active file",
            "miDebuggerPath": "/bin/gdb"
        }
    ]
}

Makefile

CXX = g++
EXENAME = GenericMqttClient

#   Directories
HDIR = ./header
LDIR = ./lib
ODIR = ./obj
SDIR = ./source
BIN = ./bin
DEBUGDIR = $(BIN)/debug
RELDIR = $(BIN)/release

# Project Files
SRC = $(wildcard $(SDIR)/*.cpp)
OBJ = $(SRC:.cpp=.o)

# Includes
DEPS = $(HDIR)/MqttManager.h
LIBS = -lpaho-mqtt3c

#flags
CXXFLAGS = -Wall -I$(HDIR) -L$(LDIR)
# DEBUGFLAGS = $(CXXFLAGS) -g

# Object files
$(SDIR)/%.o: %.cpp $(DEPS)
    $(CXX) -c -o $@ $< $(CXXFLAGS) $(LIBS)

# Release
release: prep $(RELDIR)/$(EXENAME) objclean
$(RELDIR)/$(EXENAME): $(OBJ)
    $(CXX) -o $@ $^  $(CXXFLAGS) $(LIBS) 

# Debug
debug: prep $(DEBUGDIR)/$(EXENAME) objclean
$(DEBUGDIR)/$(EXENAME): $(OBJ)
    $(CXX) -g -o $@ $^ $(CXXFLAGS)  $(LIBS)

#
#   Other Rules
#
all: clean debug release objclean

prep:
    @mkdir -p $(DEBUGDIR) $(RELDIR)

objclean:
    rm -r $(SDIR)/*.o

clean:
    rm -rf $(BIN)/*

Let me know if I should include more files.

Upvotes: 1

Views: 3853

Answers (1)

Asteroids With Wings
Asteroids With Wings

Reputation: 17464

You used to give g++ the -g switch when compiling your CPP files. Now you don't. It'll need to be added back in.

The way you've written the Makefile, you could uncomment the definition of DEBUGFLAGS, then change $(CXXFLAGS) to $(DEBUGFLAGS) in your $(SDIR)/%.o target. However, this would affect release builds too, so you probably want to find a different way to do this, such as by replacing the CXXFLAGS definition just for the debug target (which should also affect its dependencies):

debug: CXXFLAGS=$(DEBUGFLAGS)
debug: prep $(DEBUGDIR)/$(EXENAME) objclean
$(DEBUGDIR)/$(EXENAME): $(OBJ)
    $(CXX) -o $@ $^ $(CXXFLAGS)  $(LIBS)

I've also removed the -g from the link stage. The switch needs to be given at the compilation stage (not the link stage, which is performed by the debug target) because it is responsible for writing debugging information into your program. This can only be generated from source code and, without it, your debugger has no way to honour breakpoints.


tl;dr

In the resulting commands you've quoted, you need to make it so that -g is moved, from the second command to the first:

g++ -Wall -I./header -L./lib -c -o source/MqttManager.o source/MqttManager.cpp
g++ -g -o bin/debug/GenericMqttClient source/main.o source/MqttManager.o -Wall -I./header -L./lib -lpaho-mqtt3c
rm -r ./source/*.o

Upvotes: 1

Related Questions