[英]How to write conditional statements inside a Makefile in C?
我有两个 C 文件(myfile1.c 和 myfile2.c),它们执行两个不同的任务。 用户只需键入: make program
,然后,如果用户键入./program 25 x.txt y.txt
,我希望 myfile1.c 运行。 如果用户键入./program 50 x.txt y.txt
我希望 myfile2.c 运行。 这里应该采用什么方法?
听起来您希望目标程序在 Makefile 中构建这两个文件:
.PHONY: program
program: myfile1 myfile2
然后编写基于 argv[1] 执行正确文件的program
。 这是一个 bash 实现:
#!/bin/bash
if [ "$1" = "25" ]
then
suffix=1
elif [ "$1" = "50" ]
then
suffix2=2
else
echo error
exit 1
fi
# validate arguments
# shift?
./myfile$suffix "$@"
通常,您只需编写一个程序(即 program.c),查看 argv[1] 以查看它是否需要运行 myfile1 或 myfile2 的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.