makefile运行它编译的代码

don*_*lan 2 c++ makefile

如果我有一个将运行的代码,调用它main.cpp,并且可执行文件是r.exe,那么我编写一个包含以下目标的makefile:

compile: 
    g++ -std=c++11 main.cpp -o r
Run Code Online (Sandbox Code Playgroud)

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

make run i.txt o.txt
Run Code Online (Sandbox Code Playgroud)

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

run:
    r.exe $1 $2
Run Code Online (Sandbox Code Playgroud)

例如,但是声明:"'r'是最新的"和"没有为'i.txt'做什么,......等等"

我现在也尝试过搜索一段时间,但是"make","run"和"变量"或"参数"本质上都是一个不相关内容的搜索防火墙.

Bar*_*rry 5

你无法将参数传递给make那样的人.该命令make run i.txt o.txt将试图建立的规则run,i.txto.txt.

您可以改为使用变量:

run:
    r.exe ${ARGS}

make run ARGS="i.txt o.txt"
Run Code Online (Sandbox Code Playgroud)

旁注,规则应该制作他们说他们所做的文件.所以你真的希望你的编译规则看起来像:

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

compile : r.exe
.PHONY  : compile
Run Code Online (Sandbox Code Playgroud)