Visual Studio Code编写运行C++

       刚开始用vscode(windows的),不知道怎么配置,甚至都不知道如何编译调试。在网上搜了好多教程,但都是或多或少有些瑕疵,老是无法编译运行。我总结了一下前辈们的经验(复制了他们的配置),相互结合了一下,终于弄好了。我只给出三个文件的配置吧,不是我的,不是我的,不是我的,我只是普通的搬运工,侵删!

       貌似运行是F5,调试是加断点后再F5。运行控制台窗口可能闪退,在return前加一句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
}

可能用着用着就出问题了,当调试不了的时候,看看是否把环境变量删了,当报错函数未定义时,重新配置一下c_cpp_properties.json ,我只是把"C:/MinGW/include"调换了一下位置就好了又。

猜你喜欢

转载自blog.csdn.net/mbest6/article/details/81664954