1

I'm really new to makefiles and also to the libraries or frameworks I'm using (Gtk, Gdk and OpenCL) to create a program which should display the mandelbrot set as a dynamic image.

I've created this make file based on a makefile provided by my professor. The problem I was having with his makefile is due to the fact that my professor was using another operating system, etc.

So, I've changed his makefile to this my new makefile:

GTK_PACKAGES=gdk-pixbuf-2.0 gtk+-2.0
GTK_CFLAGS=$(shell pkg-config --cflags $(GTK_PACKAGES))
GTK_LIBS=$(shell pkg-config --libs $(GTK_PACKAGES))

CFLAGS=-g -Wall -O2 -std=c99 $(GTK_CFLAGS)
LIBS=$(GTK_LIBS)

PROGS=opencl_mandelbrot

.PHONY: all
all: $(PROGS)

%: %.c
    $(CC) $(CFLAGS) $(LDFLAGS) $*.c -framework OpenCL -o $@

%.o: %.c
    $(CC) $(CFLAGS) -c $*.c -o $@

opencl_mandelbrot: opencl_mandelbrot.o opencl_util.o
    $(CC) $(CFLAGS) $(LDFLAGS) opencl_mandelbrot.o opencl_util.o -framework OpenCL -o $@

.PHONY: clean
clean:
    rm -f *.o $(PROGS)

But when I try to type make on the terminal, I've the following error:

cc -g -Wall -O2 -std=c99 -D_REENTRANT -I/opt/X11/include/cairo -I/opt/X11/include/pixman-1 -I/opt/X11/include -I/opt/X11/include/freetype2 -I/opt/X11/include/libpng15 -I/opt/X11/include -I/usr/local/Cellar/gdk-pixbuf/2.32.3/include/gdk-pixbuf-2.0 -I/usr/local/Cellar/gtk+/2.24.30/include/gtk-2.0 -I/usr/local/Cellar/gtk+/2.24.30/lib/gtk-2.0/include -I/usr/local/Cellar/pango/1.38.1/include/pango-1.0 -I/usr/local/Cellar/harfbuzz/1.2.6/include/harfbuzz -I/usr/local/Cellar/pango/1.38.1/include/pango-1.0 -I/usr/local/Cellar/atk/2.18.0_1/include/atk-1.0 -I/usr/local/Cellar/libpng/1.6.21/include/libpng16 -I/usr/local/Cellar/glib/2.46.2/include/glib-2.0 -I/usr/local/Cellar/glib/2.46.2/lib/glib-2.0/include -I/usr/local/opt/gettext/include -c opencl_mandelbrot.c -o opencl_mandelbrot.o
cc -g -Wall -O2 -std=c99 -D_REENTRANT -I/opt/X11/include/cairo -I/opt/X11/include/pixman-1 -I/opt/X11/include -I/opt/X11/include/freetype2 -I/opt/X11/include/libpng15 -I/opt/X11/include -I/usr/local/Cellar/gdk-pixbuf/2.32.3/include/gdk-pixbuf-2.0 -I/usr/local/Cellar/gtk+/2.24.30/include/gtk-2.0 -I/usr/local/Cellar/gtk+/2.24.30/lib/gtk-2.0/include -I/usr/local/Cellar/pango/1.38.1/include/pango-1.0 -I/usr/local/Cellar/harfbuzz/1.2.6/include/harfbuzz -I/usr/local/Cellar/pango/1.38.1/include/pango-1.0 -I/usr/local/Cellar/atk/2.18.0_1/include/atk-1.0 -I/usr/local/Cellar/libpng/1.6.21/include/libpng16 -I/usr/local/Cellar/glib/2.46.2/include/glib-2.0 -I/usr/local/Cellar/glib/2.46.2/lib/glib-2.0/include -I/usr/local/opt/gettext/include  opencl_mandelbrot.o opencl_util.o -framework OpenCL -o opencl_mandelbrot
Undefined symbols for architecture x86_64:
  "_g_object_unref", referenced from:
      _reallocate_pixbufs in opencl_mandelbrot.o
  "_g_type_check_instance_cast", referenced from:
      _main in opencl_mandelbrot.o
  "_gdk_draw_pixbuf", referenced from:
      _draw_mandelbrot_full_with_gpu in opencl_mandelbrot.o
  "_gdk_pixbuf_get_height", referenced from:
      _reallocate_pixbufs in opencl_mandelbrot.o
      _recenter in opencl_mandelbrot.o
      _draw_mandelbrot_full_with_gpu in opencl_mandelbrot.o
  "_gdk_pixbuf_get_n_channels", referenced from:
      _draw_mandelbrot_full_with_gpu in opencl_mandelbrot.o
  "_gdk_pixbuf_get_pixels", referenced from:
      _draw_mandelbrot_full_with_gpu in opencl_mandelbrot.o
  "_gdk_pixbuf_get_rowstride", referenced from:
      _reallocate_pixbufs in opencl_mandelbrot.o
      _draw_mandelbrot_full_with_gpu in opencl_mandelbrot.o
  "_gdk_pixbuf_get_width", referenced from:
      _reallocate_pixbufs in opencl_mandelbrot.o
      _recenter in opencl_mandelbrot.o
      _draw_mandelbrot_full_with_gpu in opencl_mandelbrot.o
  "_gdk_pixbuf_new", referenced from:
      _reallocate_pixbufs in opencl_mandelbrot.o
  "_gtk_init", referenced from:
      _main in opencl_mandelbrot.o
  "_gtk_main", referenced from:
      _main in opencl_mandelbrot.o
  "_gtk_main_quit", referenced from:
      _destroy_window in opencl_mandelbrot.o
      _keyboard_input in opencl_mandelbrot.o
  "_gtk_object_get_type", referenced from:
      _main in opencl_mandelbrot.o
  "_gtk_signal_connect_full", referenced from:
      _main in opencl_mandelbrot.o
  "_gtk_widget_set_events", referenced from:
      _main in opencl_mandelbrot.o
  "_gtk_widget_show_all", referenced from:
      _main in opencl_mandelbrot.o
  "_gtk_window_get_type", referenced from:
      _main in opencl_mandelbrot.o
  "_gtk_window_new", referenced from:
      _main in opencl_mandelbrot.o
  "_gtk_window_resize", referenced from:
      _main in opencl_mandelbrot.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [opencl_mandelbrot] Error 1

even though I've installed gtk using brew, but I guess the problem is related to the order of linkage or something similar...

I appreciate your help!

nbro
  • 15,395
  • 32
  • 113
  • 196

3 Answers3

1
opencl_mandelbrot: opencl_mandelbrot.o opencl_util.o
    $(CC) $(CFLAGS) $(LDFLAGS) $(LIBS) opencl_mandelbrot.o opencl_util.o -framework OpenCL -o $@ 

You missed LIBS

Stian Skjelstad
  • 2,277
  • 1
  • 9
  • 19
1

You assign GTK+ linker flags to LIBS variable, but in your rules you use LDFLAGS variable, so that you get linker errors, because linker cannot resolve symbols, as it was not instructed to look for them in fight places.

Change:

LIBS=$(GTK_LIBS)

to

LDFLAGS=$(GTK_LIBS)

or add $(LIBS) to your linker rules.

1

By attempting to reinvent the built-in rules you've ended up adding your own bugs, you don't need to tell make how to compile or link, it already knows, all you need to do is to set the right variables.

GTK_PACKAGES := gdk-pixbuf-2.0 gtk+-2.0
GTK_CFLAGS   := $(shell pkg-config --cflags $(GTK_PACKAGES))
GTK_LIBS     := $(shell pkg-config --libs $(GTK_PACKAGES))

CFLAGS       := -g -Wall -O2 -std=c99 $(GTK_CFLAGS)
LDLIBS       := $(GTK_LIBS)

PROGS := opencl_mandelbrot

.PHONY: all clean

all: $(PROGS)

opencl_mandelbrot: LDFLAGS += -framework OpenCL
opencl_mandelbrot: opencl_mandelbrot.o opencl_util.o

clean: ; $(RM) *.o $(PROGS)

Watch out when deleting files using wildcards, if you're going to be redistributing your code it's a bad idea.

user657267
  • 20,568
  • 5
  • 58
  • 77