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

查看:2015
本文介绍了在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: fatal error: 'header.h' file not found

Issue

When I build my program in VSCode, I get the following error.
project_root/src/main.c:xx:xx: fatal error: 'header.h' file not found

如何避免此错误?
(如何让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.

推荐答案

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

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天全站免登陆