制作文件以编译许多文件

时间:2015-12-27 19:04:51

标签: makefile

我已经编写了一个makefile和我需要的所有库,任何人都可以使用正确的表格和结构正确编辑它:

# Macros
CC = gcc
COMP_FLAG = -std=c99 -Wall -pedantic-errors -Werror -DNDEBUG
LIB_FLAG = -L. -lmtm
# Main target
tests: memcache_test my_set_test cache_test user_test

# Targets make <file>
user_test: user_test.o user.o
$(CC) user_test.o user.o $(LIB_FLAG) -o $@

memcache_test: memcache_test.o memcache.o user.o cache.o
$(CC) memcache_test.o memcache.o user.o cache.o $(LIB_FLAG) -o $@ 

cache_test: cache_test.o cache.o
$(CC) cache_test.o cache.o $(LIB_FLAG) -o $@

my_set_test: my_set_test.o my_set.o
$(CC) my_set_test.o my_set.o -o $@


# Targets make <file>_test.o
user_test.o: tests/user_test.c user.h tests/test_utilities.h map.h set.h 
$(CC) -c $(COMP_FLAG) $(LIB_FLAG) tests/$*.c 

memcache_test.o: tests/memcache_test.c tests/test_utilities.h memcache.h map.h cache.h set.h list.h
$(CC) -c $(COMP_FLAG) $(LIB_FLAG) tests/$*.c

cache_test.o: tests/cache_test.c cache.h tests/test_utilities.h set.h 
$(CC) -c $(COMP_FLAG) $(LIB_FLAG) tests/$*.c

my_set_test.o: my_set/my_set_test.c tests/test_utilities.h my_set/my_set.h
$(CC) -c $(COMP_FLAG) $(LIB_FLAG) my_set/$*.c

# Targets make <file>.o
memcache.o: memcache.c memcache.h map.h cache.h list.h set.h user.h
$(CC) -c $(COMP_FLAG) $(LIB_FLAG) $*.c

user.o: user.c user.h set.h map.h
$(CC) -c $(COMP_FLAG) $(LIB_FLAG) $*.c

cache.o: cache.c cache.h set.h
$(CC) -c $(COMP_FLAG) $(LIB_FLAG) $*.c

my_set.o: my_set/my_set.c my_set/my_set.h 
$(CC) -c $(COMP_FLAG) my_set/$*.c

# Target runs all test files
run: run_my_set_test run_cache_test run_memcache_test run_user_test

run_clean: clean run

run_my_set_test: my_set_test
./my_set_test

run_cache_test: cache_test
./cache_test

run_memcache_test: memcache_test
./memcache_test

run_user_test: user_test
./user_test

# Target remove all <*_test> and <*.o> files
clean: clean_o clean_test

clean_test:
rm -f *_test
clean_o:
rm -f *.o

当我尝试运行它时,它说:

  

*****缺少分隔符。停止。

我不知道如何以及在何处放置分隔符。

1 个答案:

答案 0 :(得分:0)

makefile中的每个规则都是带冒号的行(&#39;:&#39;),后跟命令的可选行。每个命令必须以制表符开头:

user_test: user_test.o user.o
    $(CC) user_test.o user.o $(LIB_FLAG) -o $@

这是充分的解释吗?