2

I have a make file, which creates obj files for all source files and then the executable using those obj files (basically compiling each individual file and then linking all of them together).

CC = gcc
SRC_DIR = src
INC_DIR = inc
OBJ_DIR = obj
CFLAGS = -c -Wall -I$(INC_DIR)
EXE = project

SRCS = $(SRC_DIR)/main.c $(SRC_DIR)/file1.c            # and so on...
OBJS = $(OBJ_DIR)/main.o $(OBJ_DIR)/file1.o            # and so on...

main : clean build

build:  $(OBJS)
    $(CC)   $(OBJS) -o $(EXE)

$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c
        $(CC) $(CFLAGS) -c $< -o $@

I tried to do the same for gtk+3.0 but haven't been successful as the examples on the web always have been with respect to the example file and not the project as a whole (consisting multiple source files). one such eg:

$ cc `pkg-config --cflags --libs gtk+-3.0` hello.c -o hello

Make file for gtk+ is:

CC = gcc
SRC_DIR = .
INC_DIR = .
OBJ_DIR = Obj
CFLAGS = -Wall -g -o
PACKAGE = `pkg-config --cflags --libs gtk+-3.0`
LIBS = `pkg-config --libs gtk+-3.0`
EXE = Gui

SRCS = $(SRC_DIR)/main.c 
OBJS = $(OBJ_DIR)/main.o 

main : clean build       

build: $(OBJS)
    $(CC) $(OBJS) -o $(EXE)

$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c
        $(CC) $(PACKAGE) $(CFLAGS) $< -o $@ 

But this doesn't work. It gives errors (undefined reference to 'gtk_init' and other gtk functions) What modifications should i do?

madD7
  • 823
  • 10
  • 23

2 Answers2

1

It should be

 LDLIBS = $(shell pkg-config --libs gtk+-3.0)

instead of LIB

Check with make -p your builtin rules.

Look also at this example. See $(LINK.o) variable, etc.

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
1

The CFLAGS must have -c or that must be included while compiling. Also, the pkg-config must be included during linking.

After the changes, the make file becomes:

build: $(OBJS)
    $(CC) $(CFLAGS) $(OBJS) -o $(EXE) $(LIBS)

$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c
    $(CC) $(CFLAGS) ***-c*** -I$(INC_DIR) $< -o $@ $(PACKAGE)

The changes run successfully.

madD7
  • 823
  • 10
  • 23
  • I found this later, but here is a very good explanation. http://blog.borovsak.si/2009/09/glade3-tutorial-6-signals.html – madD7 Jan 15 '16 at 05:35