2

I'm trying to compile and link a simple program downloaded from the web to learn how to make a GUI using the gtk+ library.

Here is my makefile:

CC = gcc
BIN = gtk_led
SRC = main.c gtkled.c
OBJ = main.o gtkled.o

CPPFLAGS =-Wall -W -ansi -pedantic -O2 `pkg-config --cflags gtk+-2.0`

LDFLAGS = `pkg-config --libs gtk+-2.0`

all: $(BIN)

$(BIN): $(SRC)
$(CC) $(CPPFLAGS) -c $(SRC)
$(CC) $(LDFLAGS) -o $(BIN) $(OBJ)

clean:
rm -f *.o *~ core $(BIN)

When I do make, the build fails with the following errors:

gtkled.o: In function `gtk_led_size_allocate':
gtkled.c:(.text+0x43a): undefined reference to `g_return_if_fail_warning'
gtkled.c:(.text+0x487): undefined reference to `gdk_window_move_resize'
gtkled.o: In function `gtk_led_size_request':
gtkled.c:(.text+0x4f5): undefined reference to `g_return_if_fail_warning'

So I don't understand why.... I'm new to Linux, so that's hard for me :) (On ubuntu, working with virtualBox)

Thanks.

Jürgen Thelen
  • 12,745
  • 7
  • 52
  • 71
TGuerin
  • 345
  • 3
  • 18
  • It would be better to show the command that `make` is running for you... – Basile Starynkevitch Dec 14 '13 at 16:36
  • don't use -ansi -pedantic in your compiler flags. they are completely useless at best, or misleading and wrong at worst. use -Wall -Wextra if you want a decent set of modern compiler warnings from GCC. alternatively, look in the gcc man page and select the warnings that make sense for your code base. – ebassi Dec 15 '13 at 11:36

1 Answers1

2

Dont use backquotes in Makefile-s (but use the shell function of GNU make). And GTK2 is obsolete, why not use GTK3?

Replace at least the two lines CPPFLAGS and LDFLAGS with

 PACKAGES= gtk+-3.0
 OPTIMFLAGS=-g # put -O2 when all is ok
 PKG_CFLAGS= $(shell pkg-config  --cflags $(PACKAGES))
 CPPFLAGS =-Wall -W -ansi -pedantic $(OPTIMFLAGS) $(PKG_CFLAGS)
 LDFLAGS = $(shell pkg-config --libs $(PACKAGES))

BTW, you probably mean CFLAGS not CPPFLAGS. Run make -p to understand what are the builtin rules known to make; see also this answer and that answer to some related questions...

Also, order of arguments to gcc matters a lot, so

 $(CC) $(LDFLAGS) -o $(BIN) $(OBJ)

is wrong, you probably want

 $(CC) $(OBJ) $(LDFLAGS) -o $(BIN) 
Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • Thanks for your answer, But that's not working... :/ I'm not sure that I need to have a complex makefile. I found it with the source code. But I have just main.c file and many libraries. There isn't a simple way to compile this with a thing like this: all: hello hello: hello.o main.o gcc -o hello hello.o main.o hello.o: hello.c gcc -o hello.o -c hello.c -W -Wall -ansi -pedantic main.o: main.c hello.h gcc -o main.o -c main.c -W -Wall -ansi -pedantic clean: rm -rf *.o mrproper: clean rm -rf hello Sorry, but I really don't understand makefile.... Thanks ! :) – TGuerin Dec 14 '13 at 16:41
  • What is not working? Show the exact commands run by `make`. And yes, when coding for GTK, you really want to learn how to write `Makefile`-s (or else, type the single command to build your application in the terminal, or make a shell script of it), because you'll quite soon will have several source files for your GTK app. – Basile Starynkevitch Dec 14 '13 at 16:43
  • Thanks ! That's working by using $(CC) $(OBJ) $(LDFLAGS) -o $(BIN) Thanks a lot ! :)) – TGuerin Dec 14 '13 at 16:46
  • 1
    Then please upvote or accept my (improved) answer! BTW, you can use backquotes in comments; they have some special meaning (to typeset code). – Basile Starynkevitch Dec 14 '13 at 16:47