教你如何配置VSCode来开发C++程序

Quank123Wip

2018-07-10 15:28:04

Personal

# 教你如何配置VSCode来开发C++程序 # $\color{red}\text{UPDATE:再提醒一遍本文仅支持Windows}$ Visual Studio Code,是微软开发的一款开源IDE,它支持几乎所有主流平台,支持几乎所有代码的代码提示,代码高亮,还可以使用插件。但它安装之后默认并不能支持C++,而现在大部分OIER都使用C++,所以在这里我将教你学会配置VSCode.(注:本篇文章适用于Windows) ## 安装VSCode 访问Microsoft的官网 [Hit Me](https://code.visualstudio.com/) ![VSCode官网](https://github.com/quank123wip/PicDropRepo/blob/master/a.png) 选择自己的平台,下载安装,一路点下去安装。然后你就可以打开VSCode了。 ## 配置工作区 这一步应该是最简单的了,直接在欢迎界面点击打开文件夹,打开自己写代码的路径,就可以啦。 ## 配置C++代码高亮 VSCode并没有内置高亮,所以我们要安装插件。 打开VSCode的命令(Ctrl+P),在左边栏的第五个按钮是插件栏,点击它,搜索并安装Cpp插件,耐心等待一会就好啦! 为了验证你配置成功了,新建一个Cpp文件,写一个Hello World,那么在编辑器里,代码应该会自动着色。 ## 配置C++编译、调试 在VSCode的配置中,最重要的就是配置编译器。在这里我推荐TDM-GCC。([点我去](http://tdm-gcc.tdragon.net/download)) 下载好之后,安装,之后将GCC的bin路径加入系统的环境变量(Path),这一步可以自己百度。 配置好了之后,点击VSCode的调试选项(左边栏第四个),点击绿三角旁边的那一栏,点击添加配置,VSCode会给你一个launch.json,用下面的替换(注意根据自己的配置更改) ```json { "version": "0.2.0", "configurations": [ { "name": "Cpp Launch", "type": "cppvsdbg", "request": "launch", "program": "C:\\Windows\\System32\\cmd.exe",//使用cmd "args": ["/c g++ ${fileDirname}/${fileBasenameNoExtension}.cpp -o ${fileDirname}/${fileBasenameNoExtension}.exe -g3 -std=c++11 && 略略略\\ConsolePauser.exe ${fileDirname}/${fileBasenameNoExtension}.exe"],//注意将略略略改成自己的ConsolePauser路径 "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": true }, { "name": "Cpp Debug", "preLaunchTask": "build", "type": "cppdbg", "request": "launch", "program": "${fileDirname}/${fileBasenameNoExtension}.exe", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": true, "MIMode": "gdb", "miDebuggerPath": "略略略", // GDB的路径,注意替换成自己的路径 "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ] } ] } ``` 这个配置文件用到了Dev-C++的ConsolePauser,你可以在Dev-C++的目录中找到它,然后复制到一个地方,更改上面的路径。 接下来,配置tasks.json Ctrl+Shift+P,输入configure task,选择Others,用下面的代码替换。 ```json { "version": "2.0.0", "tasks": [ { "label": "build", "type": "shell", "group": { "kind": "build", "isDefault": true }, "presentation": { "echo": true, "reveal": "always", "focus": false, "panel": "shared" }, "windows": { "command": "g++", "args": [ "-ggdb", "\"${file}\"", "--std=c++11", "-o", "\"${fileDirname}\\${fileBasenameNoExtension}.exe\"" ] } } ] } ``` 最后配置头文件部分 打开C:\Users\\(这里改成你的用户名)\\.vscode\extensions\ms-vscode.cpptools-0.17.6 打开c_cpp_properties.schema.json 用下面的替换 ```json { "$schema": "http://json-schema.org/draft-04/schema#", "type": "object", "definitions": { "configurations": { "type": "array", "items": { "type": "object", "required": [ "name" ], "properties": { "name": { "description": "Configuration identifier. Mac, Linux, and Win32 are special identifiers for configurations that will be auto-selected on those platforms, but the identifier can be anything.", "type": "string" }, "compilerPath": { "description": "Full path of the compiler being used, e.g. /usr/bin/gcc, to enable more accurate IntelliSense. Args can be added to modify the includes/defines used, e.g. -nostdinc++, -m32, etc., but paths with spaces must be surrounded with \\\" if args are used.", "type": "string" }, "cStandard": { "description": "Version of the C language standard to use for IntelliSense.", "type": "string", "enum": [ "c89", "c99", "c11", "${default}" ] }, "cppStandard": { "description": "Version of the C++ language standard to use for IntelliSense.", "type": "string", "enum": [ "c++98", "c++03", "c++11", "c++14", "c++17", "${default}" ] }, "compileCommands": { "description": "Full path to compile_commands.json file for the workspace.", "type": "string" }, "includePath": { "description": "A list of paths for the IntelliSense engine to use while searching for included headers. Searching on these paths is not recursive.", "type": "array", "items": { "type": "string" } }, "macFrameworkPath": { "description": "A list of paths for the Intellisense engine to use while searching for included headers from Mac frameworks. Only supported on Mac configuration.", "type": "array", "items": { "type": "string" } }, "defines": { "description": "A list of preprocessor definitions for the IntelliSense engine to use while parsing files. Optionally, use = to set a value, e.g. VERSION=1.", "type": "array", "items": { "type": "string" } }, "intelliSenseMode": { "description": "If set, it overrides the default mode used by the IntelliSense engine. Windows defaults to msvc-x64 and Linux/Mac default to clang-x64.", "type": "string", "enum": [ "msvc-x64", "clang-x64", "${default}" ] }, "forcedInclude": { "description": "A list of files that should be included before any include file in a translation unit.", "type": "array", "items": { "type": "string" } }, "configurationProvider": { "description": "The id of a VS Code extension that can provide IntelliSense configuration information for source files.", "type": "string" }, "browse": { "type": "object", "properties": { "limitSymbolsToIncludedHeaders": { "description": "true to process only those files directly or indirectly included as headers, false to process all files under the specified include paths.", "type": [ "boolean", "string" ] }, "databaseFilename": { "description": "Path to the generated symbol database. If a relative path is specified, it will be made relative to the workspace's default storage location.", "type": "string" }, "path": { "description": "A list of paths for the tag parser to use while searching for included headers. Searching on these paths is recursive by default. Specify '*' to indicate non-recursive search. For example: '/usr/include' will search through all subdirectories while '/usr/include/*' will not.", "type": "array", "items": { "type": "string" } } } } } } }, "env": { "type": "object", "description": "Custom variables that can be reused anywhere in this file using the ${variable} or ${env:variable} syntax.", "patternProperties": { "(?!^workspaceFolder$)(?!^workspaceRoot$)(?!^default$)(^.+$)": { "oneOf": [ { "type": "string" }, { "type": "array", "items": { "type": "string" } } ] } }, "additionalProperties": false }, "version": { "type": "integer", "description": "Version of the configuration file. This property is managed by the extension. Please do not change it." } }, "properties": { "configurations": { "$ref": "#/definitions/configurations" }, "env": { "$ref": "#/definitions/env" }, "version": { "$ref": "#/definitions/version" } }, "required": [ "configurations", "version" ] } ``` 至此,就配置完成了,编写C++程序,支持bits/stdc++.h,按F5调试/运行