C 在make文件中包括输入语句和条件

C 在make文件中包括输入语句和条件,c,ubuntu,makefile,C,Ubuntu,Makefile,我第一次尝试创建一个makefile。我读了一些教程,并成功地创建了一个,但我在一些方面遇到了问题。详情如下 以下是按执行顺序排列的文件: CSV_to_txt.c-不依赖任何其他文件 我想在make文件中包含CSV\u文件/balancement\u trajectories.CSV,这是我的输入。此外,我在终端中运行命令tac Chemical_balancement.txt。我可以把这个也包括在make文件中吗 fluid_profile.c-取决于pdfutil.h和beta_util

我第一次尝试创建一个makefile。我读了一些教程,并成功地创建了一个,但我在一些方面遇到了问题。详情如下

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

  • CSV_to_txt.c
    -不依赖任何其他文件
  • 我想在make文件中包含
    CSV\u文件/balancement\u trajectories.CSV
    ,这是我的输入。此外,我在终端中运行命令
    tac Chemical_balancement.txt
    。我可以把这个也包括在make文件中吗

  • fluid_profile.c
    -取决于
    pdfutil.h
    beta_util.h
  • 我在读取输入时也有同样的问题,例如:

    输入点数

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

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

    生成文件的结构:

    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 *~
    
    任何类型的帮助,甚至是教程的链接都会很有帮助。

    建议的生成文件:

    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 *~
    
    
    因此,一个分解——第一行,predeclare target
    run
    ,这样它就成为默认目标(如果您执行
    make
    ,它将运行第一个目标)。将其声明为虚假目标(这意味着没有生成名为
    run
    的实际文件。您可以查找更多详细信息)

    然后创建一些规则来生成可执行文件。每个可执行文件都有自己的生成规则。通常,您会使用建议的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 *~
    
    
    因此,一个分解——第一行,predeclare target
    run
    ,这样它就成为默认目标(如果您执行
    make
    ,它将运行第一个目标)。将其声明为虚假目标(这意味着没有生成名为
    run
    的实际文件。您可以查找更多详细信息)


    然后创建一些规则来生成可执行文件。每个可执行文件都有自己的生成规则。通常情况下,您会使用
    $@
    $这样的方法,因为您有一个常见的新手错误。。。这就是认为源文件依赖于其他源文件(一个
    .c
    文件依赖于一些
    .h
    文件),这是一个错误,可能是您没有得到结果的原因

    Makefile
    的目标是描述文件依赖关系,以便执行最少的命令集来构建指定的最终目标。为此,你需要认为目标是你要创造的东西

    .c
    文件是您在构建过程中创建的吗?不是,因此它不能成为规则的目标。目标确实是汇编的结果。源文件不依赖于头文件。。。它只是包含它来编译
    .o
    目标(这实际上就是目标)

    假设您有一个程序
    hello.c
    ,其中包括
    modA.h
    modB.h
    。(甚至
    modB.h
    也包括
    modB2.h
    )如果您修改其中任何一个,您需要重新编译
    hello.c
    ,,因此您的规则是:

    # (1)
    hello.o: hello.c modA.h modB.h modB2.h
        cc -c hello.c                 # (2) see below.
    
    • (1) 规则行从第1列开始,左侧(目标文件)和源列表(依赖项)。每次make看到目标不存在或最后一次更改日期早于任何依赖项的更改日期时,都会依次执行下面的命令行
    • (2) 命令规则以行的第一列中的
      字符开头。它表示从源代码生成目标文件所需的命令(或命令列表,每个命令在其命令行中)
    • 从代码< > <代码>开始的行是注释行(也可以在规则或命令行的中间开始)
    • 行的类型(宏定义)是不一样的,但在开始学习如何创建宏之前,您需要先学习如何创建依赖项并习惯它们。首先阅读
      make(1)
      doc。 您可以看到,我们只编译
      hello.c
      ,但每次更改上述任何其他文件时都必须编译。有两个模块,
      modA.o
      modB.o
      ,每个模块都带有
      .c
      文件和
      hello.c
      中所需的内容。因此:
    因此,当我们更改
    modA.c
    modA.h
    中的任何一个时,就会创建
    modA.o
    。正如我们上面所说的,包含了modB2.h,如果我们修改它,它应该被编译

    现在,要链接的程序的依赖关系:在编译程序时,它只有三个模块:
    hello.o
    modA.o
    modB.o
    。要创建
    hello
    所有这三个模块都必须提供给链接器。。。。因此,Makefile还需要:

    hello: hello.o modA.o modB.o
        cc -o hello hello.o modA.o modB.o
    
    因此,完整的
    Makefile
    是:

    # this rule is put first to become the default target.
    # the default target is the final program.
    hello: hello.o modA.o modB.o
        cc -o hello hello.o modA.o modB.o
    
    # this rule is for the hello.o target only.
    hello.o: hello.c modA.h modB.h modB2.h
        cc -c hello.c
    
    # this rule is for modA.o
    modA.o: modA.c modA.h
        cc -c modA.c
    
    # and this one for modB.o
    modB.o: modB.c modB.h modB2.h
        cc -c modB.c
    
    
    有了这个
    Makefile
    你会很享受,因为你可以触摸任何文件,但是编译器只编译正确的依赖项来生成最终的可执行程序


    Make具有更多的功能,但这要求您至少了解最基本的功能。一旦成功创建了正确的依赖项,就可以开始研究make的其他功能,这些功能只是为了简化/避免多次重写相同的内容。但至少要阅读《制作手册》一页。

    你有一个常见的新手错误。。。这就是认为源文件依赖于其他源文件(一个
    .c
    文件依赖于一些
    .h
    文件),这是一个错误,可能是您没有得到结果的原因

    目标