I want to use syntax checking with Flycheck in my C++ projects, but the Clang checker fails to find the local file includes. For example, when I have:

#include "foo/bar/x.h"

in the file foo2/y.cc, it reports an error that it couldn't find the include file. Both foo/ and foo2/ are in the project root. I can work around this by adding the following to a .dir-locals.el file in the project:

((c++-mode .
     ( (flycheck-clang-include-path . "/home/user/src/myproject") ) )

However, I have dozens of projects and it would be a lot of work to set this for each directory. I'm using Projectile and I think it should be possible to set the include path based on the project root automatically, but I cannot find any variable or function in Projectile that would return the path to the current project.

2 Answers 2


Use projectile-project-root to get the root path and add it to the list with a c++-mode-hook

(defun setup-flycheck-clang-project-path ()
  (let ((root (ignore-errors (projectile-project-root))))
    (when root
       (make-variable-buffer-local 'flycheck-clang-include-path)

(add-hook 'c++-mode-hook 'setup-flycheck-clang-project-path)
  • I don't seem to have a function called "when-let" (symbol's function definition is void). I don't know Lisp very well...how can I replace it?
    – mkollaro
    Commented Jun 10, 2015 at 14:22
  • I removed the when-let and replace it with a let and a when Commented Jun 10, 2015 at 14:34

You could wait for this pull request to get merged. This adds a new clang-check based checker to flycheck. The main difference is the tooling supports a compilation database which contains all the include paths (and defines and other compilation controls) for the file. This database can be automatically created with CMAKE or you can use a tool like bear to create it.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.