在VSCode中构建程序时如何指定包含路径?

问题描述 投票:0回答:1

让我们说这是我的项目。

文件结构:

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中包含我的标头的花样。

对我来说这不是解决方案

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

让我们说这是我的项目。文件结构:project_root |-inc | |-header.h |-src | |-helpers.c | |-main.c header.h #ifndef HEADER_H#定义HEADER_H void func(void); #...

c visual-studio-code compiler-errors vscode-settings vscode-tasks
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.