Search code examples
cmakefilegtk3

'gtk/gtk.h' File not found when using makefile to build gtk project


When I build my project manually it builds correctly:

gcc main.c -o midget `pkg-config --cflags --libs gtk+-3.0`

When I try to use a Makefile, it fails. For clarity, I have split up the code into smaller files in the src directory (only the main file is at root, along with the make file). I'm sure that I'll have problem with these in due course - but at the moment I can't even get it to see gtk!

The error that I get is:

gcc    -c -o *.o main.c
In file included from main.c:1:
In file included from ./main.h:2:
./src/gtk_functions.h:3:9: fatal error: 'gtk/gtk.h' file not found
#import <gtk/gtk.h>
        ^~~~~~~~~~~
1 error generated.
make: *** [*.o] Error 1

My makefile is as follows:

TARGET = midget
CC = gcc
GTKLIBS = `pkg-config --cflags --libs gtk+-3.0`
SRC_DIR = src

midget: *.o
    $(CC) -o $(TARGET) $(GTKLIBS) *.o

$(SRC_DIR)/gtk_%.o: %.c
    $(CC) -c $(SRC_DIR)/gtk_%.c $(GTKLIBS) -o $(SRC_DIR)/%.o

clean :
    rm -f $(TARGET) *.o

The structure of my folders is:

.
main.c
main.h
makefile
[src]
   |_gtk_functions.c
   |_gtk_functions.h
   |_gtk_window_setup.c
   |_gtk_window_setup.h
   |_ ...

I've looked at the following question - but it doesn't seem to help make file for Gtk


Solution

  • You never specified, that the compiler should use GTKLIBS when compiling the *.o files.

    The canonical way would be to use the CFLAGS variable:

    CFLAGS = `pkg-config --cflags gtk+-3.0`