首页 > 解决方案 > 在 make 文件中包含输入语句和条件

问题描述

我第一次尝试创建一个makefile。我浏览了一些教程并设法创建了一个,但我遇到了一些问题。以下是详细信息。

以下是按执行顺序排列的文件:

  1. CSV_to_txt.c- 不依赖任何其他文件。

在此处输入图像描述

我想CSV_files/Equilibrium_trajectories.csv在 make 文件中包含我的输入。此外,我 tac Chemical_Equilibrium.txt在终端中运行命令。我也可以将它包含在make文件中吗?

  1. fluid_profile.c- 取决于pdfutil.hbeta_util.h

我在读取输入时遇到同样的问题,例如:

Enter the number of points

1000--> 包含在 make 文件中。

此文件创建一个名为fluid_points.txt. 我想在 makefile 中包含的是,如果该文件已经存在,请不要执行命令gcc fluid_points.c -o fluid_points.o -lm

make文件的结构:

all:
     gcc CSV_to_txt.c -o CSV_to_txt.o -lm
     ./CSV_to_txt.o
     #Include the file path and name when asked for it

     #ubuntu terminal command --> tac filename.txt > filename_changed.txt

     gcc fluid_profile.c -o fluid_profile.o -lm
     ./fluid_profile.o
     #Enter the number of points when prompted to do so

     #If fluid_points.txt file is already existing don't execute the above command, instead execute the below one

     gcc blah.c -o blah.o -lm
     ./blah.o

clean:
     $(RM) *.o *~

任何形式的帮助,甚至是教程的链接都会有所帮助。

标签: cubuntumakefile

解决方案


建议的makefile:

run:

.PHONY: run

CSV_to_txt: CSV_to_txt.c
     gcc CSV_to_txt.c -o CSV_to_txt -lm

fluid_profile: fluid_profile.c
     gcc fluid_profile.c -o fluid_profile -lm

blah: blah.c
     gcc blah -o blah.c -lm

run: CSV_to_txt fluid_profile blah
     echo "CSV_files/Equilibrium_trajectories.csv" | ./CSV_to_txt.o 
     tac Chemical_Equilibrium.txt
     echo "1000" | ./fluid_profile.o
     ./blah.o

clean:
     $(RM) *.o *~

所以,分解——第一行,预先声明目标run,使其成为默认目标(如果你这样做make,它将运行第一个目标)。将其声明为虚假目标(这意味着没有实际文件被称为run正在生成。您可以查找.PHONY更多详细信息)

然后创建一些规则来生成可执行文件。每个可执行文件都有自己的规则来生成它。通常你会为这些像和使用自动变量,但我现在想保持简单。$@$<

然后规则为run。这取决于可执行文件(因此可执行文件将在此规则运行之前完成构建)。

然后,要将文件名传递给可执行文件,您可以简单地echo将文件名传递给可执行文件。


推荐阅读