make file for Gtk

℡╲_俬逩灬. 提交于 2021-01-01 06:30:58

问题


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?


回答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.




回答2:


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.



来源:https://stackoverflow.com/questions/34762056/make-file-for-gtk

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!