我有三个文件:
主.cpp
#include <iostream>
#include "log.h"
int main()
{
int a = 8; // Breakpoint on this line
a++;
const char *string = "Hello";
for (int i = 0; i < 5; i++)
{
const char c = string[i];
std::cout << c << std::endl;
}
Log("Hello, World!");
std::cin.get();
}
日志.cpp
#include "log.h"
#include <iostream>
void Log(const char* message)
{
std::cout << message << std::endl;
}
log.h
#pragma once
void Log(const char* message);
我的 launch.json 看起来像这样:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(lldb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb"
}
]
}
终端中的输出是:
H
e
l
l
o
Hello, World!
当我使用 VSCode 调试器进行调试时,断点显示一个白色圆圈,并将鼠标悬停在其上时显示以下内容:“包含此断点的模块尚未加载或无法获取断点地址”。我对语言和调试很陌生,无法根据互联网上类似问题的解决方案弄清楚该怎么做。请帮忙,谢谢!
对我有用并允许我继续进行调试的一件事是转到 launch.json 文件并将“程序”更改为您要调试的文件所在的路径。
所以就我而言,
"program": "${workspaceFolder}/cpp/main.cpp"
这可能是由于程序中缺少调试信息造成的,这个问题可以通过在编译时添加调试标志来解决(通常是
-g
标志,请参阅 gcc 和 Clang 手册)。
指示编译器向程序添加调试信息使调试器(在您的例子中为 lldb)能够执行各种操作,例如在源代码中设置断点。
如果简单地将
-g
添加到编译标志中并不能解决问题,VSCode 的关于调试符号的 documentation 可能会有所帮助。