我有一个这样的项目结构:
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/