在 VSCode 中构建程序时如何指定包含路径? [英] How do I specify the include path when I build a program in VSCode?

查看:82
本文介绍了在 VSCode 中构建程序时如何指定包含路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文件结构:

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: 致命错误:'header.h' 文件未找到

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

How do I avoid this error?
(How do I let the VSCode's build feature know where my header is?)

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

I configured my include path(s) in c_cpp_properties.json, so I'm not getting the squiggles in main.c, where I include my header.

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

I don't want to write #include "../inc/header.h" in main.c, so this would not be a solution for me.

推荐答案

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

specify the include paths in tasks.json, under the args property, using the -I flag.

{
    "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"
}

这篇关于在 VSCode 中构建程序时如何指定包含路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆