• 8.31-使用vscode进行c/c++程序设计


    前言

    • 此处介绍使用vscode进行cpp程序设计,使用make进行构建,gdb进行调试
    • 此次项目用于到PAT刷题
    • 以后逐步地使用gTest 进行测试,valgrind 进行深度检查,log4cplus进行日志输出

    目录

    TOC

    参考

    how-do-i-set-up-vscode-to-compile-c-code
    vscode c++ programming
    vscode external tasks
    gdb does't find sources
    别人的完全配置日志

    学习记录

    安装c/c++插件

    Ctrl+Shift+P
    ext install cpp

    文件准备

    cd pat-basic
    mkdir bin
    mkdir include
    mkdir src
    touch Makefile
    cd ./src
    touch hello_world.cpp
    touch 1035.cpp

    编写Makefile

    PROJECT := zs_pat_basic
    
    # define files and directories
    CXX_SRCS := $(shell find src/ -name "*.cpp")
    HEADER_FILES := $(shell find include/ -name "*.h")
    SRC_DIR := ./src
    INCLUDE_DIR := ./include
    BUILD_DIR := ./bin
    
    # define debug options
    DEBUG_OPTIONS := -g
    
    # define build targets
    .PHONY : all
    all: $(BUILD_DIR)/hello_world 
            $(BUILD_DIR)/1035 
             $(BUILD_DIR)/1050 
    
    $(BUILD_DIR)/% : $(SRC_DIR)/%.cpp
        $(CXX) -o $@ $(DEBUG_OPTIONS) -I$(INCLUDE_DIR) $<
    
    .PHONY : clean 
    clean : 
        rm -rf $(BUILD_DIR)/* 

    编写c++配置文件c_cpp_properties.json

    • vscode自身配置文件全部在./.vscode/目录下,在vscode资源浏览器中可以看到
    • 注意,刚刚对cpp文件进行编辑后,#include 这句话是有红色下划线警示的,提示找不到文件,这时使用鼠标悬浮功能,点击“红色灯泡”,vscode会自动在配置文件夹中新建c_cpp_properties.json文件,文件内容如下
    {
        "configurations": [
            {
                "name": "Mac",
                "includePath": ["/usr/include"],
                "browse" : {
                    "limitSymbolsToIncludedHeaders" : true,
                    "databaseFilename" : ""
                }
            },
            {
                "name": "Linux",
                "includePath": ["/usr/include"],
                "browse" : {
                    "limitSymbolsToIncludedHeaders" : true,
                    "databaseFilename" : ""
                }
            },
            {
                "name": "Win32",
                "includePath": ["c:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/include"],
                "browse" : {
                    "limitSymbolsToIncludedHeaders" : true,
                    "databaseFilename" : ""
                }
            }
        ],
        "clang_format" : {
            "style" : "file",
            "fallback-style" : "LLVM",
            "sort-includes" : false
        }
    }

    配置make任务

    • vscode没有内置make功能,需要借助Task功能进行配置
    • Ctrl+shift+P 进入命令模式,键入tasks: Configure Task Runner,此时vscode自动生成task.json文件,编写内容如下
    {
        // See https://go.microsoft.com/fwlink/?LinkId=733558
        // for the documentation about the tasks.json format
        "version": "0.1.0",
        "command": "make",
        "isShellCommand": true,
        "tasks": [
            {
                "taskName": "Makefile",
                // Make this the default build command.
                "isBuildCommand": true,
                // Show the output window only if unrecognized errors occur.
                "showOutput": "always",
                // No args
                "args": ["all"],
                // Use the standard less compilation problem matcher.
                // zs: The problem matcher is for gcc task, not for make.
                "problemMatcher": {
                    "owner": "cpp",
                    "fileLocation": ["relative", "${workspaceRoot}"],
                    "pattern": {
                        "regexp": "^(.*):(\d+):(\d+):\s+(warning|error):\s+(.*)$",
                        "file": 1,
                        "line": 2,
                        "column": 3,
                        "severity": 4,
                        "message": 5
                    }
                }
            }
        ]
    }

    编写调试配置文件launch.json

    • 同样,vscode也需要自己定义调试相关选项
    • 点击左侧工具栏”Debug“,点击”齿轮“按钮,此时vscode自动生成launch.json文件,编写内容如下
    {
        "version": "0.2.0",
        "configurations": [
            {
                "name": "C++ Launch",
                "type": "cppdbg",
                "request": "launch",
                "targetArchitecture": "x64",
                "program": "${workspaceRoot}/bin/hello_world",
                "args": [],
                "stopAtEntry": false,
                "cwd": "${workspaceRoot}",
                "environment": [],
                "externalConsole": true,
                "linux": {
                    "MIMode": "gdb"
                },
                "osx": {
                    "MIMode": "lldb"
                },
                "windows": {
                    "MIMode": "gdb"
                }
            },
            {
                "name": "C++ Attach",
                "type": "cppdbg",
                "request": "launch",
                "targetArchitecture": "x64",
                "program": "${workspaceRoot}/bin/hello_world",
                "args": [],
                "stopAtEntry": false,
                "cwd": "${workspaceRoot}",
                "environment": [],
                "processId": "${command.pickProcess}",
                "externalConsole": false,
                "linux": {
                    "MIMode": "gdb"
                },
                "osx": {
                    "MIMode": "lldb"
                },
                "windows": {
                    "MIMode": "gdb"
                }
            }
        ]
    }

    使用流程

    • 编写hello_world.cpp文件
    • 设置断点
    • Ctrl+shift+B进行make
    • 点击左侧工具栏”调试“按钮进入调试工具区,点击绿色开始运行按钮,即可开始调试
  • 相关阅读:
    gauss消元
    POJ1229 域名匹配
    HDU3487 play with chain
    POJ1185 炮兵阵地
    POJ2411
    sgu233 little kings
    树形DP初步-真树1662
    树形DP初步-二叉树1661
    c++——string类用法
    UVa1354 ——天平难题
  • 原文地址:https://www.cnblogs.com/lizhensheng/p/11117323.html
Copyright © 2020-2023  润新知