Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 362
user3717819 Avatar asked Jun 07 '14 13:06

user3717819


2 Answers

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.

like image 175
nephewtom Avatar answered Sep 28 '22 02:09

nephewtom


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.

like image 32
lunaryorn Avatar answered Sep 28 '22 02:09

lunaryorn