VSCode 编译头文件需要的配置

需要更改两个文件中的内容

c_cpp_properties.json

在 includepath 中添加头文件所在路径

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                // add your headfile path here
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "10.0.19041.0",
            "compilerPath": "D:\\Program File\\Microsoft Visual Studio\\2019\\Community\\VC\\Tools\\MSVC\\14.28.29910\\bin\\Hostx64\\x64\\cl.exe",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "windows-msvc-x64"
        }
    ],
    "version": 4
}

注意,在此处添加的 path 并不能被编译器所获取,只能在编辑界面有所帮助

tasks.json

在第一个 args 中添加头文件所在文件夹的路径,若有实现,需要添加实现的 .cpp 文件

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "task g++",
            "command": "C:\\Program Files\\mingw64\\bin\\g++.exe",
            "args": [
                "-g",
                "${file}",
                "-I",  // 添加
                "E:\\My Library\\Note\\Algorithms\\fundamental\\lib", // 头文件路径
                "E:\\My Library\\Note\\Algorithms\\MyAlgorithmsTimer.cpp", // 实现的路径
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "调试器生成的任务。"
        },
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe 生成活动文件",
            "command": "C:\\Program Files\\mingw64\\bin\\g++.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build",
            "detail": "编译器: \"C:\\Program Files\\mingw64\\bin\\g++.exe\""
        }
    ],
    "version": "2.0.0"
}

这个文件就是个脚本帮你干了命令行里要做的事情,可以在 console 中看到脚本的效果

Q.E.D.


To baldly -_- go no man has gone before