C Makefile - 可执行文件和目标文件

C Makefile - exec and object files

我正在尝试使用 makefile.txt 和一些 gcc 参数编译 .c 文件。

我有以下 Makefile:

 GCC_OPTIONS=-std=c11 -pedantic-errors -Wstrict-prototypes -Wall -Wextra -Wno-format-security -Werror -Werror=vla
        
        OBJS=driver.o q.o
        
        EXEC=q.out
        
        $(EXEC):$(OBJS)
        
            gcc $(GCC_OPTIONS) $(OBJS) -o q.out -lm
        
        qdriver.o:qdriver.c q.h
        
            gcc $(GCC_OPTIONS) -c qdriver.c -o qdriver.o    
        
        q.o:q.c q.h
        
            gcc $(GCC_OPTIONS) -c q.c -o q.o
        
        clean:
        
            rm $(OBJS) $(EXEC)

当我 运行 全部完成时,出现以下我无法修复的错误:

./makefile.txt: line 1: -pedantic-errors: command not found
: Permission deniede 2: ./q.o
./makefile.txt: line 4: EXEC: command not found
./makefile.txt: line 4: OBJS: command not found
./makefile.txt: line 4: $':\r': command not found
./makefile.txt: line 5: GCC_OPTIONS: command not found
./makefile.txt: line 5: OBJS: command not found
/usr/bin/ld: cannot find -lm
collect2: error: ld returned 1 exit status
./makefile.txt: line 6: qdriver.o:qdriver.c: command not found
./makefile.txt: line 7: GCC_OPTIONS: command not found
: No such file or directory
./makefile.txt: line 8: q.o:q.c: command not found
./makefile.txt: line 9: GCC_OPTIONS: command not found
./makefile.txt: line 10: $'clean:\r': command not found
./makefile.txt: line 11: OBJS: command not found
./makefile.txt: line 11: EXEC: command not found
rm: cannot remove ''$'\r': No such file or directory

您似乎试图 运行 将 makefile 作为脚本 -- 如果您键入

GCC_OPTIONS=-std=c11 -pedantic-errors

在 bash 提示符下,它会给你错误

-pedantic-errors: command not found...

这意味着您正在尝试 运行 将其作为 bash 中的一行。我可以看到这可能发生的两种方式:

  1. 你 运行 这是一个 bash 脚本(即你做了类似 source makefile.txtchmod +x makefile.txt; ./makefile.txt 而不是 make -f makefile.txt

  2. 第一行其实前面有一个tab,而且事先指定了一个target,导致第一行(及后续行)都是运行 as recipe线。您将不得不 运行 和 make -i,以使输出与您发布的内容匹配,否则,它会在第一次失败后退出。

无论哪种方式,您都必须修复 makefile 中的缩进,正如@datenwolf 提到的那样 - makefiles 中的前导 tabs/spaces 很重要