11

I know this one way of adding include paths to clang:- clang++ -I <dir> a.cpp

but with this, that path is only added for that particular file, and u have to write that every time linting, so how can I add some include paths globally to clint.

Vishnubly
  • 539
  • 1
  • 8
  • 17
  • 1
    You could use a build system generator like CMake or write your own build system using Makefiles. That's what people typically do. If you feel particularly lazy/flamboyant you could write a shell script. – Aleksander Bobiński Sep 07 '20 at 18:43
  • hmm actually I am kind of new to this, @AleksanderBobiński do you have any reference of how to do this with build system generator – Vishnubly Sep 07 '20 at 18:46
  • Have a look at the CMake tutorial. It will help you in the long run. https://cmake.org/cmake/help/latest/guide/tutorial/index.html – Aleksander Bobiński Sep 07 '20 at 18:47

1 Answers1

18

There are also some environment variables which Clang looks at for include paths. For c++, they would be CPATH (both C and C++) and CPLUS_INCLUDE_PATH (C++ only) (And LIBRARY_PATH for the linker). So you can add something like this to your shell startup file if you are using bash or similar:

export CPLUS_INCLUDE_PATH="${CPLUS_INCLUDE_PATH:+${CPLUS_INCLUDE_PATH}:}<dir>"

And you could also just alias clang++ with clang++ -I<dir>.

Artyer
  • 31,034
  • 3
  • 47
  • 75
  • wouldn't I still need to add the config file every time linting? and ```clang++ -I``` does not work without specifying cpp. – Vishnubly Sep 07 '20 at 19:31
  • @Vishnu you're right, their not global configs like I thought they were. You can still use the `CPLUS_INCLUDE_PATH` variable though, and the `alias clang++='clang++ -I'` means that `clang++ a.cpp` becomes `clang++ -I a.cpp` so you don't have to write the include – Artyer Sep 07 '20 at 19:51