自動依存性生成機能を備えたMakefile

自動依存性生成機能を備えたMakefile

狂っていると思います、誰が助けてください...

私が望むもの:srcの.cファイルには.hファイルの.oファイルが含まれています。ビルドビルドバイナリ実行可能ファイルの.dファイルは

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 プロトコルを使用するマクロが含まれています。

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)

ベストアンサー1

おすすめ記事