快速入门:使用 Visual Studio 调试器调试 C++Quickstart: Debug with C++ using the Visual Studio debugger

Visual Studio 调试器提供了许多强大的功能以帮助调试应用。The Visual Studio debugger provides many powerful features to help you debug your apps. 本主题提供了一种快速了解部分基本功能的方法。This topic provides a quick way to learn some of the basic features.

创建新项目Create a new project

  1. 打开 Visual Studio 并创建一个项目。Open Visual Studio and create a project.

    按 Esc 关闭启动窗口 。Press Esc to close the start window. 键入 Ctrl+Q 以打开搜索框,键入“c++”,选择“模板”,然后选择“创建新的控制台应用项目” 。Type Ctrl + Q to open the search box, type c++, choose Templates, then choose Create new Console App project. 在出现的对话框中,选择“创建” 。In the dialog box that appears, choose Create.

    从顶部菜单栏中选择“文件” > “新建” > “项目” 。From the top menu bar, choose File > New > Project. 在“新建项目”对话框的左窗格中,在“Visual C++”下,选择“Windows 桌面”,然后在中间窗格中选择“Windows 控制台应用程序” 。In the left pane of the New project dialog box, under Visual C++, choose Windows Desktop, and then in the middle pane choose Windows Console Application. 然后,键入名称(如 MyDbgApp),然后单击 “确定” 。Then, type a name like MyDbgApp and click OK.

    如果没有看到“Windows 控制台应用程序” 项目模板,请转到“工具” > “获取工具和功能...”,这会打开 Visual Studio 安装程序。If you don't see the Windows Console Application project template, go to Tools > Get Tools and Features..., which opens the Visual Studio Installer. Visual Studio 安装程序启动。The Visual Studio Installer launches. 选择“使用 C++ 的桌面开发”工作负载,然后选择“修改”按钮 。Choose the Desktop development with C++ workload, then choose Modify.

    Visual Studio 随即创建项目。Visual Studio creates the project.

  2. 在 MyDbgApp.cpp 中,将以下代码In MyDbgApp.cpp, replace the following code

    int main()
    {
        return 0;
    }
    

    替换为此代码(请勿删除 #include "stdafx.h"):with this code (do not remove #include "stdafx.h"):

    #include <list>
    #include <iostream>
    
    using namespace std;
    
    void doWork()
    {
        list <int> c1;
    
        c1.push_back(10);
        c1.push_back(20);
    
        const list <int> c2 = c1;
        const int &i = c2.front();
        const int &j = c2.front();
        cout << "The first element is " << i << endl;
        cout << "The second element is " << j << endl;
    
    }
    
    int main()
    {
        doWork();
    }
    

设置断点Set a breakpoint

断点是一个标记,指示 Visual Studio 应在哪个位置挂起运行的代码,以查看变量的值或内存的行为,或确定代码的分支是否运行 。A breakpoint is a marker that indicates where Visual Studio should suspend your running code so you can take a look at the values of variables, or the behavior of memory, or whether or not a branch of code is getting run. 它是调试中最基本的功能。It is the most basic feature in debugging.

  1. 若要设置断点,请单击 doWork 函数调用左侧的滚动条槽(或选择代码行,然后按 F9) 。To set the breakpoint, click in the gutter to the left of the doWork function call (or select the line of code and press F9).

    设置断点Set a breakpoint

  2. 现在,按 F5(或选择“调试”>“开始调试”) 。Now press F5 (or choose Debug > Start Debugging).

    命中断点Hit a breakpoint

    调试器会在设置断点的位置暂停。The debugger pauses where you set the breakpoint. 调试器和应用程序在该处暂停的语句由黄色箭头指示。The statement where the debugger and app execution is paused is indicated by the yellow arrow. 具有 doWork 函数调用的行尚未执行。The line with the doWork function call has not yet executed.

    提示

    如果循环或递归中存在断点,或者如果有许多频繁单步执行的断点,请使用条件断点以确保代码仅在满足特定条件时挂起。If you have a breakpoint in a loop or recursion, or if you have many breakpoints that you frequently step through, use a conditional breakpoint to make sure that your code is suspended ONLY when specific conditions are met. 条件断点可以节省时间,还可以更容易地调试难以重现的问题。A conditional breakpoint saves time and can also make it easier to debug issues that are hard to reproduce.

    当尝试调试 C++ 中与内存相关的故障时,还可以使用断点来检查地址值(查找 NULL)和引用计数。When trying to debug memory-related failures in C++, you can also use breakpoints to inspect address values (look for NULL) and reference counts.

有不同的命令来指示调试器继续。There are different commands to instruct the debugger to continue. 我们将演示自 Visual Studio 2017 开始新增的一个有用的代码导航命令。We show a useful code navigation command that is available starting in Visual Studio 2017.

在断点处暂停时,将鼠标悬停在 c1.push_back(20) 语句上,直至绿色的“运行到单击处” 按钮 运行到单击处 出现,然后按“运行到单击处”按钮 。While paused at the breakpoint, hover over the statement c1.push_back(20) until the green Run to click button Run to Click appears, and then press the Run to click button.

运行到单击处Run to click

应用将继续执行,调用 doWork,并于你在其中单击按钮的代码行上暂停。The app continues execution, calling doWork, and pauses on the line of code where you clicked the button.

用于单步执行代码的常见键盘命令包括 F10 和 F11 。Common keyboard commands used to step through code include F10 and F11. 如需获取更多详尽介绍,请参阅初探调试器For more in-depth instructions, see First look at the debugger.

检查数据提示中的变量Inspect variables in a datatip

  1. 在当前代码行中(由黄色执行指针标记),将鼠标悬停在 c1 对象上以显示数据提示。In the current line of code (marked by the yellow execution pointer), hover over the c1 object with your mouse to show a datatip.

    查看数据提示View a datatip

    数据提示显示 c1 变量的当前值且允许检查其属性。The datatip shows you the current value of the c1 variable and allows you to inspect its properties. 调试时,如果出现意外值,则表示在前一代码行或调用的代码行上出现 bug。When debugging, if you see a value you don't expect, you probably have a bug in the preceding or calling lines of code.

  2. 展开数据提示以查看 c1 对象的当前属性值。Expand the datatip to look at the current property values of the c1 object.

  3. 如果要固定数据提示,以便在执行代码时可以继续查看 c1 的值,请单击小图钉图标。If you want to pin the datatip so that you can continue to see the value of c1 while you execute code, click the small pin icon. (可将固定的数据提示移动到方便的位置。)(You can move the pinned datatip to a convenient location.)

编辑代码并继续调试Edit code and continue debugging

如果在调试会话期间发现一个要在代码中测试的更改,则也可以执行此操作。If you identify a change that you want to test in your code while in the middle of a debugging session, you can do that, too.

  1. 单击 c2.front() 的第二个实例且将 c2.front() 更改为 c2.back()Click the second instance of c2.front() and change c2.front() to c2.back().

  2. 多次按 F10(或“调试”>“单步跳过”),向前移动调试器并执行已编辑的代码 。Press F10 (or Debug > Step Over) a few times to advance the debugger and execute the edited code.

    编辑并继续Edit and continue

    F10 一次使调试器前进一个语句,但是是跳过函数而不是单步执行它们(跳过的代码仍然执行)。F10 advances the debugger one statement at a time, but steps over functions instead of stepping into them (the code that you skip still executes).

有关使用编辑并继续以及功能限制的详细信息,请参阅编辑并继续For more information on using edit-and-continue and on feature limitations, see Edit and Continue.

后续步骤Next steps

在本教程中,你已了解了如何启动调试器、逐步执行代码以及检查变量。In this tutorial, you've learned how to start the debugger, step through code, and inspect variables. 你可能会希望更深入地了解调试器功能以及查看指向更多信息的链接。You may want to get a high-level look at debugger features along with links to more information.