MinGW-w64 是 Minimalist GNU for Windows 的一个扩展版,是一个开源的 Windows 平台的开发工具集,主要用于在 Windows 上构建和运行基于 GNU 工具链(如 GCC)的应用程序。相比原始的 MinGW 项目,MinGW-w64 增强了对 64 位 Windows 的支持,并提供了更多的功能和改进。换句话说,你想要在 Windows 上面编译 C/C++,必须得安装这个工具集。所以安装它是必要的一个步骤,如果安装过了可以跳过!
miDebuggerPath: MI 调试程序(如 gdb)的路径。一般设置为你安装 MinGW 目录 /bin/gdb.exe。对照我的:D:\\MinGW\\mingw64\\bin\\gdb.exe。
设置完成之后,我们可以试着编写复杂点的程序来测试断点调试。我给出一个经典的二分查找的代码用于测试:
cpp
#include <iostream>
#ifdef _WIN32
#include <windows.h>
#endif
using namespace std;
class Solution
{
public:
int binary_search(int arr[], int n, int target)
{
int left = 0, right = n - 1;
while (left <= right)
{
int mid = left + (right - left) / 2;
if (arr[mid] == target)
return mid;
else if (arr[mid] < target)
left = mid + 1;
else
right = mid - 1;
}
return -1;
}
};
void test()
{
Solution solution;
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int n = sizeof(arr) / sizeof(arr[0]);
int target = 5;
int result = solution.binary_search(arr, n, target);
if (result != -1)
cout << "Element found at index " << result << endl;
else
cout << "Element not found in the array" << endl;
}
int main()
{
#ifdef _WIN32
SetConsoleOutputCP(CP_UTF8);
#endif
cout << "Hello, World!" << endl;
test();
return 0;
}
我们在 int mid = left + (right - left) / 2; 的地方打断点,之后启动调试: