Include glib library in Makefile - c

I'm not an expert of Makefile.
In my program I'm using hashtables of glib.h so in my Makefile I wrote this:
exec: bin/test
bin/test
clean:
rm -f build/* bin/*
CFLAGS = -g -Wall -Wpedantic -Wno-padded -O $(shell pkg-config --cflags --libs glib-2.0)
INCLUDES = include/*.h
COMMON_DEPS = $(INCLUDES) Makefile
build/%.o: src/%.c $(COMMON_DEPS)
$(CC) $(CFLAGS) -c $< -o $#
bin/test: /* functions.o*/ $(COMMON_DEPS)
$(CC) -o bin/test /* functions.o*/
But when I execute the Makefile I receive a list of these error messages:
...
functions.c: undefined reference to "g_str_hash"
functions.c: undefined reference to "g_str_equal"
...
I don't understand why

At the end I solved it in this way:
exec: bin/test
bin/test
clean:
rm -f build/* bin/*
CFLAGS = -g -Wall -Wpedantic -Wno-padded -O $(shell pkg-config --cflags glib-2.0)
LFLAGS = $(shell pkg-config --libs glib-2.0)
INCLUDES = include/*.h
COMMON_DEPS = $(INCLUDES) Makefile
build/%.o: src/%.c $(COMMON_DEPS)
$(CC) $(CFLAGS) -c $< -o $#
bin/test: /*functions.o */ $(COMMON_DEPS)
$(CC) -o bin/test /*functions.o */ $(LFLAGS)

The crux of the problem is that you're including the link flags in the compile command rather than the link command itself.
Remove --libs glib-2.0 from CFLAGS and add it to a new variable LFLAGS that can be used on the link line...
exec: bin/test
bin/test
clean:
rm -f build/* bin/*
CFLAGS = -g -Wall -Wpedantic -Wno-padded -O $(shell pkg-config --cflags glib-2.0)
LFLAGS = $(shell pkg-config --libs glib-2.0)
INCLUDES = include/*.h
COMMON_DEPS = $(INCLUDES) Makefile
build/%.o: src/%.c $(COMMON_DEPS)
$(CC) $(CFLAGS) -c $< -o $#
bin/test: /* functions.o*/ $(COMMON_DEPS)
$(CC) $(LFLAGS) -o bin/test /* functions.o*/
[Note: I've left the rest of the makefile untouched but the dependency specification for bin/test does look very odd.]

Related

Build c program for release and build

The original makefile comes for Release build.
Original
APP:= deepstream-app
TARGET_DEVICE = $(shell gcc -dumpmachine | cut -f1 -d -)
NVDS_VERSION:=5.0
LIB_INSTALL_DIR?=/opt/nvidia/deepstream/deepstream-$(NVDS_VERSION)/lib/
APP_INSTALL_DIR?=/opt/nvidia/deepstream/deepstream-$(NVDS_VERSION)/bin/
ifeq ($(TARGET_DEVICE),aarch64)
CFLAGS:= -DPLATFORM_TEGRA
endif
SRCS:= $(wildcard *.c)
SRCS+= $(wildcard ../../apps-common/src/*.c)
INCS:= $(wildcard *.h)
PKGS:= gstreamer-1.0 gstreamer-video-1.0 x11
OBJS:= $(SRCS:.c=.o)
CFLAGS+= -I../../apps-common/includes -I../../../includes -DDS_VERSION_MINOR=0 -DDS_VERSION_MAJOR=5
LIBS+= -L$(LIB_INSTALL_DIR) -lnvdsgst_meta -lnvds_meta -lnvdsgst_helper -lnvdsgst_smartrecord -lnvds_utils -lm \
-lgstrtspserver-1.0 -ldl -Wl,-rpath,$(LIB_INSTALL_DIR)
CFLAGS+= `pkg-config --cflags $(PKGS)`
LIBS+= `pkg-config --libs $(PKGS)`
all: $(APP)
%.o: %.c $(INCS) Makefile
$(CC) -c -o $# $(CFLAGS) $<
$(APP): $(OBJS) Makefile
$(CC) -o $(APP) $(OBJS) $(LIBS)
install: $(APP)
cp -rv $(APP) $(APP_INSTALL_DIR)
clean:
rm -rf $(OBJS) $(APP)
Modified for both Release and Debug
APP:= deepstream-app
DEBUGAPP:= deepstream-app-debug
TARGET_DEVICE = $(shell gcc -dumpmachine | cut -f1 -d -)
NVDS_VERSION:=5.0
LIB_INSTALL_DIR?=/opt/nvidia/deepstream/deepstream-$(NVDS_VERSION)/lib/
APP_INSTALL_DIR?=/opt/nvidia/deepstream/deepstream-$(NVDS_VERSION)/bin/
ifeq ($(TARGET_DEVICE),aarch64)
CFLAGS:= -DPLATFORM_TEGRA
endif
SRCS:= $(wildcard *.c)
SRCS+= $(wildcard ../../apps-common/src/*.c)
INCS:= $(wildcard *.h)
PKGS:= gstreamer-1.0 gstreamer-video-1.0 x11
OBJS:= $(SRCS:.c=.o)
CFLAGS+= -I../../apps-common/includes -I../../../includes -DDS_VERSION_MINOR=0 -DDS_VERSION_MAJOR=5
LIBS+= -L$(LIB_INSTALL_DIR) -lnvdsgst_meta -lnvds_meta -lnvdsgst_helper -lnvdsgst_smartrecord -lnvds_utils -lm \
-lgstrtspserver-1.0 -ldl -Wl,-rpath,$(LIB_INSTALL_DIR)
CFLAGS+= `pkg-config --cflags $(PKGS)`
LIBS+= `pkg-config --libs $(PKGS)`
all: $(APP)
%.o: %.c $(INCS) Makefile
$(CC) -c -o $# $(CFLAGS) $<
%.o: %.c $(INCS) Makefile
$(CC) -g3 -c -o $# $(CFLAGS) $<
$(APP): $(OBJS) Makefile
$(CC) -o $(APP) $(OBJS) $(LIBS)
$(DEBUGAPP): $(OBJS) Makefile
$(CC) -g3 -o $(DEBUGAPP) $(OBJS) $(LIBS)
install: $(APP)
cp -rv $(APP) $(DEBUGAPP) $(APP_INSTALL_DIR)
clean:
rm -rf $(OBJS) $(APP) $(DEBUGAPP)
But it doesn't produce deepstream-app-debug. What should I change for both release and debug?
You can add the DEBUGAPP to the target all:
# This belongs in the makefile:
all: $(APP) $(DEBUGAPP)
This will build both APP and DEBUGAPP, if you call make like you used to do it most probably.
However, you can give make any target as an argument without further changing the makefile:
# This is a command in your shell:
make deepstream-app-debug

make in raspberrypi gives : missing separator error [duplicate]

This question already has answers here:
makefile:4: *** missing separator. Stop
(17 answers)
Closed 3 years ago.
I keep getting this error while I try to run the code on my raspberry pi, I dont know what has caused this:
CC = gcc
CFLAGS = -Wall -O0 -std=gnu99 -I/usr/local/include -g
LDFLAGS = -L/usr/local/lib -pthread -lm -lwiringPi
FUSEFLAGS = `pkg-config fuse --cflags --libs`
SOURCES = $(wildcard *.c)
PROGS = $(patsubst %.c,%,$(SOURCES))
BINS = $(SOURCES:.c=)
all: $(PROGS)
%: %.c
$(CC) $< $(CFLAGS) $(LDFLAGS) -o $# $(FUSEFLAGS)
clean:
$(RM) *.o *.a $(BINS)
this gives the following error:
Makefile:14: *** missing separator. Stop.
when I use make to run my code. my make was working before I am not sure what has caused this happend all the sudden!
CC = gcc
CFLAGS = -Wall -O0 -std=gnu99 -I/usr/local/include -g
LDFLAGS = -L/usr/local/lib -pthread -lm -lwiringPi
FUSEFLAGS = `pkg-config fuse --cflags --libs`
SOURCES = $(wildcard *.c)
PROGS = $(patsubst %.c,%,$(SOURCES))
BINS = $(SOURCES:.c=)
all: $(PROGS)
%: %.c
<___T___A___B___>$(CC) $< $(CFLAGS) $(LDFLAGS) -o $# $(FUSEFLAGS)
clean:
<___T___A___B___>$(RM) *.o *.a $(BINS)
Where I have written <___T___A___B___>, there should be 1 tab and not spaces !

makefile not updating .o file with respectively with .h file

I have following make file :-
VER = Debug
CC = gcc
objectfiles = Getstr.o ui.o ustreqsol.o main.o
pkg = `pkg-config --cflags --libs gtk+-3.0`
obj = $(addprefix objs/,$(objectfiles))
../$(VER)/Calculator: $(obj)
$(CC) -o $# $(obj) $(pkg)
./objs/ui.o:ui.c
$(CC) -c -o $# $< $(pkg)
./objs/main.o:main.c
$(CC) -c -o $# $< $(pkg)
./objs/%.o: %.c %.h
$(CC) -c -o $# $<
clean:
-rm ../$(VER)/Calculator
-rm /objs/*
and follwing files in my src dir:-
$ ls
Getstr.c main.c Makefile objs ui.c ui.h ustreqsol.c ustreqsol.h
objs is directory. Whenver I change ustreqsol.h file it compiles ustreqsol.c file but not in case for ui.h file
$ touch ustreqsol.h
$ make
gcc -c -o objs/ustreqsol.o ustreqsol.c
gcc -o ../Debug/Calculator objs/Getstr.o objs/ui.o objs/ustreqsol.o objs/main.o `pkg-config --cflags --libs gtk+-3.0`
$ make
make: '../Debug/Calculator' is up to date.
$ touch ui.h
$ make
make: '../Debug/Calculator' is up to date.
As a noob in makefiles i have no idea why is this happening
The reason ui.c is not being rebuilt is because you explictly said ui.h is not a dependency:
./objs/ui.o:ui.c
$(CC) -c -o $# $< $(pkg)
For the general dependency you have set up:
./objs/%.o: %.c %.h
$(CC) -c -o $# $<
The dependency list only takes effect for files that you didn't explicitly set them for, such as ustreqsol.c.
You need to add targets for each object file specifying the dependencies for each one. The targets can be blank, as the %.o target will fill in what to do.
For example:
./objs/ustreqsol.o: ustreqsol.c ustreqsol.h ui.h
./objs/ui.o: ui.c ui.h
./objs/main.o: main.c ui.h ustreqsol.h
./objs/Getstr.o: Getstr.c
./objs/%.o: %.c %.h
$(CC) -c -o $# $<

How to write generic commands in makefile? [duplicate]

This question already has answers here:
Wildcard targets in a Makefile
(3 answers)
Closed 9 years ago.
This is what my current version looks like:
CC = gcc
CFLAGS = `sdl-config --cflags`
LIBS = `sdl-config --libs` -lSDL_ttf
program: uprising
uprising: main.o init.o display.o move.o global.o control.o battle.o
$(CC) main.o init.o display.o move.o global.o control.o battle.o -o uprising $(CFLAGS) $(LIBS)
global.o: global.c
$(CC) -c global.c -o global.o $(CFLAGS)
battle.o: battle.c
$(CC) -c battle.c -o battle.o $(CFLAGS)
main.o: main.c
$(CC) -c main.c -o main.o $(CFLAGS)
init.o: init.c
$(CC) -c init.c -o init.o $(CFLAGS)
display.o: display.c
$(CC) -c display.c -o display.o $(CFLAGS)
move.o: move.c
$(CC) -c move.c -o move.o $(CFLAGS)
control.o: control.c
$(CC) -c control.c -o control.o $(CFLAGS)
clean:
rm -f *~ *# uprising init.o main.o display.o move.o global.o control.o
You see, every module gets compiled in the same manner. I've been tired of editing this makefile when adding a new module to my project. Is there any way to type a module's name for just once (as if it was a argument), and let makefile build each module in the same way?
If you change LIBS to LDLIBS, you can write the entire makefile in just this:
CC = gcc
CFLAGS = `sdl-config --cflags`
LDLIBS = `sdl-config --libs` -lSDL_ttf
program: uprising
uprising: main.o init.o display.o move.o global.o control.o battle.o
$(CC) $^ -o $# $(CFLAGS) $(LDLIBS)
clean:
rm -f *~ *# uprising *.o

Makefile allegro-nasm

Hi i have to write a code where i'll be combining NASM (assembly) with C and allegro library
CC = gcc
OBJ = main.o func.o
BIN = program
CFLAGS = -m32
$(BIN): $(OBJ)
$(CC) $(OBJ) $(CFLAGS) -o $(BIN)
main.o: main.c
$(CC) $(CFLAGS) -c main.c -o main.o
func.o: func.s
nasm -f elf func.s
how do I add allegro-confing --libs here? and where?
Given this makefile, you should add it to the link line:
$(BIN): $(OBJ)
$(CC) $(CFLAGS) -o $(BIN) $(OBJ) `allegro-config --libs`

Resources