c - 如何编写 makefile 以使用 -S 和 -c 进行编译?

标签 c gcc makefile

我有一个源文件 test.c 及其 header test.h,一个文件 main.cmakefile 。 我想设置我的 makefile,以便它 (1) 编译 test.c 以构建可执行文件 test.o, (2) 编译 test.c 以使用 -S 标志将汇编代码打印到 test.s

我已尝试按我认为正确的方式设置 makefile,但这当然不会实际运行最后的 test.s 行。

FLAGS = -c -Wall
CC = gcc
ASM = -S

all : optimize
optimize: main.o test.o
    $(CC) main.o test.o -o optimize
main.o: main.c test.h
    $(CC) $(FLAGS) main.c
test.o: test.h test.c
    $(CC) $(FLAGS) test.c
test.s: test.h test.c
    $(CC) $(ASM) -Wall test.c

谁能告诉我我应该如何以不同的方式构造它,以创建两者 test.s 和 test.o?

我尝试将 optimize: 行和以下行更改为:

optimize: main.o test.o test.s
    $(CC) main.o test.o test.s -o optimize

但是链接器给出了一个错误'test'的多重定义

最佳答案

只需将 test.s 添加到以 optimize: 开头的行。

这样做是将规则 test.s 添加到优化行的依赖项中。

您也可以按照 Vaughn Cato 的建议将它添加到 all 行。

重要的是要知道规则 test.s 和文件 test.s 之间存在差异

您可以将任何您想要的名称作为规则的名称,但按照惯例,您通常只使用文件名。

这是一个 Makefile 示例,它可以执行您想要的操作。我将 dep 放在 all 下,这样你就可以make asm,为了清楚起见,我还将规则名称更改为 asm。

FLAGS = -c -Wall
CC = gcc
ASM = -S

all : optimize asm
optimize: main.o test.o
    $(CC) main.o test.o -o optimize
main.o: main.c test.h
    $(CC) $(FLAGS) main.c
test.o: test.h test.c
    $(CC) $(FLAGS) test.c
asm: test.h test.c
    $(CC) $(ASM) -Wall test.c -o test.s

最后,因为您似乎对 Makefile 的工作方式不太了解(老实说,我并没有责怪您),所以我建议您阅读本教程:http://mrbook.org/tutorials/make/

关于c - 如何编写 makefile 以使用 -S 和 -c 进行编译?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13334093/

相关文章:

oracle - 在 makefile 中设置动态 ORACLE_HOME

c - GStreamer - 使用 RTSP 服务器编译

c - 这是语义错误还是语法错误?

c - “?”不适用于 C Posix 正则表达式

c - C服务器功能问题

c - x86_64-pc-cygwin gcc编译错误

ruby - 在 Mountain Lion 上安装 Ruby 时出现问题 - ruby​​ 1.9.3 不会编译

c - 为什么 math.h 需要链接到 makefile 而不是 string.h?

makefile - 如何在自动工具中强制最终目标

C 语言的 Javascript 等效项(*、f、-> 等)