为 ARM 项目编写 makefile

Mic*_*ino 4 gcc makefile

我的项目结构如下:

.
??? build
??? src
|   ??? rbpi
|   |   ??? gpio.h
|   ??? boot.c
|   ??? boot.s
|   ??? kernel.c
??? linker.ld
Run Code Online (Sandbox Code Playgroud)

这是一个简单的树莓派内核,可以让 LED 闪烁!我目前使用一个包含以下内容的简单 bat 文件来构建它:

arm-none-eabi-gcc -g -O0 -mfpu=neon-vfpv4 -mfloat-abi=hard -march=armv7-a -mtune=cortex-a7 -nostartfiles -Wl,-T,linker.ld src/kernel.c src/boot.c src/boot.s -o build/kernel.elf
arm-none-eabi-objcopy build/kernel.elf -O binary build/kernel7.img
Run Code Online (Sandbox Code Playgroud)

由于我将向该项目添加更多文件,因此我必须将每个文件附加到我的“buildscript”中。
如果可能,我想使用makefile.

如果我想要以下“规则”,我的 makefile 应该是什么样子的?

  • 在编译之前,清除目录中的所有*.elf*.img文件build
  • 编译目录中的所有*.c*.s文件src
  • kernel.elf文件输出到build目录中。
  • 使用链接描述文件linker.ld
  • 编译完成后,运行objcopy生成二进制文件。

Jul*_*ard 6

典型的Makefile可能看起来像......等等,这里有一个关于 GNU Make 的文档,里面有一个很好的简单 Makefile:http : //www.gnu.org/software/make/manual/make.html#Simple-Makefile

所以对你来说,一个简单的开始可能是:

SRC := $(wildcard src/*.c src/*.s)
CFLAGS :=  -g -O0 -mfpu=neon-vfpv4 -mfloat-abi=hard -march=armv7-a -mtune=cortex-a7 -nostartfiles -Wl,-T,linker.ld

all: build/kernel.img

build/kernel.elf: $(SRC)
    arm-none-eabi-gcc $(CFLAGS) $(SRC) -o $@


%.img: %.elf
    arm-none-eabi-objcopy $< -O binary $@

clean:
    rm -f build/*.elf build/*.img
Run Code Online (Sandbox Code Playgroud)

(小心,食谱必须以制表符开头,而不是像这里这样的四个空格,make 理解您的文件很重要,因此复制粘贴不起作用。)

您实际上并不需要在编译之前删除 elf 和 img 文件,这是 GNU Make 的角色,可以根据文件修改时间知道它是否必须重建。

在这里,工作:

$ tree
.
??? build
??? Makefile
??? src
    ??? boot.c
    ??? boot.s
    ??? kernel.c

$ make
arm-none-eabi-gcc -g -O0 -mfpu=neon-vfpv4 -mfloat-abi=hard -march=armv7-a -mtune=cortex-a7 -nostartfiles -Wl,-T,linker.ld src/boot.c src/kernel.c src/boot.s -o build/kernel.elf
arm-none-eabi-objcopy build/kernel.elf -O binary build/kernel.img

$ tree
.
??? build
?   ??? kernel.elf
?   ??? kernel.img
??? Makefile
??? src
    ??? boot.c
    ??? boot.s
    ??? kernel.c

$ make
make: Nothing to be done for 'all'.

$ touch src/boot.c # If I touch a file, make will have to rebuild evrything:

$ make
arm-none-eabi-gcc -g -O0 -mfpu=neon-vfpv4 -mfloat-abi=hard -march=armv7-a -mtune=cortex-a7 -nostartfiles -Wl,-T,linker.ld src/boot.c src/kernel.c src/boot.s -o build/kernel.elf
arm-none-eabi-objcopy build/kernel.elf -O binary build/kernel.img
Run Code Online (Sandbox Code Playgroud)

您真的应该看看非常好的文档:http : //www.gnu.org/software/make/manual/make.html您将无法在 stackoverflow 上询问您需要的任何修改要在你的 makefile 上做,从这个“bootstrap makefile”开始,你应该能够修改它以逐步学习,并将文档作为参考。