לוין ליאור
לוין ליאור

Reputation: 23

Writing a generic makefile for C and ASSEMBLY files

I'm building a linux kernel module written in ASM and C. individually, the code can compile, but I can't figure out how to compile it together (Both C and ASM files). I have 2 c files (entry.c, cpu_checks.c), 1 header file (cpu_checks.h) and 1 assembly file (cpu.asm).

I'm trying to compile all to .o object files and then link them together. My problem is that for some resaon the makefile doesn't recognize the rule for building .asm files.

This is how my makefile looks as of now:

CONFIG_MODULE_SIG=n
KDIR ?= /lib/modules/`uname -r`/build

DRIVER_DIR=path/to/driver/dir

CC = gcc
CFLAGS += -g -DDEBUG
LD = ld
LDFLAGS = -melf_i386
NASM = nasm
NASMFLAGS = -f elf64 -F stabs 

obj-m := hyper.o
hyper-objs := $(patsubst $(DRIVER_DIR)/%.asm, $(DRIVER_DIR)/%.o, $(wildcard $(DRIVER_DIR)/*.asm)) 
hyper-objs += $(patsubst $(DRIVER_DIR)/%.c, $(DRIVER_DIR)/%.o, $(wildcard $(DRIVER_DIR)/*.c))

default:
    make -C $(KDIR) M=$(PWD) modules
    $(LD) $(LDFLAGS) $(hyper-objs)

# Makefile recognizes this rule.
$(DRIVER_DIR)/%.o: $(DRIVER_DIR)/%.c
    $(CC) $(CFLAGS) $@ $<

# Makefile doesn't recognize this rule.
$(DRIVER_DIR)/%.o: $(DRIVER_DIR)/%.asm
    $(NASM) $(NASMFLAGS) $@

clean:
    rm -rf *.o *~

real_clean:
    rm -f *.o *.ko *~

I'm not very good at writing makefiles, so I might (even probably) have written something wrong. If not, what can I do to et the makefile to recognize that rule?

Upvotes: 1

Views: 1036

Answers (1)

Chris Dodd
Chris Dodd

Reputation: 126448

The problem is that your default: rule doesn't have any dependencies to check. So it will just run the commands under it without building any of the .o files that you need. Adding $(hyper-objs) as a dependency will ensure all the .o files are built first

default: $(hyper-objs)

Upvotes: 0

Related Questions