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

user3717819 picture user3717819 · Jun 7, 2014 · Viewed 14.7k times · Source

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.

Answer

lunaryorn picture lunaryorn · Jun 8, 2014

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.