简体   繁体   English

Makefile无法生成目标代码或可执行文件

[英]Makefile not generating object code or executable

I'm trying to write a makefile that generates assembly code, object code, and an executable from a .c file. 我正在尝试编写一个生成文件,该文件从.c文件生成汇编代码,目标代码和可执行文件。 This is the contents of my makefile: 这是我的makefile的内容:

good_echo.s: good_echo.c
    gcc -S -fstack-protector-all good_echo.c
good_echo.o: good_echo.s
    gcc -S good_echo.s -o good_echo.o
good_echo: good_echo.o
    gcc -v good_echo.o -o good_echo
clean:
    rm -f good_echo.o good_echo.s good_echo 

When I enter make into terminal, it only generates the assembly code. 当我在终端中输入make时,它仅生成汇编代码。 Can someone explain what's going on? 有人可以解释发生了什么吗?

If you check the GNU make manual How make Processes a Makefile you'll notice this sentence: 如果查看GNU make手册“ 如何使Makefile处理文件”,您会注意到以下句子:

make reads the makefile in the current directory and begins by processing the first rule. make读取当前目录中的makefile并从处理第一条规则开始。

Followed by in the next paragraph: 接下来的下一段:

If some other rule is not depended on by the goal (or anything it depends on, etc.), that rule is not processed, unless you tell make to do so (with a command such as make clean). 如果目标不依赖其他规则(或它所依赖的任何东西,等等),则不会处理该规则,除非您告知make这样做(使用make clean之类的命令)。

Here your first rule says to build good_echo.s , and none of the other targets are prerequistes of that, so it's the only one built. 在这里,您的第一条规则说要构建good_echo.s ,而其他目标都不是前提条件,因此这是唯一构建的目标。

If you run make good_echo then all will be built. 如果运行make good_echo则将全部构建。 Alternatively you can ensure that the rule to build the target good_echo is the first rule in your makefile. 另外,您可以确保构建目标good_echo的规则是makefile中的第一条规则。

Or, you can use an often-used idiom and create a target all as the first rule which depends on the things you want to generate: 或者,您可以使用一个常用的习惯用法,并根据您要生成的内容将all目标创建为第一条规则:

all: good_echo

To be clear, other than the first rule in the makefile it's not important in which order the rules are defined. 需要明确的是,除了makefile中的第一个规则外,定义规则的顺序并不重要。

ETA ETA

As pointed out by Joseph Quinsey in the comments, your rule here: 正如约瑟夫·昆西在评论中指出的,您在这里的规则:

good_echo.o: good_echo.s
        gcc -S good_echo.s -o good_echo.o

is incorrect. 是不正确的。 The -S option tells the compiler to stop after generating assembly and write it out, but already have assembly as your input. -S选项告诉编译器在生成汇编并将其写出后停止,但已经将汇编作为输入。 You want an object file, so you should use the -c option to tell the compiler to stop after generating an object file (and not try to link): 您需要一个目标文件,因此应该使用-c选项告诉编译器在生成目标文件后停止(而不是尝试链接):

good_echo.o: good_echo.s
        gcc -c good_echo.s -o good_echo.o

To be more clear: the compiler can infer what type of input it's given by using the extension to the file (in this case .s means assembly)--or you can force it with the -x option if you prefer--but it won't infer the type of output you want: you always have to tell it that with options such as -c , -S , and -E . 更清楚地说:编译器可以通过使用文件扩展名来推断输入的类型(在这种情况下, .s表示汇编)-或者,如果愿意,可以使用-x选项强制输入,但是它可以不会推断您想要的输出类型:您总是必须使用-c-S-E选项来告诉它。 If you don't use any of those then it will run all steps and try to generate an executable program. 如果您不使用其中任何一个,它将运行所有步骤并尝试生成可执行程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM