Visual Studio Code (vscode)编译C++

Visual Studio Code (简称 VS Code / VSC) 是一款免费开源的现代化轻量级代码编辑器,支持几乎所有主流的开发语言的语法高亮、智能代码补全、自定义热键、括号匹配、代码片段、代码对比 Diff、GIT 等特性,可谓是微软的良心之作。

下载安装VS Code

点击下载链接,选择合适的安装程序

安装cpptools插件

打开VS Code,快捷键ctrl+shift+p呼出命令框,输入以下命令

ext install cpptools

稍等片刻会出现插件安装列表,如图:

点击箭头所指处的按钮安装插件,安装过程可能会有些慢耐心等待 ,安装完成后vscode会提示你重启vscode。

安装MINGW-W64

这里我建议安装mingw-w64,比较稳定。
在windows下安装完成后需要设置环境变量

c:\mingw-w64\bin\

配置调试环境

1.文件-->打开文件夹,设置项目路径

2.新建一个.vscode文件夹

3.创建一个launch.json启动配置文件

{
   "version": "0.2.0",
   "configurations": [
       {
           "name": "C++ Launch (GDB)",                
           "type": "cppdbg",                         
           "request": "launch",                        
           "targetArchitecture": "x86",                
           "program": "${workspaceRoot}\\${fileBasename}.exe",                 
           "miDebuggerPath":"C:\\mingw-w64\\bin\\gdb.exe", 
           "args": [],     
           "stopAtEntry": false,                  
           "cwd": "${workspaceRoot}",                  
           "externalConsole": true,                  
           "preLaunchTask": "g++"                    
           }
   ]
}

4.创建一个tasks.json的配置文件

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

5.创建一个c_cpp_properties.json文件,注意不同的版本的MINGW路径可能有些许不同

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceRoot}",
                "C:/mingw-w64/lib/gcc/x86_64-w64-mingw32/7.2.0/include/c++",
                "C:/mingw-w64/lib/gcc/x86_64-w64-mingw32/7.2.0/include/c++/x86_64-w64-mingw32",
                "C:/mingw-w64/lib/gcc/x86_64-w64-mingw32/7.2.0/include/c++/backward",
                "C:/mingw-w64/lib/gcc/x86_64-w64-mingw32/7.2.0/include",
                "C:/mingw-w64/lib/gcc/x86_64-w64-mingw32/7.2.0/include/c++/tr1",
                "C:/mingw-w64/x86_64-w64-mingw32/include"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "__GNUC__=6",
                "__cdecl=__attribute__((__cdecl__))"
            ],
            "intelliSenseMode": "msvc-x64",
            "browse": {
                "path": [
                    "${workspaceRoot}",
                    "C:/mingw-w64/lib/gcc/x86_64-w64-mingw32/7.2.0/include/c++",
                    "C:/mingw-w64/lib/gcc/x86_64-w64-mingw32/7.2.0/include/c++/x86_64-w64-mingw32",
                    "C:/mingw-w64/lib/gcc/x86_64-w64-mingw32/7.2.0/include/c++/backward",
                    "C:/mingw-w64/lib/gcc/x86_64-w64-mingw32/7.2.0/include",
                    "C:/mingw-w64/lib/gcc/x86_64-w64-mingw32/7.2.0/include/c++/tr1",
                    "C:/mingw-w64/x86_64-w64-mingw32/include"
                ]
            },
            "limitSymbolsToIncludedHeaders": true,
            "databaseFilename": ""
        }
    ],
    "version": 3
}

6.新建一个test.cpp文件,并点击左侧的调试按钮


    #include <iostream>
    using namespace std;

    int main()
    {
        cout<<"Hello World!"<<endl;
        return 0;
    }

运行结果

为了看到输出效果,我们可以在test.cpp的最后一行上设置一个断点。运行效果如下:

更新:

  1. 2017-12-20 MINGW-W64替代了之前的MINGW
  2. 2018-02-13 修复 了includePath,避免了提示cannot open source file "xxx.h"

参考资料

  1. C/C++ for VS Code
  2. c_cpp_properties.json template
posted @ 2017-08-20 08:07  ,,,沙子,,,  阅读(103464)  评论(30编辑  收藏