9

I am having problems getting any kernel modules to build on my machine. Whenever I build a module, modpost always says there are zero modules:

MODPOST 0 modules

To troubleshoot the problem, I wrote a test module (hello.c):

#include <linux/module.h>       /* Needed by all modules */
#include <linux/kernel.h>       /* Needed for KERN_INFO */
#include <linux/init.h>         /* Needed for the macros */

static int __init hello_start(void)
{
printk(KERN_INFO "Loading hello module...\n");
printk(KERN_INFO "Hello world\n");
return 0;
}

static void __exit hello_end(void)
{
printk(KERN_INFO "Goodbye Mr.\n");
}

module_init(hello_start);
module_exit(hello_end);

Here is the Makefile for the module:

obj-m = hello.o
KVERSION = $(shell uname -r)
all:
        make -C /lib/modules/$(KVERSION)/build M=$(shell pwd) modules
clean:
        make -C /lib/modules/$(KVERSION)/build M=$(shell pwd) clean

When I build it on my machine, I get the following output:

make -C /lib/modules/2.6.32-27-generic/build M=/home/waffleman/tmp/mod-test modules
make[1]: Entering directory `/usr/src/linux-headers-2.6.32-27-generic'
  CC [M]  /home/waffleman/tmp/mod-test/hello.o
  Building modules, stage 2.
  MODPOST 0 modules
make[1]: Leaving directory `/usr/src/linux-headers-2.6.32-27-generic'

When I make the module on another machine, it is successful:

make -C /lib/modules/2.6.24-27-generic/build M=/home/somedude/tmp/mod-test modules
make[1]: Entering directory `/usr/src/linux-headers-2.6.24-27-generic'
  CC [M]  /home/somedude/tmp/mod-test/hello.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /home/somedude/tmp/mod-test/hello.mod.o
  LD [M]  /home/somedude/tmp/mod-test/hello.ko
make[1]: Leaving directory `/usr/src/linux-headers-2.6.24-27-generic'

I looked for any relevant documentation about modpost, but found little. Anyone know how modpost decides what to build? Is there an environment that I am possibly missing?

BTW here is what I am running:

uname -a
Linux waffleman-desktop 2.6.32-27-generic #49-Ubuntu SMP Wed Dec 1 23:52:12 UTC 2010 i686 GNU/Linux

Edit

Here is make ran with V=1:

make -C /lib/modules/2.6.32-27-generic/build M=/home/waffleman/tmp/mod-test modules
make[1]: Entering directory `/usr/src/linux-headers-2.6.32-27-generic'
test -e include/linux/autoconf.h -a -e include/config/auto.conf || (        \
    echo;                               \
    echo "  ERROR: Kernel configuration is invalid.";       \
    echo "         include/linux/autoconf.h or include/config/auto.conf are missing.";  \
    echo "         Run 'make oldconfig && make prepare' on kernel src to fix it.";  \
    echo;                               \
    /bin/false)
mkdir -p /home/waffleman/tmp/mod-test/.tmp_versions ; rm -f /home/waffleman/tmp/mod-test/.tmp_versions/*
make -f scripts/Makefile.build obj=/home/waffleman/tmp/mod-test
  gcc -Wp,-MD,/home/waffleman/tmp/mod-test/.hello.o.d  -nostdinc -isystem /usr/lib/gcc/i486-linux-gnu/4.4.3/include  -Iinclude  -I/usr/src/linux-headers-2.6.32-27-generic/arch/x86/include -include include/linux/autoconf.h -Iubuntu/include  -D__KERNEL__ -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -Werror-implicit-function-declaration -Wno-format-security -fno-delete-null-pointer-checks -O2 -m32 -msoft-float -mregparm=3 -freg-struct-return -mpreferred-stack-boundary=2 -march=i586 -mtune=generic -maccumulate-outgoing-args -Wa,-mtune=generic32 -ffreestanding -fstack-protector -DCONFIG_AS_CFI=1 -DCONFIG_AS_CFI_SIGNAL_FRAME=1 -pipe -Wno-sign-compare -fno-asynchronous-unwind-tables -mno-sse -mno-mmx -mno-sse2 -mno-3dnow -Wframe-larger-than=1024 -fno-omit-frame-pointer -fno-optimize-sibling-calls -pg -Wdeclaration-after-statement -Wno-pointer-sign -fno-strict-overflow -fno-dwarf2-cfi-asm -fconserve-stack  -DMODULE -D"KBUILD_STR(s)=#s" -D"KBUILD_BASENAME=KBUILD_STR(hello)"  -D"KBUILD_MODNAME=KBUILD_STR(hello)"  -c -o /home/waffleman/tmp/mod-test/.tmp_hello.o /home/waffleman/tmp/mod-test/hello.c
  set -e ; perl /usr/src/linux-headers-2.6.32-27-generic/scripts/recordmcount.pl "i386" "32" "objdump" "objcopy" "gcc" "ld" "nm" "" "" "1" "/home/waffleman/tmp/mod-test/hello.o";
(cat /dev/null;   echo kernel//home/waffleman/tmp/mod-test/hello.ko;) > /home/waffleman/tmp/mod-test/modules.order
make -f /usr/src/linux-headers-2.6.32-27-generic/scripts/Makefile.modpost
  scripts/mod/modpost -m -a -i /usr/src/linux-headers-2.6.32-27-generic/Module.symvers -I /home/waffleman/tmp/mod-test/Module.symvers  -o /home/waffleman/tmp/mod-test/Module.symvers -S -w  -s
make[1]: Leaving directory `/usr/src/linux-headers-2.6.32-27-generic'
waffleman@waffleman-desktop:~/tmp/mod-test$ cat /home/waffleman/tmp/mod-test/modules.order
kernel//home/waffleman/tmp/mod-test/hello.ko
waffleman
  • 4,159
  • 10
  • 39
  • 63

13 Answers13

7

I spent all day glued to my computer fighting this precise problem..which ended up mysteriously disappearing like for OP.

I can at least offer this meager detail from my experience: I was getting the same output as OP (for make V=1) and putting print statements in ${kernel_directory}/scripts/makefile.build showed that obj-m was strangely NOT being set after including my makefile, even though it was clearly typed as above.

I did a bunch of fooling around with the line "obj-m += hello.o" and the ones around it. Eventually it magically worked..although it looked exactly the same as before in the end. Maybe I had copied those lines from a tutorial online and it contained some sort of invalid/incorrect character?

For anyone experiencing this, verify that obj-m is in fact getting set to hello.o
If it mysteriously isn't, delete the line, hell the whole Makefile, and retype it.

I know that's not much help; I wish I could reproduce what happened!

dsf
  • 145
  • 3
  • 8
2

In another thread I found that when I copy pasted the makefile contents, the -C after make was using the wrong "-" symbol and I had to re type it. It just so happens that this is the case for the obj-m += ... line above. You need to retype that character to make it valid. This should hopefully be found by anyone following the hello world module tutorial.

Fliperdo
  • 21
  • 1
  • I think this is the real solution o the problem. Wrong "-" character! – Xerusial Nov 13 '17 at 07:39
  • Yeah, this is a nasty kind of problem... once wasted hours figuring out that a co-worker was somehow getting pretty directional unicode quotes when copying a shell incantantion out of slack... – Chris Stratton Mar 10 '19 at 19:05
1

I guess you copied the contents of the Makefile from a PDF or some HTML document. The hyphens used are somewhat weird. Just try replacing the hyphens in the makefile; it will work like a charm.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
saai63
  • 138
  • 1
  • 8
  • Welcome to Stack Overflow. That's an interesting thesis, but I'm not sure it is justified here. At least, when I copy the makefile shown in the question to a Mac (Mac OS X 10.7.5; Firefox 15.0.1), and then analyze the data, all the characters are regular ASCII. That isn't to say that it doesn't happen; 'intelligent quotes' can certainly cause grief. I just don't see a basis for making that assumption with this question. (Additionally, if the hyphens weren't hyphens, the compiler would complain about not finding files with names that start with the non-hyphens; that isn't the trouble AFAICS). – Jonathan Leffler Sep 22 '12 at 19:35
  • I just shared what happened with me. I copied the contents of the Makefile from a PDF document to my Makefile opened through vim in Ubuntu-11.10. There the copied hyphens were invalid characters. – saai63 Sep 28 '12 at 05:11
1

I just ran into this same problem and for me it was caused by changing the default grep options via the GREP_OPTIONS environment variable. I didn't dig into the details, but something in the module build process didn't like my alternate grep output (include file name and line number). Removing the GREP_OPTIONS environment variable fixed things right up.

PunkFloyd
  • 36
  • 2
1

This happens because when you copy the make file contents from the PDF or any other tutorial websites and use it as it is. While you do a copy-paste, the contents will appear a bit weird in Linux environment. ie; Some special character issues will be there. If you retype the contents in Linux environment and do a make, this should work.

Suraj
  • 11
  • 2
0

Try to remove modules string from the Makefile:

obj-m = hello.o
KVERSION = $(shell uname -r)
all:
        make -C /lib/modules/$(KVERSION)/build M=$(shell pwd) # <--
clean:
        make -C /lib/modules/$(KVERSION)/build M=$(shell pwd) clean
Ilya Matveychikov
  • 3,936
  • 2
  • 27
  • 42
0

I was able to solve this problem by putting the

obj-m += <module name>.o

In a separate file named Kbuild. See Linux/documentation/kbuild/modules.txt for a hint as to why this might work

artsylar
  • 2,648
  • 4
  • 34
  • 51
cnnrznn
  • 435
  • 1
  • 4
  • 11
0

I had the same problem. Finally, I rebuilt the kernel, rewrote the makefile. It worked finally.

I guess the main reason is because I had M=$(PWD) modules in the following line right after make ARCH=arm...

0

On the machine that fails does your .config have module support disabled?

Try doing "make menuconfig" and make sure module support is enabled.

gravitron
  • 3,514
  • 1
  • 20
  • 18
0

I can only guess your kernel build environment is botched, because it passes both the theoretical check (the look of the developer) as well as the practical test:

make -C /lib/modules/2.6.36-rc8-32-desktop/build M=/dev/shm modules
make[1]: Entering directory `/usr/src/linux-2.6.36-rc8-32-obj/x86_64/desktop'
make -C ../../../linux-2.6.36-rc8-32 O=/usr/src/linux-2.6.36-rc8-32-obj/x86_64/desktop/. modules
  CC [M]  /dev/shm/hello.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /dev/shm/hello.mod.o
  LD [M]  /dev/shm/hello.ko
make[1]: Leaving directory `/usr/src/linux-2.6.36-rc8-32-obj/x86_64/desktop'
user562374
  • 3,817
  • 1
  • 22
  • 19
0

The error mysteriously went away. If anyone has an idea what could cause this. I'd like to know in case there is a next time.

waffleman
  • 4,159
  • 10
  • 39
  • 63
  • Most likely your distro did an automatic update, because it's been a week at my machine now, and no miraculous cures have manifested themselves yet. :/ – dimitarvp Jul 25 '12 at 11:52
  • I had the same problem with u. I copy the Makefile from http://www.tldp.org/LDP/lkmpg/2.6/html/lkmpg.html . It didn't work at the first time. Then I used a Makefile I was written before. Weirdly, then I use it again It works! I have no idea why It works. – louxiu Mar 17 '13 at 02:06
-1

I solved this problem by correcting Makefile, i.e.:

obj-m := modulename.o
anothernode
  • 5,100
  • 13
  • 43
  • 62
OPat
  • 1
  • 2
-1

I think it's not about special characters. I couldn't solve even when I manually typed! Then I tried again using kate text editor because previously I used nano and this time it worked like charm by even simple copy and paste. Because of that, I think the issue is the text editor

ignacio
  • 1,181
  • 2
  • 15
  • 28