2

I wrote an implementation of several functions spread across multiple c files and those functions are declared in single header file along with several structures and preprocessors. I have been trying to compile them as a library and link it to a program. During make, it raises "undefined reference" error against methods. I referred the links (see below) but they were discuss about the usage of single .c & .h. I followed the advice but the errors still persist.

How to compile a static library in Linux?

"Undefined reference " to a function defined in static library

Undefined reference while linking static C library

I tried to compile them as normal using make and it was successful. The makefile which successfully compiled is given below:

INCLUDES = -I/usr/include

ffilter:
    gcc -Wextra -Wall -lm home.c filter.c handlerprocess.c handlerpack.c handlersys.c handlerfile.c handlerpcap.c helper.c -o ffilter $(INCLUDES)

clean: 
    $(RM) *.o *~ ffilter

Makefile code for compiling and linking the same as library with program, which raise errors, is given below. Would anyone help me to solve this problem.

CC = gcc
CFLAGS = -Wextra -Wall
TARGET = ffilter

all: $(TARGET)

#final output, linking library with main program
$(TARGET): home.o libhandlers.a
    $(CC) -o $(TARGET) home.o -L. -lhandlers

#main c program
home.o: home.c home.h
    $(CC) $(CFLAGS) -c home.c home.h

#implementation of library
filter.o: filter.h filter.c handlerprocess.c handlerpack.c handlersys.c handlerfile.c handlerpcap.c helper.c 
    $(CC) $(CFLAGS) -c -O -lm filter.h filter.c handlerprocess.c handlerpack.c handlersys.c handlerfile.c handlerpcap.c helper.c 

#creating library
libhandlers.a: filter.o
    ar rcs libhandlers.a filter.o

#remove built files
clean:
    $(RM) *.o *.a *.gch *~ $(TARGET)
Anegan
  • 23
  • 2

0 Answers0