简体   繁体   English

makefile运行它编译的代码

[英]makefile to run the code it compiles

If I have a code that will run, call it main.cpp , and the executable is r.exe , then I write a makefile with the following target: 如果我有将运行的代码,则将其称为main.cpp ,并且可执行文件为r.exe ,然后编写一个具有以下目标的makefile:

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

The executable, r.exe takes as two arguments i.txt , o.txt . 可执行文件r.exei.txto.txt作为两个参数。 How do I add a second target to the makefile such that I can run the following command, and see the program execute: 如何将第二个目标添加到makefile中,以便可以运行以下命令,并查看程序的执行情况:

make run i.txt o.txt

I have tried adding a second target to the makefile: 我尝试将第二个目标添加到makefile:

run:
    r.exe $1 $2

for instance, but make declares: "'r' is up to date" and "nothing to be done for 'i.txt', ... etc." 例如,但make声明:“'r'是最新的”和“对'i.txt'等不做任何事情,等等。”

I have also tried searching for a while now, but 'make', 'run' and 'variables' or 'arguments' have, essentially, a search firewall of unrelated content. 我也尝试过搜索一段时间,但是“ make”,“ run”和“ variables”或“ arguments”从本质上来说是具有无关内容的搜索防火墙。

You can't pass arguments to make like that. 您不能通过参数来make类似的操作。 The command make run i.txt o.txt would attempt to build the rules run , i.txt , and o.txt . 命令make run i.txt o.txt将尝试建立规则runi.txto.txt

What you could instead to is use a variable: 相反,您可以使用变量:

run:
    r.exe ${ARGS}

make run ARGS="i.txt o.txt"

Side-note, rules should make the files that they say they do. 旁注,规则应使它们声明的文件生效。 So you really would want your compile rule to look like: 因此,您确实希望您的编译规则如下所示:

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

compile : r.exe
.PHONY  : compile

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM