makefile运行它编制的代码

makefile to run the code it compiles

本文关键字:代码 运行 makefile      更新时间:2023-10-16

如果我有一个可以运行的代码,将其称为 main.cpp,并且可执行文件为 r.exe,则用以下目标编写makefile:

compile: 
    g++ -std=c++11 main.cpp -o r

可执行文件,r.exe作为两个参数i.txto.txt。我如何在makefile中添加第二个目标,以便可以运行以下命令,并查看程序执行:

make run i.txt o.txt

我尝试在makefile中添加第二个目标:

run:
    r.exe $1 $2

例如,但要声明:"'r'是最新的"answers"为'i.txt',...等无需完成。"

我还尝试了一段时间,但是"制作","运行"answers"变量"或"参数"本质上具有无关内容的搜索防火墙。

您不能这样将参数传递给make。命令make run i.txt o.txt将尝试构建规则runi.txto.txt

您可以使用一个变量:

run:
    r.exe ${ARGS}
make run ARGS="i.txt o.txt"

旁注,规则应制作他们说的文件。因此,您确实希望您的编译规则看起来像:

r.exe : main.cpp
    g++ -std=c++11 $^ -o $@
compile : r.exe
.PHONY  : compile