Ubuntu/Linux 用vscode 运行/调试 c++ 程序

本文主要参考资料:

http://www.cnblogs.com/lidabo/p/5888997.html

参考博客详细介绍从vscode安装到cmakelist的介绍到编译执行,但其中使用vscode的launch.json与task.json有出入,故增加这篇博客。请首次使用的读者先阅读上篇文章直到看到launch.json字眼后对照本篇博客,重温的读者可直接看本博客。

在ubuntu下下载安装vscode以及C++插件后(根据参考资料)

在桌面上建立自己的文件夹,取名my_first_linux_cpp(任意),拖拽图标到vscode上打开

在其中新建下列文件:

/* solution.h */
class Solution {
public:
    void Say();
};
/* solution.cpp */
#include <iostream>
#include "solution.h"
void Solution::Say(){
   std::cout << "HI!" << std::endl;
}
/* hw2.cpp */
#include "solution.h"
#include <stdio.h>
int main () {
    Solution sln;
    sln.Say();
    getchar();
    return 0;
}

以及一个名为makefile的文件

build : hw2.o solution.o
	g++ -o build hw2.o solution.o #注意前面必须是tab,不能是空格
hw2.o : hw2.cpp solution.h
	g++ -g -c hw2.cpp
solution.o : solution.h solution.cpp
	g++ -g -c solution.cpp
clean :
	rm hw2.o solution.o build

在vscode中使用快捷键ctrl+shift+D ,点击齿轮(倒数第二个图标)

 打开launch.json,修改为

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/build",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "preLaunchTask": "build",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

按F5打开,报错,

 点击配置任务

点击使用模板 创建task.json文件

选择others

将task.json修改为

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "make"
        }
    ]
}

使用F5键运行程序,也可加入断点

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值