14

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.

itsjeyd
  • 5,070
  • 2
  • 30
  • 49
user3717819
  • 175
  • 1
  • 1
  • 6

2 Answers2

13

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
    Is there any way to do that sort of configuration on a per-project basis? – Magnus Sep 09 '15 at 18:31
  • @Magnus Emacs offers Directory Variables for this purpose. See the manual for more information. –  Sep 09 '15 at 18:35
  • Yes, that's an option of course, but it's rather limited; only variable values are allowed, and `flycheck-clang-include-path` treats realitve paths relative to the file being checked, which means that more complicate organizations of code are hard to support. – Magnus Sep 09 '15 at 19:02
  • 1
    @Magnus You can use `eval` to run arbitrary code from directory variables, and you can use absolute paths to avoid issues with relative paths. Beyond that, Flycheck has no particular support for “per-project” settings, and won't have any time soon, since there's no concept of “project” in Emacs. –  Sep 10 '15 at 08:26
  • 1
    Thanks, that gave me enough info to find this: http://stackoverflow.com/questions/16237506/wrong-type-argument-listp-eval-after-load-in-dir-locals-el – Magnus Sep 10 '15 at 10:26
  • @Magnus can you share what you figured out? I have a source tree that builds a couple node modules and I'd like an easy-ish way to drop a directory local file in two places to get the include paths right – chad Apr 17 '19 at 00:50
9

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.

nephewtom
  • 2,941
  • 3
  • 35
  • 49
  • 1
    See also [here](https://stackoverflow.com/a/44357963/161979) for a (slightly) more involved example. – mzuther Apr 28 '20 at 12:28