Vscode配置cpp外部头文件依赖

Sat, Feb 6, 2021 阅读时间 1 分钟

最近尝试在vscode中配置加载外部的hpp头文件依赖,记录如下:

vscode的settings.json中可以通过配置项C_Cpp.default.includePath配置默认的头文件路径,配置如下:

{
    "files.associations": {
        "iostream": "cpp"
    },
    "C_Cpp.default.includePath": ["/home/psy"]
}

在配置文件c_cpp_properties.json配置文件中可以指定includePath,示例如下:

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${default}"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/clang++",
            "cStandard": "c11",
            "cppStandard": "c++14",
            "intelliSenseMode": "linux-clang-x64"
        }
    ],
    "version": 4
}

其中includePath配置的为${default},表示加载前面settings.json中的C_Cpp.default.includePath配置。也可以直接在此配置其他路径。 经过上面两个配置,项目中无法找到头文件的错误提示基本上就没有了。比如在/home/psy下存在a.hpp

#include <iostream>
void printHello() {
    std::cout << "Hello cpp!" << std::endl;
}

在项目中的hello.cpp:

#include <iostream>
#include <a.hpp>

int main()
{
    std::cout << "Hello C++!" << std::endl;
    printHello();
    return 0;
}

这个<a.hpp>printHello就能被vscode正常找到和跳转。

但是如果此时直接F5运行,依旧会出现找不到a.hpp的情况出现。因为运行时执行的是launch.json配置:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "clang++ - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "console": "externalTerminal",
            "MIMode": "lldb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: clang++ build active file",
            "miDebuggerPath": "/usr/bin/lldb-mi"
        }
    ]
}

其中preLaunchTask指定了需要先运行的task,这个在tasks.json中配置:

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: clang++ build active file",
            "command": "/usr/bin/clang++",
            "args": [
                "-g",
                "${file}",
                "-I",
                "/home/sypeng",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

需要在其中args中添加头文件的路径依赖(使用-I参数,如果是添加库路径依赖需要使用-L参数),这样F5运行时就能正常执行了。 需要注意的是,F5执行的是当前鼠标选中的文件,需要选中cpp文件(比如hello.cpp),然后F5执行。