[英]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.