How to add include path to flycheck c/c++-clang?

16.6k Views Asked by At

I tried to add include path to flycheck c/c++-clang, but it didn't work.

I put foo.h in ~/local/include and added the following lines to init.el:

(add-hook 'c++-mode-hook
          (lambda () (setq flycheck-clang-standard-library "libc++")))
(add-hook 'c++-mode-hook
          (lambda () (setq flycheck-clang-language-standard "c++1y")))
(add-hook 'c++-mode-hook
          (lambda () (setq flycheck-clang-include-path
                           (list "$HOME/local/include/"))))

And in a file called test.cpp I wrote

#include <foo.h>

flycheck said that

'foo.h' file not found

What am I doing wrong? I'm using emacs24, flycheck.el from package.el and clang3.4.

2

There are 2 best solutions below

6
On BEST ANSWER

Use expand-file-name and ~ to refer to paths in your home directory:

(add-hook 'c++-mode-hook
          (lambda () (setq flycheck-clang-include-path
                           (list (expand-file-name "~/local/include/")))))

Flycheck does not use the system shell to run Clang, nor does it otherwise attempt to expand shell parameters in command lines. Hence, $HOME is passed literally to Clang, which does obviously not work.

1
On

I do not want to get credit by this answer but it could be useful to someone.
Using the accepted answer and comments to set flycheck variable with Directory variables:

You have a project with the C++ source code in ~/myproject.
Add the file ~/myproject/.dir-locals.el with the following content:

((nil . ((eval . (setq flycheck-clang-include-path
                       (list (expand-file-name "~/myproject/include/")))))))

That worked for me.