I’m new to C programming and Makefiles.
I have a function in my c code which uses strlcpy for copying string.
I’m not allowed to used #include <bsd/string.h> in my code so I have included #include <glib.h> .
But while compiling makefile,
getting error:
/usr/bin/ld: reg_maker.o: in function main': /home/reg_maker.c:66: undefined reference to
strlcpy’
/usr/bin/ld: /home/reg_maker.c:67: undefined reference to `strlcpy’
Below is the makefile I used :
OUT=reg_maker
OBJS=reg_maker.o
CC=gcc
IDIR = -I../../../include -I../../../include/xxxx -I/usr/include/json-c/ -I/usr/lib/x86_64-linux-gnu/glib-2.0/include/ -I/usr/include/glib-2.0 -I/usr/include
FLAGS= -c -g -Wall
LFLAGS= -lcrypto -ljson-c -lglib-2.0
all: $(OBJS)
$(CC) -g $(OBJS) -o $(OUT) $(LFLAGS)
reg_maker.o:reg_maker.c
$(CC) $(FLAGS) reg_maker.c $(IDIR) $(LFLAGS)
clean:
rm -f $(OBJS) $(OUT) reg_maker.bin`
I’m using ubuntu vm version- 20.04 , I have glib-2.0 library.
Could anyone please help me out what changes I have to make in make file to compile it with strlcpy
Thanks in advance
3
Answers
The GLib wrapper for
strlcpy
isg_strlcpy
, described as:To use
libbsd
version ofstrlcpy
directly, the program should linked with-lbsd
in overlay mode1 and include the<string.h>
header.1 The details of overlay mode, from
libbsd(7)
:Code using the BSD functions
strlcpy
andstrlcat
should be linked with thelibbsd
. If this library is not available on your system, you can add the source code for these functions in your own program:after installing libbsd and pkg-config(3), compile with this
pkg-config --cflags --libs libbsd-overlay
in order to use the libbsd-overlay. your library headers should remain the same as if you were on a system that uses BSD as default. overlay is cool in terms of portability.pkg-config-guide