首页 > 解决方案 > Makefile 编译问题

问题描述

我正在尝试使用制作的 makefile 编译我的 .cpp 文件,但它不会编译。我有 2 个目录 ex1 和 ex2。帮助将不胜感激,并提前感谢!

生成文件:


output: Main.o Gamer.o Manager.o Person.o EncryptionDerived.o
    g++ Main.o Gamer.o Manager.o Person.o EncryptionDerived.o -o output -I.

./Main.o: ./ex1/Main.cpp
    g++ -c ./ex1/Main.cpp

./Gamer.o: ./ex1/Gamer.cpp
    g++ -c ./ex1/Gamer.cpp

./Manager.o: ./ex1/Manager.cpp
    g++ -c ./ex1/Manager.cpp

./Person.o: ./ex1/Person.cpp
    g++ -c ./ex1/Person.cpp
 
./EncryptionDerived.o: ./ex2/EncryptionDerived.cpp
    g++ -c ./ex2/EncryptionDerived.cpp

clean:
    rm -rf *.o output

run:
    ./output

这就是我从命令行得到的

./makefile: line 1: ./Main.o:: No such file or directory
        ./makefile: line 4: ./Gamer.o:: No such file or directory
./makefile: line 7: ./Manager.o:: No such file or directory
./makefile: line 10: ./Person.o:: No such file or directory
./makefile: line 13: ./EncryptionDerived.o:: No such file or directory
./makefile: line 16: clean:: command not found
./makefile: line 19: all:: command not found
./makefile: line 21: output:: command not found
g++.exe: error: Main.o: No such file or directory
g++.exe: error: Gamer.o: No such file or directory
g++.exe: error: Manager.o: No such file or directory
g++.exe: error: Person.o: No such file or directory
g++.exe: error: EncryptionDerived.o: No such file or directory
g++.exe: fatal error: no input files
compilation terminated.
./makefile: line 24: run:: command not found
./makefile: line 25: ./output: No such file or directory

标签: c++makefilegit-bash

解决方案


尝试这个:

output: Main.o Gamer.o Manager.o Person.o EncryptionDerived.o
    g++ Main.o Gamer.o Manager.o Person.o EncryptionDerived.o -o output -I.

Main.o: ex1/Main.cpp
    g++ -c ex1/Main.cpp

Gamer.o: ex1/Gamer.cpp
    g++ -c ex1/Gamer.cpp

Manager.o: ex1/Manager.cpp
    g++ -c ex1/Manager.cpp

Person.o: ex1/Person.cpp
    g++ -c ex1/Person.cpp
 
EncryptionDerived.o: ex2/EncryptionDerived.cpp
    g++ -c ex2/EncryptionDerived.cpp

clean:
    rm -rf *.o output

run:
    ./output

推荐阅读