我正在制作一个C项目,必须在其中使用Makefile才能创建可执行的C文件。我的Makefile代码如下:
CC = gcc
CFLAGS = -lpthread -Wall -Werror
cracker: sha256.o reverse.o thread_functions.o cracker.o
$(CC) sha256.o reverse.o thread_functions.o cracker.o -o cracker $(CFLAGS)
sha256.o: sha256.c sha256.h
$(CC) -c sha256.c sha256.h $(CFLAGS)
reverse.o: reverse.c reverse.h
$(CC) -c reverse.c reverse.h $(CFLAGS)
thread_functions.o: thread_functions.c thread_functions.h
$(CC) -c thread_functions.c thread_functions.h $(CFLAGS)
cracker.o: cracker.c
$(CC) -c cracker.c $(CFLAGS)
clean:
rm -f *.o cracker
当我执行Makefile时,对于许多变量,它多次给我带来臭名昭著的“ VARIABLE的多个定义”错误:
gcc sha256.o reverse.o thread_functions.o cracker.o -o cracker -lpthread -Wall -Werror
cracker.o:(.bss+0x0): multiple definition of `num_of_threads'
thread_functions.o:(.bss+0x0): first defined here
cracker.o:(.bss+0x4): multiple definition of `selection_criterium'
thread_functions.o:(.bss+0x4): first defined here
cracker.o:(.bss+0x8): multiple definition of `thread_flag'
thread_functions.o:(.bss+0x8): first defined here
cracker.o:(.bss+0xc): multiple definition of `file_index'
thread_functions.o:(.bss+0xc): first defined here
cracker.o:(.bss+0x10): multiple definition of `display_criterium'
thread_functions.o:(.bss+0x10): first defined here
cracker.o:(.bss+0x18): multiple definition of `file_results'
thread_functions.o:(.bss+0x18): first defined here
collect2: error: ld returned 1 exit status
Makefile:5: recipe for target 'cracker' failed
make[1]: *** [cracker] Error 1
我试图仅使用“ cracker.o”来创建可执行文件,但是它显示了许多“未定义的对VARIABLE的引用”错误。我试图删除“ cracker.c”中的“ include”行,因为我看到了一个Stack Overflow解决方案,该解决方案说要删除include行,但是我按预期给了我错误。怎么了回想一下,C文件之间存在相互依赖关系(reverse.c取决于sha256.c,thread_functions.c取决于reverse.c,cracker.c取决于thread_functions.c和reverse.c)。