问题
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.
回答1:
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)
来源:https://stackoverflow.com/questions/20585474/gtk-compilation-undefined-reference-c