6

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

8

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
      (add-to-list 
       (make-variable-buffer-local 'flycheck-clang-include-path)
       root)))

(add-hook 'c++-mode-hook 'setup-flycheck-clang-project-path)
2
  • 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
    Jun 10, 2015 at 14:22
  • I removed the when-let and replace it with a let and a when Jun 10, 2015 at 14:34
1

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.

0

Your Answer

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

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