我快疯了,请有人帮忙...
我想要: src 中的 .c 文件 .h 文件中的 include .o 文件 构建中的 .d 文件 bin 中的构建二进制可执行文件
我从 OSDev wiki 中获得了灵感,但我根本无法让它与目录结构一起工作。
我希望能够在 bin 中拥有单独的单元测试文件,例如 %_t 。
我收到以下错误(目前):
/usr/bin/ld: /tmp/cc0HW3O2.o: in function `outstr':
outstr.c:(.text+0x174): undefined reference to `kstrlen'
collect2: error: ld returned 1 exit status
make: *** [Makefile:38: bin/outstr_t] Error 1
src: 'kstrlen.c' 'outstr.c' 包括: 'kstrlen.h' 'outstr.h' 'test.h'
test.h 包含使用 Test Anything Protocol 的宏
src 文件包含:
#include "kstrlen.h"
#ifdef TEST
#include "test.h"
int test_string (char *str, int len);
int main () {
PLAN(8);
OK("One word", test_string("abcde", 5));
OK("Zero-length", test_string("", 0));
OK("Single char", test_string("a", 1));
OK("Spaces", test_string(" ", 3));
OK("Multiline", test_string("First Line\nSecond Line\nThird Line", 33));
OK("Non-printable characters", test_string("\x01\x02\x03\x04\x05", 5));
OK("End with null", test_string("End with null\0", 13));
OK("Embedded null", test_string("This\0 is embedded", 4));
GRADE();
}
int test_string (char *str, int len) {
return kstrlen(str) == len ? 1 : 0;
}
#endif
int kstrlen (char *str) {
if (*str == 0) {
return 0;
}
int len = 0;
while (*str) {
len++;
str++;
}
return len;
}
有了这个 Makefile:
SRCDIR := src
BINDIR := bin
INCDIR := include
BUILDDIR := build
SRCFILES := $(shell find $(SRCDIR) -type f -name "*.c")
HDRFILES := $(shell find $(INCDIR) -type f -name "*.h")
OBJFILES := $(patsubst $(SRCDIR)/%.c,$(BUILDDIR)/%.o,$(SRCFILES))
DEPFILES := $(patsubst $(SRCDIR)/%.c,$(BUILDDIR)/%.d,$(SRCFILES))
TSTFILES := $(patsubst $(SRCDIR)/%.c,$(BINDIR)/%_t,$(SRCFILES))
TSTDEPFILES := $(patsubst $(BINDIR)/%_t, $(BUILDDIR)/%.d, $(TSTFILES))
MKDIR := mkdir -p
RM := rm -rf
CC := gcc
WARNINGS := -Wextra -Werror -Wall
CFLAGS := $(WARNINGS) -I $(INCDIR)
check: testdrivers
-@rc=0; count=0; \
for file in $(notdir $(TSTFILES)); do \
echo " TST $(BINDIR)/$$file"; $(BINDIR)/$$file; \
rc=`expr $$rc + $$?`; count=`expr $$count + 1`; \
done; \
echo; echo "Tests executed: $$count Tests failed: $$rc"
testdrivers: $(TSTFILES)
-include $(DEPFILES) $(TSTDEPFILES)
$(BUILDDIR)/%.o: $(SRCDIR)/%.c Makefile | $(BUILDDIR)
@$(CC) $(CFLAGS) -MMD -MP -c $< -o $@
$(BINDIR)/%_t: $(SRCDIR)/%.c Makefile | $(BINDIR)
@$(CC) $(CFLAGS) -MMD -MP -MF $(BUILDDIR)/$(@F).d -DTEST $< -o $@
$(BUILDDIR):
$(MKDIR) $@
$(BINDIR):
$(MKDIR) $@
clean:
$(RM) $(BUILDDIR) $(BINDIR)