首页 > 解决方案 > Makefile 在 c 中找不到函数

问题描述

我写了以下makefile:

CC = gcc
OBJS = Car.o Guide.o GuideSystem.o
DEBUG_OBJS = Car_debug.o Guide_debug.o GuideSystem_debug.o
SOURCE = Car.c Guide.c GuideSystem.c
HEADER = Car.h Guide.h GuideSystem.h list.h set.h
CFLAGS = -std=c99 -Wall -pedantic-errors -Werror
LIBM = -L. -lib
EXEC = test.exe test1.exe test2.exe test2_debug.exe
TEST_O = test1.o test2.o test2_debug.o test.o

#make test1.exe
test1.exe : $(OBJS) test1.o
    $(CC) $(CFLAGS) -DNDEBUG $(OBJS) test1.o $(LIBM) -o $@

#make test2.exe
test2.exe : $(OBJS) test2.o
    $(CC) $(CFLAGS) -DNDEBUG $(OBJS) test2.o $(LIBM) -o $@

#make test2_debug.exe
test2_debug.exe : $(OBJS) test2_debug.o
    $(CC) $(CFLAGS) -g $(OBJS) test2_debug.o $(LIBM) -o $@

#make test.exe
test.exe : $(OBJS) test.o
    $(CC) $(CFLAGS) -DNDEBUG $(OBJS) test.o $(LIBM) -o $@

#Testing (no asserts)
test.o : test.c Guide.h GuideSystem.h
    $(CC) -c -DNDEBUG $*.c $(CFLAGS)
test1.o : test1.c Guide.h GuideSystem.h
    $(CC) -c -DNDEBUG $*.c $(CFLAGS)
test2.o : test2.c Guide.h GuideSystem.h
    $(CC) -c -DNDEBUG $*.c $(CFLAGS)
Guide.o : Guide.c Guide.h list.h Car.h
    $(CC) -c -DNDEBUG $*.c $(CFLAGS)
GuideSystem.o : GuideSystem.c GuideSystem.h set.h
    $(CC) -c -DNDEBUG $*.c $(CFLAGS)
Car.o : Car.h
    $(CC) -c -DNDEBUG $*.c $(CFLAGS)

#Debug testing
test2_debug.o : test2.c Guide.h GuideSystem.h
    $(CC) -c -g $(CFLAGS) test2.c -o $@
Guide_debug.o : Guide.c Guide.h list.h Car.h
    $(CC) -c -g $(CFLAGS) Guide.c -o $@
GuideSystem_debug.o : GuideSystem.c GuideSystem.h set.h
    $(CC) -c -g $(CFLAGS) GuideSystem.c -o $@
Car_debug.o : Car.h
    $(CC) -c -g $(CFLAGS) Car.c -o $@

#Clean builds
clean :
    rm -f $(OBJS) $(DEBUG_OBJS) $(EXEC) $(TEST_O)

当我跑步时,make test我得到:

gcc   test.o   -o test
test.o: In function `main':
test.c:(.text+0x29): undefined reference to `createGuide'
... more undefined functions

我的makefile有一些问题,但我似乎找不到问题。所有其他make选项都可以正常工作,只有make test失败。

据我了解,它应该运行:

gcc -c -DNDEBUG -std=c99 -Wall -Werror -pedantic-errors test.c
gcc -o test.exe -DNDEBUG Guide.o GuideSystem.o Car.o test.o -L. -lib

可能是什么问题呢?我该如何解决?我的makefile有问题吗?

标签: cmakefile

解决方案


您没有定义任何test目标,因此make错误地猜测您想制作一个test仅命名为 from test.o(implicit rule) 的程序。

您可能应该插入的Makefile是:

test: test.exe test2.exe
    ./test.exe
    ./test2.exe
.PHONY: test


推荐阅读