设为首页 加入收藏

TOP

Ubuntu下VS Code如何调试C++代码(一)
2018-11-20 22:09:00 】 浏览:177
Tags:Ubuntu Code 如何 调试 代码

我写了一个实例程序(不重要)


#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main(void)
{
    fstream iofile("test.txt");
    vector<string> strs(20);
    int i=0;
    if(!iofile){
        cerr<<"open file failed"<<endl;
    }else{
        while(iofile>>strs[i++]);
    }
    for(int j=0;j<i-1;j++)
    {
        cout<<strs[j]<<endl;
    }
//    cout<<endl;
    cout<<"after sort"<<endl;
    cout<<*(strs.begin())<<endl;
    sort(strs.begin(),strs.begin()+i-1);
    cout<<"i:"<<i<<" "<<strs[1]<<endl;
    for(int j=0;j<i;j++)
        {
                cout<<strs[j]<<" ";
        }
    cout<<endl;
    return 0;
}


这个时候,我们按F5,发现不能运行,它提示需要一个Launch.json文件,OK,这是一个启动文件,我们来配置它。


{
    // 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": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/build",
            "preLaunchTask": "build",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}


注意,这里需要修改的部分主要是program那一行,仅需修改为自己编译后产生的文件名,不如g++ -g 1.7.cpp -o build,所以这里我就取了build这个名字。


还有,就是要添加preLaunchTask这一行,名字与下面的task要对应。


这里,它还需要一个task.json文件,配置如下,


{
    // See https://

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Ubuntu 18.04多个版本GCC编译器的.. 下一篇C语言实现搬山游戏代码解析

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目