从 makefile 中获取未定义的引用错误

埃米利奥·托雷斯·莫利纳

我正在开发一个项目,该项目使用其他人在名为 CommandParser 的文件夹中编写的一些代码。我的项目叫做 TCP_IP,里面有 CommandParser 文件夹。在此处输入图片说明

这是我的生成文件。

TARGET = $(BIN_DIR)/sim_tcp_ip
LIBS = -lpthread -L ./CommandParser -lcli
OBJS = $(OBJ_DIR)/prueba.o \
       $(OBJ_DIR)/ListaEnlazada.o \
       $(OBJ_DIR)/Grafico.o \
       $(OBJ_DIR)/Net.o \
       $(OBJ_DIR)/Topologias.o
BIN_DIR = ./bin
OBJ_DIR = ./obj
INC_DIR = ./inc
SRC_DIR = ./src
CFLAGS = -Wall -I$(INC_DIR)

$(TARGET): $(OBJS) CommandParser/libcli.a
    mkdir -p $(BIN_DIR)
    gcc $(CFLAGS) $(OBJS) -o $(TARGET) $(LIBS)
    
$(OBJ_DIR)/%.o : %.c
    mkdir -p $(OBJ_DIR)
    gcc -c -MD $(CFLAGS) $< -o $@
    
CommandParser/libcli.a:
    (cd CommandParser; make)

-include $(OBJ_DIR)/*.d
    
.PHONY: clean
clean:
    rm -rf $(OBJ_DIR) $(BIN_DIR)
    (cd CommandParser; make clean)
all:
    make
    (cd CommandParser; make)

CommandParser 有自己的 makefile。

CC=gcc
CFLAGS=-g -Wall
INCLUDES=-I .
CLILIB=libcli.a
TARGET:exe ${CLILIB}
OBJ=cmd_hier.o parser.o serialize.o string_util.o clistd.o clicbext.o gluethread/glthread.o ut/utinfra/ut_parser.o
exe:testapp.o ${CLILIB}
    @echo "Building final executable"
    @ ${CC} ${CFLAGS} ${INCLUDES} testapp.o -o exe -L . -lcli -lpthread -lrt
cmd_hier.o:cmd_hier.c
    @echo "Building cmd_hier.o"
    @ ${CC} ${CFLAGS} -c ${INCLUDES} cmd_hier.c -o cmd_hier.o
parser.o:parser.c
    @echo "Building parser.o"
    @ ${CC} ${CFLAGS} -c ${INCLUDES} parser.c -o parser.o
gluethread/glthread.o:gluethread/glthread.c
    @echo "Building gluethread/glthread.o"
    @ ${CC} ${CFLAGS} -c ${INCLUDES} gluethread/glthread.c -o gluethread/glthread.o
serialize.o:serialize.c
    @echo "Building serialize.o"
    @ ${CC} ${CFLAGS} -c ${INCLUDES} serialize.c -o serialize.o
string_util.o:string_util.c
    @echo "Building string_util.o"
    @ ${CC} ${CFLAGS} -c ${INCLUDES} string_util.c -o string_util.o
clistd.o:clistd.c
    @echo "Building clistd.o"
    @ ${CC} ${CFLAGS} -c ${INCLUDES} clistd.c -o clistd.o
clicbext.o:clicbext.c
    @echo "Building clicbext.o"
    @ ${CC} ${CFLAGS} -c ${INCLUDES} clicbext.c -o clicbext.o
testapp.o:testapp.c
    @echo "Building testapp.o"
    @ ${CC} ${CFLAGS} -c ${INCLUDES} testapp.c -o testapp.o
ut/utinfra/ut_parser.o:ut/utinfra/ut_parser.c
    @echo "Building ut/utinfra/ut_parser.o"
    @ ${CC} ${CFLAGS} -c ${INCLUDES} ut/utinfra/ut_parser.c -o ut/utinfra/ut_parser.o
${CLILIB}: ${OBJ}
    @echo "Building Library ${CLILIB}"
    ar rs ${CLILIB} ${OBJ}
clean:
    rm -f exe
    rm -f *.o
    rm -f gluethread/*.o
    rm -f ut/utinfra/*.o
    rm -f ${CLILIB}
    rm -f CMD_HIST_RECORD_FILE.txt
install:
    cp ${CLILIB} /usr/local/lib/
    cp libcli.h /usr/include/
    cp cmdtlv.h /usr/include/
uninstall:
    rm -f /usr/local/lib/${CLILIB}
    rm -f /usr/include/libcli.h
    rm -f /usr/include/cmdtlv.h

在 CommandParser 中执行make 时,我没有收到任何错误。但是,我的 makefile 似乎有问题,因为如果我在 TCP_IP 文件夹中进行make,我会收到一些未定义的引用错误。

/usr/bin/ld: ./CommandParser/libcli.a(ut_parser.o): 在函数ut_parser_init': /home/darinel/Documentos/Cursos/C++/TCP_IP/CommandParser/ut/utinfra/ut_parser.c:52: referencia a mq_open '未定义

/usr/bin/ld: ./CommandParser/libcli.a(ut_parser.o): en la función run_test_case': /home/darinel/Documentos/Cursos/C++/TCP_IP/CommandParser/ut/utinfra/ut_parser.c:261: referencia a mq_timedreceive' sin definir

/usr/bin/ld: ./CommandParser/libcli.a(ut_parser.o): en la función cli_out': /home/darinel/Documentos/Cursos/C++/TCP_IP/CommandParser/ut/utinfra/ut_parser.c:517: referencia a mq_send' sin definir

collect2: error: ld returned 1 exit status

make: [makefile:16: bin/sim_tcp_ip] Error 1

The message shows that these errors come from the CommandParser code, and not from my code, even though it previously worked fine in the CommandParser folder.

MadScientist

It appears that the CommandParser library is looking for another library, that contains functions like mq_open, mq_send, and mq_timedreceive. If you look up these functions you'll see that they're provided by the realtime library, librt. So, you need to add the -lrt option to your link line.

另外,请密切注意链接线上库的顺序。您应该始终将它们从最高级别排列到最低级别。你有:

LIBS = -lpthread -L ./CommandParser -lcli

但是-lpthread是一个较低级别的库,所以它应该在-lcli. 此外,使用线程构建可执行文件的现代方法是-pthread在编译行和链接行上使用该选项,因此您应该使用:

LIBS = -pthread -L ./CommandParser -lcli -lrt

CFLAGS = -pthread -Wall -I$(INC_DIR)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章