Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
确定在C中使用makefile时将运行哪些命令?_C_Makefile - Fatal编程技术网

确定在C中使用makefile时将运行哪些命令?

确定在C中使用makefile时将运行哪些命令?,c,makefile,C,Makefile,现在我正在为一门计算机科学课程做一些练习题。我遇到的其中一个涉及makefile,如下所示: You are working in a directory which contains the following files and no others: makefile myscript one.c three.c two.h ------------------------------------ Here are the contents of makefi

现在我正在为一门计算机科学课程做一些练习题。我遇到的其中一个涉及makefile,如下所示:

You are working in a directory which contains the following files and no others:
makefile     myscript     one.c     three.c     two.h
------------------------------------
Here are the contents of makefile:

CC=gcc
CFLAGS=-ansi

myprogram: two.o three.o
 gcc -o myprogram two.o three.o

two.c: one.c myscript
 sed -rf myscript one.c >| two.c

three.o: two.h
------------------------------------

1.)  If you type `make myprogram` in this directory, what commands will be run?

2.)  After the commands are run, what new files will have been created?

3.)  After this, you edit `two.h` (and no other files) and then type make `myprogram` again. What
commands will be run? 
因此,根据我对
Makefile
的有限理解,我知道对于
1.)
来说,要运行的命令将是
gcc-o myprogram two.o three.o
,因为
myprogram
在目录中不存在。但是,
two.o
two.o
也不存在。。。这就是我有点迷糊的地方,我是否会运行
two.c:one.cmyscript
下面的命令,因为它会强制覆盖?非常感谢您的帮助,谢谢

  • three.o

    对于
    three.o
    ,有一个特定的规则,因此解释了
    three.o
    的来源

  • two.o

    make
    有很多隐式规则:它知道(猜测)如果找到
    *.c
    文件,如果没有特定的规则,它应该将其编译成
    *.o
    文件,甚至更进一步,创建一个
    a.out
    /可执行文件

    这些隐式规则将在
    two.o
    的情况下工作,它没有特定的规则,但是
    make
    可以从
    two.c
    推断规则

  • three.o
    (重新发行)

    make
    还可以很好地猜测源文件应该是什么(如果没有给出):在
    three.o
    的情况下,它将尝试将其与
    three.c
    匹配,后者是该对象文件的逻辑源文件。(
    two.h
    只是一个附加依赖项)


如果你真的想知道,就做练习。这可能有点困难,因为
myscript
sed
命令,但请记住,您基本上可以创建几乎为空的源文件(在头文件中包含卫士仍然是一个好主意,因此不会意外地包含两次)

然后,您可以使用
-d
(调试)标志运行
make
,或者使用
-n
(无操作标志;不执行任何操作)。只需执行步骤1、2和3


当然,做所有这些可能会告诉你发生了什么,但这并不意味着你理解它。结合您对Make的了解,这个答案中的上述内容以及您从运行实际实验中获得的结果应该会让您受益匪浅。

一些Make实用程序可以选择显示将要运行的命令,但不运行它们。例如,对于Linux和Unix,使其为-n,-仅打印,-干运行,-recon。对于Microsoft nmake,选项为/n


如果涉及脚本或批处理文件,可能会更加困难。

好的,由于make的隐含规则,
two.o
two.o
也会“make”吗?
two.o
在我的答案中;请仔细阅读
three.o
有一个规则,但是有一个隐式源文件(所以称之为Make,它使用了“部分隐式规则”)。非常感谢您的帮助,我会尽我所能理解您所展示的一切!