0

I have a folder structure as follows:

root dir
       |__build/
       |__Makefile
       |__mod_init.c

Makefile:

obj-m += mod_klm.o
mod_klm-y := mod_init.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 changes my folder structure as follows:

    root_dir
       |__build/
       |__Makefile
       |__mod_init.c
       |__mod_klm.ko
       |__mod_klm.mod.o
       |__mod_init.o
       |__mod_klm.mod
       ......

But I want all this generated .o .ko .order .mod files in build/ ie.,

   root_dir    
      |__build/
           |__mod_klm.ko
           |__mod_klm.mod.o
           |__mod_init.o
           |__mod_klm.mod
            ..........
      |__Makefile
      |__mod_init.c

I know this code is not enough to do that, but unable to figure out what else to add in the Makefile.

I already tried make .o files in separate folder in linux kernel compilation this. But getting the following errors.

make -C /lib/modules/5.5.15-200.fc31.x86_64/build  M=/home/aninsen/projects/kspace/kmod_template/kspace modules
make[1]: Entering directory '/usr/src/kernels/5.5.15-200.fc31.x86_64'
make[2]: Entering directory '/home/aninsen/projects/kspace/kmod_template/kspace/build'
  CC [M]  /home/aninsen/projects/kspace/kmod_template/kspace/mod_init.o
In file included from /usr/src/kernels/5.5.15-200.fc31.x86_64/include/linux/types.h:6,
                 from /usr/src/kernels/5.5.15-200.fc31.x86_64/include/linux/limits.h:6,
                 from /usr/src/kernels/5.5.15-200.fc31.x86_64/include/linux/kernel.h:7,
                 from /home/aninsen/projects/kspace/kmod_template/kspace/mod_init.c:1:
/usr/src/kernels/5.5.15-200.fc31.x86_64/include/uapi/linux/types.h:5:10: fatal error: asm/types.h: No such file or directory
    5 | #include <asm/types.h>
      |          ^~~~~~~~~~~~~
compilation terminated.
make[3]: *** [/usr/src/kernels/5.5.15-200.fc31.x86_64/scripts/Makefile.build:266: /home/aninsen/projects/kspace/kmod_template/kspace/mod_init.o] Error 1
make[2]: *** [/usr/src/kernels/5.5.15-200.fc31.x86_64/Makefile:1694: /home/aninsen/projects/kspace/kmod_template/kspace] Error 2
make[2]: Leaving directory '/home/aninsen/projects/kspace/kmod_template/kspace/build'
make[1]: *** [Makefile:179: sub-make] Error 2
make[1]: Leaving directory '/usr/src/kernels/5.5.15-200.fc31.x86_64'
make: *** [Makefile:25: all] Error 2

Thanks in advance.

Anindya
  • 41
  • 1
  • 13
  • No its not working, already tried that out. Getting error as that is trying to find a Makefile inside the build directory. – Anindya May 02 '20 at 09:46
  • Your description of the error is not very useful... Please, add to the question post your Makefile, which is based on the referenced question, and **exact error message** it produces. – Tsyvarev May 02 '20 at 09:54
  • Sorry for improper description. Added the error in the question. – Anindya May 02 '20 at 10:00
  • 1
    Have you got this output with `KBUILD_OUTPUT` variable set? (You don't show what exactly you have tried, so we could only guess). Anywhere, it seems that this variable and `O=` option doesn't work for out-of-tree modules. But `src=` option should work, as described in [that answer](https://stackoverflow.com/a/37858314/3440745). – Tsyvarev May 02 '20 at 10:04
  • Thanks a lot @Tsyvarev. It worked. One thing I am curious about is Why do we need a blank Makefile in build directory? – Anindya May 02 '20 at 11:04
  • yes @MarcoBonelli, it works. Thanks a lot. One thing I am curious about is Why do we need a blank Makefile in build directory? – Anindya May 02 '20 at 13:48
  • @Anindya that's because it does `M=$(BUILD_DIR)` and the kernel does `$(MAKE) -C` on that dir and it expects a Makefile there. – Marco Bonelli May 02 '20 at 14:02
  • @MarcoBonelli To run make in any directory we need a Makefile, even if that is blank it is ok. As, the command it needs to run, it can get from its sub-dir Makefile ie., $(src) here. Is my understanding correct? Is this idea true for all Makefile or only kernel module makefiles? – Anindya May 02 '20 at 14:17

0 Answers0