Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 778
KIYZ Avatar asked Feb 14 '20 02:02

KIYZ


People also ask

How do I add the include path in Visual Studio?

Adding The Include Directory Go to the Visual Studio Project Property Pages dialog (From the Project menu, select Properties, or right-click on the project in the Solution Explorer). Select Configuration Properties, C/C++, General, and then add $(PIXELINK_SDK_ROOT)\include to the Additional Include Directories field.

How do I select a path in VS Code?

One option is Ctrl + Shift + E to select the Explorer, use the arrow keys to navigate to the target location, and then click the New File or New Folder button.

What is the include path?

includePath An include path is a folder that contains header files (such as #include "myHeaderFile. h" ) that are included in a source file. Specify a list of paths for the IntelliSense engine to use while searching for included header files.


Video Answer


1 Answers

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"
}
like image 114
KIYZ Avatar answered Oct 14 '22 04:10

KIYZ