Including header files recursively for syntastic
Asked Answered
X

3

20

I'm working with a C++ project and trying to configure it to use syntastic. In my project I have a nested directory structure of header files (The actual nested structure is much worse, this is an example).

--libs    
    |---dir1
         |---foo1.h    
    |---dir2
         |---foo2.h
         |---foo3.h
         |---dir3
               |---foo4.h

I have included the lib files in my .vimrc file using:

let g:syntastic_cpp_include_dirs = [ 'libs/']

I assumed this would take all the header files recursively, but it doesn't. In the code, syntastic complains with the error 'no such file or directory found'.

When I explicitly change the variable to refer to a specific directory:

let g:syntastic_cpp_include_dirs = [ 'libs/dir2/dir3/']

it works.

My questions:

  1. How do you configure syntastic so that it includes header files of a set of directories recursively?
  2. How do you do this for multiple projects? Always editing the .vimrc as I switch the project I'm working on doesn't sound right. I believe there must be a better way.

EDIT:

I didn't mention that in my .vimrc, the following options are present for syntastic:

let g:syntastic_check_on_open=1
let g:syntastic_enable_signs=1
let g:syntastic_cpp_include_dirs = ['libs/dir2/dir3', 'libs/dir2 ]
let g:syntastic_cpp_check_header = 1
let g:syntastic_cpp_remove_include_errors = 1
Xenophanes answered 18/5, 2013 at 10:2 Comment(2)
Have you tried let g:syntastic_cpp_check_header = 1?Ethiopian
Yes, I do. I have added the config options I had to the questions now. Sorry about that.Xenophanes
A
36

You can include all the directories to be searched for header files per project in the project root directory in a file .syntastic_cpp_config. The format for doing so would the same as providing the -I directives to the compiler.

For your case it means:

  • Create a file .syntastic_cpp_config under sources (assuming that's where your code is and sources is at a same depth level in the directory hierarchy as libs).
  • Put the following lines in it:

    -Ilibs/dir1

    -Ilibs/dir2

    -Ilibs/dir2/dir3

  • Note the the flags are 1 per line.

  • This way you don't have to have all the include directories in your .vimrc.

You can have a different file to hold this custom configuration per project, specified by the .vimrc global variable g:syntastic_cpp_config_file, eg

let g:syntastic_cpp_config_file = '.my_custom_include_file_for_syntastic'

Syntastic will check each source directory and upwards until it finds this file and then use it for producing its output.

See the Syntastic wiki page, Old link for more details.

Amygdalin answered 2/10, 2013 at 18:14 Comment(3)
This is out of date as of a 2018 May 24 update. .syntastic_cpp_config is no longer "magic" and you must explicitly set with let g:syntastic_cpp_config_file = ...Modestamodeste
Maybe I'm being pedantic, and this might be a wrong place to mention/ask this, but: Is it considered clean to have a file in the project folder just for this purpose? While it could be added to .gitignore for a project you're working on, I would not expect other developers to also be using Syntastic.Bultman
But if others are also using Syntastic it would be helpful for them as well. And there is no harm if they're not; it is a dotfile after all.Ingest
M
0

I've had the same question with little luck. However, I've found that if I use the quotation mark style header includes, syntactic will appropriately check the folders and not issue warnings. For example, if you're working on foo2.cpp,

#include "dir3/foo4.h"
#include "../dir1/foo1.h"

Save bracket includes for standard libs and any libs you feel like hardcoding into vim.

Macbeth answered 30/5, 2013 at 3:8 Comment(0)
W
0

For anyone looking for a quick answer in the future, try this in your root project directory:

find . -type f -regex '.*\.h\(pp\)?$'    |\
    sed -E 's|/[a-zA-Z0-9_-]*.h(pp)?$||' |\
    sed 's|^|-I|'                        |\
    sort | uniq > .syntastic_cpp_config

This will recursively find the directory of every file ending in .h or .hpp from the current working directory, append an -I in front, and output it to a file called .syntastic_cpp_config.

Also, as stated by other answers, make sure to add this to your .vimrc!

let g:syntastic_cpp_config_file = .syntastic_cpp_config
let g:syntastic_cpp_check_header = 1
Whim answered 29/5 at 17:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.