我的手写 C++ Makefile 给出了未找到的命令

Bru*_*uce 1 compiling make

我制作了一个 makefile 来帮助编译多个 C++ 文件,但它给了我“找不到命令”的错误。我需要修复它。

我得到的错误:

Make: line 1: main.out::command not found 
g++: error: GradeBook.o: No such file or directory 
g++: error: main.o: No such file or directory 
g++: fatal error: no input files 
compilation terminated. 
Make: line 4: main.o:: command not found 
Make: line 7: GradeBook.o:: command not found 
Make: line 10: clear:: command not found 
Run Code Online (Sandbox Code Playgroud)

这是我的生成文件:

main.out: GradeBook.o main.o
    g++ -Wall -g -o main.out GradeBook.o main.o 

main.o: main.cpp GradeBook.h
    g++ -Wall -g -c main.cpp

GradeBook.o: GradeBook.cpp GradeBook.h
    g++ -Wall -g -c GradeBook.cpp

clean:
    rm -f main.out main.o GradeBook.o 
Run Code Online (Sandbox Code Playgroud)

slm*_*slm 7

以下是人们使用 makefile 所犯的典型错误的列表。

问题 #1 - 使用空格而不是制表符

make众所周知,该命令对Makefile. 您需要确保与给定目标关联的操作以制表符而不是空格为前缀。

这是一个单一的Tab后跟您要为给定目标运行的命令。

例子

这是你的目标。

main.out: GradeBook.o main.o
Run Code Online (Sandbox Code Playgroud)

Tab后面的命令前面应该有一个。

    g++ -Wall -g -o main.out GradeBook.o main.o 
^^^^--Tab
Run Code Online (Sandbox Code Playgroud)

这是您清理的 Makefile

//Here is my makefile:

main.out: GradeBook.o main.o
        g++ -Wall -g -o main.out GradeBook.o main.o 

main.o: main.cpp GradeBook.h
        g++ -Wall -g -c main.cpp

GradeBook.o: GradeBook.cpp GradeBook.h
        g++ -Wall -g -c GradeBook.cpp

clean:
        rm -f main.out main.o GradeBook.o 
Run Code Online (Sandbox Code Playgroud)

问题 #2 - 命名错误

该工具make期望调用该文件Makefile。其他任何事情,您都需要告诉make您希望它使用什么文件。

$ make -f mafile

-or- 

$ make --file=makefile

-or-

$ make -f smurfy_makefile
Run Code Online (Sandbox Code Playgroud)

注意:如果您将文件命名为 file Makefile,则只需运行以下命令即可:

$ make
Run Code Online (Sandbox Code Playgroud)

问题 #3 - 运行 Makefile

Makefile是命令的数据文件make。它们不是可执行文件。

例子

使其可执行

$ chmod +x makefile
Run Code Online (Sandbox Code Playgroud)

运行

$ ./makefile 
./makefile: line 1: main.out:: command not found
g++: error: GradeBook.o: No such file or directory
g++: error: main.o: No such file or directory
g++: fatal error: no input files
compilation terminated.
./makefile: line 4: main.o:: command not found
g++: error: main.cpp: No such file or directory
g++: fatal error: no input files
compilation terminated.
./makefile: line 7: GradeBook.o:: command not found
g++: error: GradeBook.cpp: No such file or directory
g++: fatal error: no input files
compilation terminated.
./makefile: line 10: clean:: command not found
Run Code Online (Sandbox Code Playgroud)

其他问题

除了上述提示之外,我还建议您大量使用make的功能进行“试运行”或“测试模式”。开关:

   -n, --just-print, --dry-run, --recon
        Print the commands that would be executed, but do not execute them 
        (except in certain circumstances).
Run Code Online (Sandbox Code Playgroud)

例子

运行文件makefile

$ make -n -f makefile 
g++ -Wall -g -c GradeBook.cpp
g++ -Wall -g -c main.cpp
g++ -Wall -g -o main.out GradeBook.o main.o 
Run Code Online (Sandbox Code Playgroud)

但请注意,当我们运行此命令时,实际上并没有创建任何结果文件:

$ ls -l
total 4
-rw-rw-r--. 1 saml saml   0 Dec 22 08:39 GradeBook.cpp
-rw-rw-r--. 1 saml saml   0 Dec 22 08:45 GradeBook.h
-rw-rw-r--. 1 saml saml   0 Dec 22 08:45 main.cpp
-rwxrwxr-x. 1 saml saml 262 Dec 22 08:25 makefile
Run Code Online (Sandbox Code Playgroud)