Visual Studio Code编写运行C++

  •        刚开始用vscode(windows的),不知道怎么配置,甚至都不知道如何编译调试。在网上搜了好多教程,但都是或多或少有些瑕疵,老是无法编译运行。我总结了一下前辈们的经验(复制了他们的配置),相互结合了一下,终于弄好了。我只给出三个文件的配置吧,不是我的,不是我的,不是我的,我只是普通的搬运工,侵删!
  •        貌似运行是F5,调试是加断点后再F5。运行控制台窗口可能闪退,在return前加一句system("Pause");就好了
  1. 首先下载MinGW,或者直接使用某些IDE(譬如CodeBlocks)自带的MinGW。具体可参考MinGW安装。嫌麻烦的可直接在CodeBlocks的安装目录里找到MinGW。
  2. 在vscode里打开一个文件夹,再新建一个子文件夹命名为.vscode,把下边的三个配置文件放在里面即可,我给出两个launch.json供选择,一个运行必须加system("Pause")用来暂停窗口,另一个给出两种编译方式既可以和第一个一样,也可以正常工作(窗口不闪退)但不能调试。参考

launch.json

// Available variables which can be used inside of strings.
// ${workspaceRoot}: the root folder of the team        
// ${file}: the current opened file                     
// ${fileBasename}: the current opened file's basename 
// ${fileDirname}: the current opened file's dirname    
// ${fileExtname}: the current opened file's extension  
// ${cwd}: the current working directory of the spawned process
{
    "version": "0.2.0",
    "configurations": [{
        "name": "C++ Launch (GDB)", // 配置名称,将会在启动配置的下拉菜单中显示
        "type": "cppdbg", // 配置类型,这里只能为cppdbg
        "request": "launch", // 请求配置类型,可以为launch(启动)或attach(附加)
        "targetArchitecture": "x86", // 生成目标架构,一般为x86或x64,可以为x86, arm, arm64, mips, x64, amd64, x86_64
        "program": "${file}.exe", // 将要进行调试的程序的路径

        "miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe", // miDebugger的路径,注意这里要与MinGw的路径对应

        "args": [], // 程序调试时传递给程序的命令行参数,一般设为空即可
        "stopAtEntry": false, // 设为true时程序将暂停在程序入口处,一般设置为false
        "cwd": "${fileDirname}", // 调试程序时的工作目录,一般为${workspaceRoot}即代码所在目录
        "externalConsole": true, // 调试时是否显示控制台窗口,一般设置为true显示控制台
        "preLaunchTask": "g++"   // 调试会话开始前执行的任务,一般为编译程序,c++为g++, c为gcc
    }]
}

tasks.json

{
    "version": "2.0.0",
    "command": "g++",
    "args": ["-g","${file}","-o","${file}.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
        }
    }
}

c_cpp_properties.json 

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                "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",
                "C:/MinGW/lib/gcc/mingw32/6.3.0/include",
                "C:/MinGW/lib/gcc/mingw32/6.3.0/include"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "cStandard": "c11",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}

第二个launch.json 

3. 接下来就可以愉快的使用啦。

可能用着用着就出问题了,当调试不了的时候,看看是否把环境变量删了,当报错函数未定义时,重新配置一下c_cpp_properties.json文件。我只是把"C:/MinGW/include"调换了一下位置而且把路径里的 "/" 替换成 "\\" 就好了又;或者是includePath参数某一行出路径现问题直接删除(一行或全部)也没影响,还能变得清净。文件夹好像必须是小写字母,有些问题需要重启电脑才能解决。

遇到代码不报错的情况下设置C_Cpp.errorSquiggles项,并点击一下旁边的齿轮按钮重置。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值