美文网首页
VSCode编译运行C++(for Windows)

VSCode编译运行C++(for Windows)

作者: BackInAction | 来源:发表于2018-01-17 22:40 被阅读0次

1.下载安装VSCode

https://code.visualstudio.com/Download

2.扩展中安装c++

扩展.png

3.安装编译调试环境

  • 下载安装MinGW
    Download mingw-get-setup.exe (86.5 kB)
    图片.png
  • 选择gdb的部分右键Make for Installation进行标记 -> 左上角Installation -> Apply Changes进行安装(error则需要翻墙)
  • 配置环境变量


    配置环境变量.png

4. VSCode配置

  • 新建一个文件夹并打开,创建一个main.cpp文件
#include <iostream>
using namespace std;

int main()
{
    cout <<"hello" << endl;
    return 0;
}
  • F5调试弹出选择运行环境,选择C++(GDB/LLDB)


    图片.png
  • .vscode文件夹里面自动创建了launch.json文件
launch.json
{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "enter program name, for example ${workspaceFolder}/a.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "/path/to/gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

添加 "preLaunchTask": "build"
更改 "program": " ${file}.exe",
gdb路径根据你的安装路径来改 "miDebuggerPath": "C:\mingw\bin\gdb.exe",

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": " ${file}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "preLaunchTask": "build",
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\mingw\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}
  • F5 提示找不到任务,选择配置任务


    图片.png

    配置任务


    图片.png
    模板选择 Others
    图片.png

    得到task.json文件

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "echo",
            "type": "shell",
            "command": "echo Hello"
        }
    ]
}

改为如下

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "g++",
            
            "args": [
                "-g",
                "${file}",
                "-o",
                "${file}.exe"
            ],
        }
    ]
}
  • 配置include路径


    图片.png

    鼠标点击波浪线部分,出现小灯泡,点击小灯泡选择配置文件


    图片.png
    得到c_cpp_properties.json文件,,修改win32部分的includePath
        {
            "name": "Win32",
            "includePath": [
                "${workspaceRoot}",
                "C:/MinGW/lib/gcc/mingw32/6.3.0/include/c++",
                "C:/MinGW/lib/gcc/mingw32/6.3.0/include/c++/mingw32",
                "C:/MinGW/lib/gcc/mingw32/6.3.0/include/c++/tr1",
                "C:/MinGW/include",
                "C:/MinGW/lib/gcc/mingw32/6.3.0/include"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE"
            ],
            "intelliSenseMode": "msvc-x64",
            "browse": {
                "path": [
                    "${workspaceRoot}",
                    "C:/MinGW/lib/gcc/mingw32/6.3.0/include",
                    "C:/MinGW/include"
                ],
                "limitSymbolsToIncludedHeaders": true,
                "databaseFilename": ""
            }
        }

踩过的一些坑

  • gcc支持openMP
    但是一定要在MinGW Installation Manager中安装相应库


    图片.png

相关文章

网友评论

      本文标题:VSCode编译运行C++(for Windows)

      本文链接:https://www.haomeiwen.com/subject/slhpoxtx.html