简体   繁体   English

在 Visual Studio Code 中调试 C++ 时未命中断点

[英]Breakpoints not hit while debugging c++ in Visual Studio Code

System: POP OS IDE: Visual Studio Code系统: POP OS IDE: Visual Studio Code

I am attempting to debug a simple c++ project.我正在尝试调试一个简单的 C++ 项目。 When I initially set up the project, the debugger would function as expected.当我最初设置项目时,调试器会按预期运行。 When I added a makefile, breakpoints were no longer hit.当我添加一个 makefile 时,断点不再被命中。 Before adding the makefile I compiled from a task.json file with g++ -g -o main.o main.cpp .在添加 makefile 之前,我使用g++ -g -o main.o main.cpp从 task.json 文件编译。 This command obviously didn't do what the makefile is doing, but debugging DID work in my environment.这个命令显然没有做 makefile 正在做的事情,但在我的环境中调试确实有效。

Setting launch.json to stopAtEntry also does not work.将 launch.json 设置为 stopAtEntry 也不起作用。 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++ -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 g++ -g -o bin/debug/GenericMqttClient source/main.o source/MqttManager.o -Wall -I./header -L./lib -lpaho-mqtt3c

rm -r ./source/*.o 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.让我知道是否应该包含更多文件。

You used to give g++ the -g switch when compiling your CPP files.你曾经在编译 CPP 文件时给 g++ 一个-g开关。 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.按照您编写 Makefile 的方式,您可以取消注释DEBUGFLAGS的定义,然后在您的$(SDIR)/%.o目标$(DEBUGFLAGS) $(CXXFLAGS)更改$(CXXFLAGS) $(DEBUGFLAGS) 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定义(这也会影响其依赖项):

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.我还从链接阶段删除了-g 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.该开关需要在编译阶段(不是链接阶段,由debug目标执行)给出,因为它负责将调试信息写入您的程序。 This can only be generated from source code and, without it, your debugger has no way to honour breakpoints.这只能从源代码生成,没有它,您的调试器将无法执行断点。


tl;dr tl;博士

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从第二个命令移动到第一个命令:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM