Home > Enterprise >  Does anyone have any advice on how to avoid errors in Vscode for putting header files in a separate
Does anyone have any advice on how to avoid errors in Vscode for putting header files in a separate

Time:06-16

Ok so I am having an issue with errors in VSCode. Basically I decided to reorganize and move my header files into a separate folder, "include". My directory put simply is as follows:

-build
-include
 |-SDL2
 |-SDL2_Image
 |-someHeaderFile1.h
 |-someHeaderFile2.h
-src
 |-main.cpp
 |-someCppFile.cpp
-Makefile

My Makefile contains:

SRC_DIR = src
BUILD_DIR = build/debug
CC = g  
SRC_FILES = $(wildcard $(SRC_DIR)/*.cpp)
OBJ_NAME = play
INCLUDE_PATHS = -Iinclude -I /include
LIBRARY_PATHS = -Llib
COMPILER_FLAGS = -std=c  11 -Wall -O0 -g
LINKER_FLAGS = -lsdl2 -lsdl2_image

all:
    $(CC) $(COMPILER_FLAGS) $(LINKER_FLAGS) $(INCLUDE_PATHS) $(LIBRARY_PATHS) $(SRC_FILES) -o $(BUILD_DIR)/$(OBJ_NAME)

The program compiles and runs, however, my issue is with VSCode as it shows an error having the include as : #include "someHeaderFile1.h" vs #include "../include/someHeaderFile1.h"

Any assistance would be appreciated.

CodePudding user response:

You need to put that folder's path to the Include path. One way to do that is shown below. The screenshots are attached with each steps so that it(the process) would be more clear.

Step 1

Press Ctrl Shift P

This will open up a prompt having different options. You have to select the option saying Edit Configurations

s1

Step 2

After selecting Edit Configurations a page will open with different options. You have to scroll down and go the the option saying Include Path and just paste the path to your include folder there.

s2

Below is the picture after adding the include folder's path into the Include Path option.

s3

Step 3

Now after adding the path to the include folder into the Include path field you can close this window and all the vscode errors that you mentioned will not be there anymore.

CodePudding user response:

If you have install Microsoft C/C extension properly, and the directory you show is the root path of your VSCode workspace, you can add Include path options in C/C : Edit configurations (UI), or edit .vscode/c_cpp_properties.json like:

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                // Add your custom include path here
                "${workspaceFolder}/include/**",
            ],
            "defines": [],
            "compilerPath": "/usr/bin/g  ",
            // ...other options
        }
    ],
    "version": 4
}

For more details refer to the document.

  • Related