3

Heyo!

I have been using the Syntastic syntax checker plugin for a little while and have been really enjoying the experience except one irritating bug I cannot seem to figure out how to eliminate.

Every time I try to use NULL in my C programs, I get an error signified by the red ">>" and an error bubble saying "use of undeclared identifier 'NULL' [undefined:]"

>>Link to screenshot of issue<<

I have no issues using header files and the program compiles without any issues. It's just visually quite irritating to see a bunch of error flags when I am trying to implement data structures that use NULL pointers or NULL to signify free space.

I am using the gcc compiler and have set the compile options in my vimrc to:

let g:syntastic_c_compiler_options= '-std=c18'

(nothing changes if I remove the line from my vimrc and let it fall back to the default -gnu99 as set by the syntactic syntax-checkers config files in ~/.vim/plugged/syntastic/syntax_checkers/c/gcc.vim)

Output of :SyntasticInfo:

Syntastic version: 3.10.0-7 (Vim 802, Linux, GUI)
Info for filetype: c
Global mode: active
Filetype c is active
The current file will be checked automatically
Available checkers: gcc make
Currently enabled checkers: gcc

Was hoping someone on here had dealt with a similar issue or had some ideas for a workaround.

Thanks in advance!!

EDIT 1: Syntastic configuration commands in .vimrc


"Syntastic settings

let g:syntastic_always_populate_loc_list = 1
let g:syntastic_auto_loc_list = 1
let g:syntastic_check_on_open = 1
let g:syntastic_check_on_wq = 0

"C Settings

let g:syntastic_c_compiler = 'gcc'
let g:syntastic_c_checkers=['gcc']
let g:syntastic_c_include_dirs=['/usr/lib/gcc/x86_64-pc-linux-gnu/9.3.0/include']
let g:syntastic_c_auto_refresh_includes = 1
let g:syntastic_c_compiler_options= '-std=c18'
let g:syntastic_c_check_header = 1


Adding this to aid in diagnosis, must say thank you very much to everyone giving their input and trying to help me out!

EDIT 2:

I edited the compiler options to :

let g:syntastic_c_compiler_options= '-I/usr/lib/gcc/x86_64-pc-linux-gnu/9.3.0/include -Wall -std=c18'

based on a suggestion from @user3629249. Now the undeclared identifier 'NULL' error shows up in the :Errors window (this can be attributed to the -Wall flag). But progress is progress :). Just need to understand how to configure Syntastic to understand NULL is actually defined in <stddef.h>.

  • 2
    Can you list a small full program that reproduces this? Do you have an explicit include for `#include `? If not, can you add it and see if it fixes the warning? If you build your code passing gcc an extra `-Wall` flag, do you see the same warning? – filbranden May 02 '20 at 18:57
  • 2
    Syntastic's default settings are meaningless, you really need to adapt them to your project. For `gcc` you need to tell syntastic to use the same options that you're using to compile the file. See `:h syntastic-debug` for more information. – lcd047 May 02 '20 at 19:06
  • @filbranden Hey! Thanks for the response! It makes no differences whether I include stddef.h or not. The program has no problem compiling even with -Wall flag. Zero warnings or errors from the compiler. This appears to be something wrong with syntastic. For example I can open the same file in Visual Studio Code and I do not see any syntax errors. I can even peek the reference for NULL and see that it is defined in . – shallow_learning May 02 '20 at 19:13
  • @lcd047 Hey! Appreciate the quick response. I did read the debug doc earlier and am giving it another read now. I am not using any compiler flags at the moment when I compile my program as they are just little programs for practice. In my .vimrc file I have the recommended configuration lines and a couple more I added in attempts to fix this issue (auto_refresh_includes, check_header, and std=c11). As per my SyntasticInfo report it seems to be recognising and using gcc just fine. Is there something more specific I need to configure? Sorry If I didn't interpret your response correctly. – shallow_learning May 02 '20 at 19:38
  • You need to configure syntastic to use the exact same command you use when you compile the file. Otherwise you'll get different results, and you'll blame syntastic for it. :) Syntastic is a Vim script, it doesn't know (nor particularly cares) about C syntax. It's all about how you tell syntastic to run `gcc`, nothing more, nothing less. To see the command line constructed by syntastic you need to enable debugging and look at the logs. – lcd047 May 02 '20 at 20:06
  • @lcd047 Again I really appreciate your help! I hope I didn't sound like I was blaming syntastic, that's definitely not my intention :). My experience with the plugin has been really great. It works well for me and behaves as expected in C files except for in this one case when I am trying to use `NULL` . I have set `let g:syntastic_c_compiler = 'gcc' ` in my `vimrc`. I hope this is what you meant. I understand you are saying I should forward all my compile flags to syntastic but I am not using any additional flags and gcc doesn't mind NULL. Did you mean I should path to the header files? – shallow_learning May 02 '20 at 20:47
  • if you don't tell syntastic where to find the header files, then there will be lots of issues. – user3629249 May 02 '20 at 20:58
  • regarding: `let g:syntastic_c_compiler_options= '-std=c18'` This does not enable the warnings for the compiler. Suggest: `let g:syntastic_c_compiler_options= '-ggdb3 -Wall -Wextra -Wconversion -pedantic -std=c18'` The `-ggdb3` will optimize the output for the `gdb` (or `ddd`) debugger – user3629249 May 02 '20 at 21:00
  • @user3629249 Thanks for the insight, I will try this now. Another behavior of the issue I observed is that the NULL related undefined error doesn't show up in the :Errors window. It works fine for other errors like for example syntax errors such as not terminating with a semi colon. Just an observation. – shallow_learning May 02 '20 at 21:03
  • @user3629249 Giving a direct path to the stddef.h file didn't seem to help. I tested adding 'make' to the checkers list to see if it was reading it. The change was reflected in :SyntasticInfo so it appears it's reading the config commands just fine. I added all my config commands for Syntastic to the original post, perhaps it can help. Thanks again! – shallow_learning May 02 '20 at 21:23
  • 1
    Please don’t put the tags into the title; it’s completely redundant: that’s what *tags* are for. – Konrad Rudolph May 02 '20 at 21:29
  • @KonradRudolph Sorry bud, still developing my etiquette, it's my first post. Figured I should stop lurking and get more directly involved. – shallow_learning May 02 '20 at 21:45
  • @shallow_learning Absolutely nothing to apologise for! – Konrad Rudolph May 02 '20 at 21:46
  • regarding: `'/usr/lib/gcc/x86_64-pc-linux-gnu/9.3.0/include'` That is the includes for compiling the OS. I would expect your statement to be: `/usr/include` – user3629249 May 03 '20 at 05:41
  • That is the only location I could find where stddef.h . I tried copying it into the standard include directory as you suggested and sure enough, no longer getting bugged about using NULL! Thanks for all your help! – shallow_learning May 03 '20 at 11:31

1 Answers1

0

The problem appeared to be the stddef.h file was not being accessed by Syntastic as it was deep in a gcc folder (see above) and not in the /usr/include. Making a copy of stddef.h and placing it in /usr/include seems to have resolved the issue I was having regarding the usage of NULL. Hopefully this won't create any further issues ;D.

Thanks again to everyone for their input and I hope this can save someone some headache in the future :).

  • 2
    If you could compile the file without errors the problem is still that syntastic builds a different command line than you use for that file. Meaning, you'll probably run into the same issue again. _shrug_ – lcd047 May 03 '20 at 12:00
  • I realised this, I will work on doing it the less lazy way soon. – shallow_learning May 04 '20 at 16:40