Omit directory name when including header files in different folder than src

294 Views Asked by At

Suppose a folder structure in a C++ project with an include directory with header files,a src directory with .cpp files and the makefile. Lets assume header.h lives in /include,I want to be able to #include the header above without the ./include/ part. In my makefile I can add a -I./include flag,but my current problem is that (On VS Code) Clang complains about a pp_file_not_found,even though when compiling everything works fine. I am looking for either

  • Supressing this error
  • Somehow telling clang that /include/ should be searched for headers

What's the workaround for using my header.h like #include "header.h" in a situation like this?

Structure 
 -include 
    -header.h
 -src
    main.cpp
    makefile

Header.h :

#ifndef HEADER_H
#define HEADER_H
#include <iostream>
void foo(int a) { std::cout << a << '\n'; }
#endif  // HEADER_H

main.cpp

#include "header.h" // 'header.h' file not foundclang(pp_file_not_found)
int main() {
  foo(5);
  return 0;
}

makefile

all: main.o 
    clang++ main.o 

main.o: main.cpp ../include/header.h
    clang++ -c main.cpp -I../include
1

There are 1 best solutions below

1
ABacker On

Add a compile_flags.txt file, the clangd extension parses it:

-xc++
-I
./include

Or a compile_flags.json file, see clangd's description.