【问题标题】:How do I specify the include path when I build a program in VSCode?在 VSCode 中构建程序时如何指定包含路径?
【发布时间】:2020-05-29 20:00:45
【问题描述】:

假设这是我的项目。

文件结构:

project_root
|-- inc
|   |-- header.h
|-- src
|   |-- helpers.c
|   |-- main.c

header.h

#ifndef HEADER_H
# define HEADER_H

void    func(void);

#endif

helpers.c

void    func()
{
    /* do something */
}

main.c

#include "header.h"

int    main(void)
{
    func();
    return (0);
}

c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Mac",
            "includePath": [
                "${workspaceFolder}/inc",
            ],
            "defines": [],
            "macFrameworkPath": [
                "/System/Library/Frameworks",
                "/Library/Frameworks"
            ],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "gcc-x64"
        }
    ],
    "version": 4
}

tasks.json

    "tasks": [
        {
            "type": "shell",
            "label": "gcc build active file",
            "command": "/usr/bin/gcc",
            "args": [
                "-g",
                "-Wall",
                "-Werror",
                "-Wextra",
                "-o0"
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}",
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "group": {
            "kind": "build",
            "isDefault": true
            },
        }
    ],
    "version": "2.0.0"
}

问题

当我在 VSCode 中构建程序时,出现以下错误。
project_root/src/main.c:xx:xx: fatal error: 'header.h' file not found

问题

如何避免此错误?
(如何让 VSCode 的构建功能知道我的标题在哪里?)

我已经做了什么

我在c_cpp_properties.json 中配置了我的包含路径,所以我没有在main.c 中得到曲线,我在其中包含了我的标题。

什么对我来说不是解决方案

我不想在main.c 中写#include "../inc/header.h",所以这对我来说不是一个解决方案。

【问题讨论】:

    标签: c visual-studio-code compiler-errors vscode-settings vscode-tasks


    【解决方案1】:

    使用-I 标志在args 属性下指定tasks.json 中的包含路径。

    {
        "tasks": [
            {
                "type": "shell",
                "label": "gcc build active file",
                "command": "/usr/bin/gcc",
                "args": [
                    "-g",
                    "-Wall",
                    "-Werror",
                    "-Wextra",
                    "-o0",
                    "-I${workspaceFolder}/inc",
                    "${file}",
                    "-o",
                    "${fileDirname}/${fileBasenameNoExtension}",
                ],
                "options": {
                    "cwd": "${workspaceFolder}"
                },
                "group": {
                "kind": "build",
                "isDefault": true
                },
            }
        ],
        "version": "2.0.0"
    }
    

    【讨论】:

    • 我这样做了,它仍然说:“没有这样的文件或目录”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-21
    • 2021-11-20
    • 2011-07-01
    • 2022-10-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多