4

I'm using emacs with flycheck to check C source code syntax and trying to get it working with glib. My code compiles and runs correctly, however flycheck reports a file not found error in the #include <glib.h> line and stops reporting further errors, defeating its purpose.

Here's my sample source file:

#include <stdio.h>
#include <glib.h>

GList* list = NULL;

int main() {
    list = g_list_append(list, "a");
    list = g_list_append(list, "b");
    list = g_list_append(list, "c");

    for ( ; list!=NULL; list=list->next) {
        printf("%s\n", (char*)list->data);
    }
    return 0;
}

And the makefile

P=glist
OBJECTS=
CFLAGS=-g -Wall -O3 `pkg-config --cflags glib-2.0`
LDLIBS=`pkg-config --libs glib-2.0`
CC=gcc-4.9

$(P): $(OBJECTS)

If I change the include line to read #include <glib-2.0/glib.h> I get the following error reported in the minibuffer:

Checker c/c++-clang returned non-zero exit code 1, but no errors from output: In file included from /var/folders/f/ts3zs3cjbq1fqfhdfrl1w0000gn/T/flycheck87881gVK/glist.c:2: /usr/local/include/glib-2.0/glib.h:32:10: error: 'glib/galloca.h' file not found with include; use "quotes" instead In file included from /var/folders/_f/ts3_zs3cjbq1fqfhdfrl1w0000gn/T/flycheck87881gVK/glist.c:2: In file included from /usr/local/include/glib-2.0/glib.h:32: /usr/local/include/glib-2.0/glib/galloca.h:34:10: fatal error: 'glib/gtypes.h' file not found

Checker definition probably flawed.

Still the code compiles and runs correctly. I'm not sure why it can't find glib/gtypes.h as it exists under one of the included directories. The output from pkg-config --cflags glib-2.0 is:

-I/usr/local/Cellar/glib/2.36.4/include/glib-2.0 -I/usr/local/Cellar/glib/2.36.4/lib/glib-2.0/include -I/usr/local/opt/gettext/include

Listing ls /usr/local/Cellar/glib/2.36.4/include/glib-2.0/glib/gtypes.h

/usr/local/Cellar/glib/2.36.4/include/glib-2.0/glib/gtypes.h

And listing ls /usr/local/include/glib-2.0/glib/gtypes.h

/usr/local/include/glib-2.0/glib/gtypes.h

So the file is there. I wouldn't mind switching to flymake if flycheck is to blame, but I'm not sure if it's a problem with my setup or flycheck itself. Plus flycheck configuration is dead simple and otherwise works very well. I'm using version 20130904.2245 installed from elpa.

Cesar
  • 5,488
  • 2
  • 29
  • 36

1 Answers1

6

Flycheck does not use Makefiles, nor does it attempt to parse them. I cannot help but wonder how you even got this idea, given that no such behaviour is documented in the manual, and no syntax checker for make files even exists.

Flycheck runs Clang directly. You need to explicitly configure the include path for syntax checking by setting flycheck-clang-include-path accordingly. You must do so yourself, this is not done automatically based on your Makefile.

You can set the path via file/dir local variables, or write some custom Emacs Lisp code to parse your Makefile.

  • Thanks, that's the lead I needed. I created a .dir-locals.el and set the value of flycheck-clang-include-path to the glib directories reported by pkg-config and it works. – Cesar Sep 09 '13 at 15:44
  • @lunaryorn: is it normal when flycheck with clang checker does not recognize the included files on the same dir of the checked file? – rano Sep 28 '13 at 11:30
  • @rano I do not know. Without a specific example, I cannot say. Please open an issue if you think Flycheck behaves wrongly. –  Sep 29 '13 at 13:45
  • @lunaryon: in the same directory I have myfile.h and myfile.cpp with the last having `include "myfile.h"`. Flycheck with clang as a checker gives an error saying 'Cannot find file myfile.h'. Specifying the dir with the local var flycheck-clang-include-path gives no error. I will later open an issue, even tough I am not yet sure whether that is an 'acceptable' behaviour – rano Sep 29 '13 at 15:27
  • 1
    @rano As I said, please open an issue. I do not consider this discussion a good fit for Stack Overflow. –  Sep 29 '13 at 15:31