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
  • 点击左侧工具栏”调试“按钮进入调试工具区,点击绿色开始运行按钮,即可开始调试

转载于:https://www.cnblogs.com/lizhensheng/p/11117323.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Book Description A hands-on approach to mastering the fundamentals of Grunt Overview Gain insight on the core concepts of Grunt, Node.js and npm to get started with Grunt. Learn how to install, configure, run, and customize Grunt Example-driven and filled with tips to help you create custom Grunt tasks In Detail In recent times, modern web browsers have become the application platform of choice. Grunt, along with its wide range of plugins, provides a simple way of managing the large number of build tasks required to maintain a sophisticated web application. Getting Started with Grunt: The JavaScript Task Runner provides you with all the information you need to become an effective Grunt power-user. You will quickly learn how to install, configure, and run Grunt. You will go on to understand how to use third-party Grunt and then create your own Grunt tasks that cater to your particular needs. This book first demonstrates various Grunt use cases before running through the steps of installing, configuring, running, and customizing Grunt. You will learn how to install Node.js, the Node.js package manager (npm), and Grunt. Then, you will understand how to set up and configure a personalized Grunt environment. Next, you will look at the various methods of running and customizing Grunt to utilize its flexibility. Finally, to emphasise what has been learnt, you will see a complete example build of a web application. Getting Started with Grunt: The JavaScript Task Runner will enable you to create your very own Grunt environments from scratch and fully utilize Grunt's large feature set to effectively solve your custom requirements. What you will learn from this book Learn about Grunt and its advantages Understand Node.js and how it relates to Grunt Take an in-depth look at npm, Node.js modules, and the working of Grunt plugins Get familiar with installing Grunt and setting up your first Grunt build environment Gain insight on the methods of configuring Grunt and when each method should be used Effectively execute Grunt through the use of task arguments, task aliasing, multi-task targets, and more Construct your own Grunt tasks, multi-tasks, and asynchronous tasks Approach A step-by-step, practical tutorial to help you transform into a Grunt power-user. Who this book is written for This book is for JavaScript developers who want to get to grips with GruntJS and use it to build and test their JavaScript applications. The only requirement for this book is a basic understanding of objects and functions in JavaScript. Product Details Paperback: 132 pages Publisher: Packt Publishing (February 19, 2014) Language: English ISBN-10: 1783980621 ISBN-13: 978-1783980628 Product Dimensions: 9.2 x 7.5 x 0.3 inches
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值