0

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
drescherjm
  • 10,365
  • 5
  • 44
  • 64
laegirl
  • 144
  • 13
  • 1
    Since it's saying `pp_file_not_found`, I'm guessing it's _not_ using your `makefile`. – Ted Lyngmo Mar 22 '22 at 16:37
  • To the best of my recollection, VSCode uses `compile_commands.json` https://clang.llvm.org/docs/JSONCompilationDatabase.html it definitely doesn't parse your makefile. – MadScientist Mar 22 '22 at 16:41
  • @drescherjm I have the extension installed,I havent really configured it though. I just have a simple makefile with a simple rule e.g. `g++ main.cpp -I./include/` which compiles the program fine. Can I configure the extension with clang to avoid this behavior? – laegirl Mar 22 '22 at 16:44
  • I added the code and the structure in my last added for clarity – laegirl Mar 22 '22 at 16:54
  • I can't help with the Makefile tools extension. It's supposed to generate and configure your c_cpp_properties.json to avoid the intellisense warnings. [https://marketplace.visualstudio.com/items?itemName=ms-vscode.makefile-tools](https://marketplace.visualstudio.com/items?itemName=ms-vscode.makefile-tools) ***This extension provides IntelliSense configurations to the VS Code C/C++ Extension for Makefile projects. It also provides convenient commands to build, debug, and run your targets.*** – drescherjm Mar 22 '22 at 17:08
  • Since I deleted a previous comment I put the link back in the latest comment for context. – drescherjm Mar 22 '22 at 17:13
  • @drescherjm Im on Arch so I dont have C/C++. I use the [Code-OSS](https://github.com/microsoft/vscode) and C/C++ extension isnt available,i guess this means that the Makefile Tools is useless? – laegirl Mar 22 '22 at 17:15
  • I can't answer that. I hope someone with more experience will answer. – drescherjm Mar 22 '22 at 17:19
  • I don't understand this question at all. If the header file is not found, then definitely the compile will fail, but you say "even though when compiling everything works fine". Please show, via cut and paste with formatting, the exact make command you invoked, the exact compile line that make invoked, and the exact error message(s) you received. Also, I don't understand your comment "I'm on Arch so I don't have C/C++"; do you mean Arch Linux? All Linux systems have C/C++ compilers available for them, else they could not be built! – MadScientist Mar 23 '22 at 12:57
  • @MadScientist C/C++ extension for Vscode isnt available on Arch linux. I have g++ and clang++ ,thats not what i meant. Compiling the code via terminal with `g++ main.cpp -I../include` compiles just fine. But thats because I use the `-I` flag to include the header,when writing the code its annoying to deal with clang complaining about header not found. And i dont want to use `#include "../include/header.h"`,i want to use `#include "header.h"` – laegirl Mar 23 '22 at 14:30
  • Please show the output I asked for. I doubt it's really true that extensions are not available for Arch: most likely you just have to figure out how to install them. But, that's irrelevant here: if your makefile is not working we need to see what it does because the info you've provided here looks correct... so there must be something you haven't shown us that's causing the problems. – MadScientist Mar 23 '22 at 14:38

1 Answers1

0

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

-xc++
-I
./include

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

ABacker
  • 190
  • 4