8

I am trying to compile a module against any source tree on the file system but I am having trouble with the Makefile. This was the original Makefile I had against the kernel specified:

obj-m += new-mod.o

all:
        make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
        make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

This Makefile would compile correctly, but goal is to have it compile against any source tree. I have tried just:

obj-m += new-mod.o

I thought that "all:" is assumed but I get error:

make: *** No targets.  Stop.

I have also added:

all: 

to the Makefile with no difference except for error message:

make: Nothing to be done for `all'

I have tried a lot of documentation but no luck. I would greatly appreciate any help.

pevik
  • 4,523
  • 3
  • 33
  • 44
Chris Fritz
  • 281
  • 2
  • 4
  • 14
  • I am also facing the same problem, I used following Makefile > KVERSION=$(shell uname -r) PWD := $(shell pwd) all: make -C /lib/modules/$(KVERSION)/build/ M=$(PWD) modules clean: make -C /lib/modules/$(KVERSION)/build M=$(PWD) clean $(info Building with KERNELRELEASE = ${KERNELRELEASE}) obj-m := assignment1.o – Rahul Mar 13 '14 at 16:19

2 Answers2

11

goal is to have it compile against any source tree

ya you can do it providing a compiled source-code path

just replace make -C /lib/modules/$(shell uname -r)/build M=$PWD modules

with this

make -C <path-to-compiled-src-code> M=$PWD modules

make -C /home/vinay/linux-3.9 M=$PWD modules

try below makefile

Makefile –

# if KERNELRELEASE is defined, we've been invoked from the
# kernel build system and can use its language.
ifneq (${KERNELRELEASE},)
obj-m := new-mod.o
# Otherwise we were called directly from the command line.
# Invoke the kernel build system.
  else
    KERNEL_SOURCE := /usr/src/linux
    PWD := $(shell pwd)
default:
      ${MAKE} -C ${KERNEL_SOURCE} SUBDIRS=${PWD} modules

clean:
      ${MAKE} -C ${KERNEL_SOURCE} SUBDIRS=${PWD} clean
endif

In above you can change KERNEL_SOURCE := /usr/src/linux-->to.--> your sr-code KERNEL_SOURCE := <path to compiled-src-code>

for further info find below liks

while building kernel modules why do we need /lib/modules?

A simple program on linux device driver

How to make a built-in device driver in linux

PypeBros
  • 2,607
  • 24
  • 37
vinay hunachyal
  • 3,781
  • 2
  • 20
  • 31
  • What if I dont know KERNEL_SOURCE, as I dont know what will be the value for this variable in the target system, so can you provide any other way to do the same. – Rahul Mar 13 '14 at 15:54
  • Add "CC=" on the make line, like `make CC=/home/myGcc -C M=...` – Danny Jul 09 '19 at 03:21
  • `$ make -C ~/stuff/openwrt/build_dir/toolchain-*/linux M=$PWD modules` results in `./include/uapi/linux/types.h:5:10: fatal error: asm/types.h: No such file or directory` so this cannot be all of it ☹ – mirabilos Sep 22 '22 at 21:31
0

Build against your custom kernel source ( not the one that is installed), You can use following steps.

1.download kernel from kernel.org (tar)

2.Extract

3.make x86_64_defconfig

4.make prepare

5.make modules_prepare

Now you have to change Makefile to point to kernel source you had downloaded and extracted. Something similar mentioned in example by Vinay Answer.

Just remember you cannot insmod this module as kernel running and module built is for different kernel.

Alok Prasad
  • 622
  • 7
  • 12