使用make和gcc将静态库与C程序编译和链接时出错

问题描述

我编写了多个函数的实现,这些函数分布在多个c文件中,这些函数与几个结构和预处理器一起在单个头文件中声明。我一直试图将它们编译为库并将其链接到程序。在制作过程中,它针对方法引发“未定义的引用”错误。我引用了链接(请参阅下文),但是他们讨论了有关单个.c和.h的用法。我遵循了建议,但错误仍然存​​在。

How to compile a static library in Linux?

"Undefined reference " to a function defined in static library

Undefined reference while linking static C library

我尝试使用make正常编译它们,并且成功。下面给出成功编译的makefile:

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代码,用于编译和链接与库相同的程序,这会引起错误,如下所示。谁能帮我解决这个问题。

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)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)