Ubuntu使用VS Code开发C++程序

1. 安装VS Code

下载地址https://code.visualstudio.com/download

2. 安装C++扩展程序

详情请看https://code.visualstudio.com/docs/languages/cpp

https://blogs.msdn.microsoft.com/vcblog/2016/03/31/cc-extension-for-visual-studio-code/

打开VS Code后,输入ext install cpptools,等待安装完成。

3. 当然打开目录,编辑源码了

4. 使用g++生成可执行程序/动态连接库

https://code.visualstudio.com/Docs/editor/tasks#_defining-a-problem-matcher

创建task.json,下面是gcc命令,改成g++对应的命令即可

{
    "version": "0.1.0",
    "command": "gcc",
    "args": ["-Wall", "helloWorld.c", "-o", "helloWorld"],
    "problemMatcher": {
        "owner": "cpp",
        "fileLocation": ["relative", "${workspaceRoot}"],
        "pattern": {
            "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
            "file": 1,
            "line": 2,
            "column": 3,
            "severity": 4,
            "message": 5
        }
    }
}

最后按Ctrl+Shift+b

注:修改快捷键设置:

File->Preferences->Keyboard Shortcuts

5. 使用gdb调试

当然第一步是要安装gdb

sudo apt-install gdb

    1) task.json的args里一定要添加-g,完整的task.json类似如下:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "0.1.0",
    "command": "g++",
    "isShellCommand": true,
    "args": ["-o","da.exe","main.c","-g"],
    "showOutput": "always",
    "problemMatcher": {
        "owner": "cpp",
        "fileLocation": ["relative", "${workspaceRoot}"],
        "pattern": {
                    "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                    "file": 1,
                    "line": 2,
                    "column": 3,
                    "severity": 4,
                    "message": 5
                }
    }
}

   2) 先生成可执行程序,详见第4点  

   4) 单击左侧的调试按钮,出来的界面上,单击绿色三角(调试)符号,会生成launch.json,根据https://blogs.msdn.microsoft.com/vcblog/2016/03/31/cc-extension-for-visual-studio-code/,默认生成的launch.json包括2个配置:打开新的进程及附加(attach)到已运行的进程,分别是C++ Launch和C++ Attach

     5)尽情的调试吧:D 发现VSCode的快捷键跟Windows下的一样,F10、F11……

转载于:https://my.oschina.net/shanlilaideyu/blog/725903

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值