使用 GNU make 跨不同目录进行编译

标签 c unit-testing makefile gnu-make

我有一个这样的项目结构:

mcts/
    src/
        node_queue.c
        node_queue.h
    tests/
        munit.c    # testing frame work
        munit.h
        list_test.c # includes node_queue.h and munit.h
        Makefile    # Makefile in question

所以我的目标是编译测试 mcts/test/list_test.c 。我已经阅读了几种不同的策略来做到这一点。阅读了一些内容后,我将从 Makefile 中获取的一些内容改编为:

CC= gcc
SOURCE= $(wildcard ../src/*.c ./*.c)
OBJECTS= $(patsubst %.c, %.o, $(SOURCE))
INCLUDE= -I. -I../src/
CFLAGS= -std=c11 -g $(INCLUDE) -Werror -Wall

list_test: list_test.o munit.o ../src/node_queue.o
    $(CC) $(CFLAGS) $(INCLUDE) -o $@ list_test.o munit.o    ../src/node_queue.o

.c.o:
    $(CC) $(CFLAGS) -c $< -o $@

这是我在大约 2 小时内最接近工作的时间,当在 mcts/tests 中调用 make 时,我收到错误:

list_test.o: In function `construct_test':
/home/----/mcts/tests/list_test.c:9: undefined reference to `construct'
collect2: error: ld returned 1 exit status
make: *** [Makefile:8: list_test] Error 1

构造在mcts/src/node_queue.h中定义。 $(INCLUDE) 不应该确保包含 header 吗? 我怎样才能让它发挥作用?

非常感谢!

最佳答案

对于您的实际错误,您正在报告 undefined symbol 的链接错误。如果在 node_queue.h 中定义了该名称的对象或函数,您将收到 construct多重定义错误。

您可能缺少的是该 header 中有一个声明,但 node_queue.c 中没有定义。

关于使用 GNU make 跨不同目录进行编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38746018/

相关文章:

python - 如何测试(使用 unittest)Django View 的 HTML 输出?

Makefiles : Get . cpp 从一个目录并将编译的 .o 放在另一个目录中

makefile 缺少分隔符

linux - 如何从源代码安装内核模块。制作过程中出错

c - 稀疏矩阵线性和非线性方程求解器

c - 二叉树Find_parent方法导致SegFault

ruby-on-rails - ControllerTest 错误,无法找到 'id' = 的事务

python - Pytest 段错误和测试失败

c - C中多线程的整数转换问题

c - C语言中*a = *b是什么意思?