0

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.

Marcus Müller
  • 34,677
  • 4
  • 53
  • 94
spencer
  • 74
  • 8

2 Answers2

2

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.

Jason
  • 36,170
  • 5
  • 26
  • 60
  • i did try to use this same method for the Qt headers it wont work, do i need to add the exact paht like ```C:/Qt/Qt5_11/..../QtCore``` ?? or where should i add the Qt related paths? – lightshadown Jun 18 '22 at 17:14
  • @lightshadown The one i showed in the above screenshot uses full path like. But you can also use relative path. – Jason Jun 18 '22 at 17:20
1

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.

rustyhu
  • 1,912
  • 19
  • 28