How do I specify the include path when I build a program in VSCode?
Asked Answered
U

2

10

Let's say this is my project.

file structure:

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

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

Question

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

What I did already

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.

What's not going to be a solution for me

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

Unharness answered 14/2, 2020 at 2:35 Comment(0)
U
12

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"
}
Unharness answered 14/2, 2020 at 3:40 Comment(1)
I did this and it still says: "No such file or directory".Bradley
G
0

In args part add the following:

"-I",
"${workspaceFolder}/inc",

Your tasks.json should look something like this:

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

It is the same as KIYZ's answer, except that the added args should be added to two separate lines.

Gilpin answered 15/5 at 11:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.