[root@localhost globalmem]# insmod globalmem.ko
insmod: error inserting 'globalmem.ko': -1 Invalid module format
内核加载出现错误: 错误主要有Makefile里面的内核版本跟PC系统的版本不一样,另外一个就是gcc版本问题;
第一个:
Makefile内容如下
obj-m :=globalmem.o
KERNEL_DIR :=/lib/modules/$(shell uname -r)/build
PWD :=$(shell pwd)
all:
make -C $(KERNEL_DIR) SUBDIRS=$(PWD) modules
clean:
rm *.o *.ko *.mod.c
这样的Makefile一般不会出现内核版本不对,有些人是固定的:比如
KERNEL_DIR :=/usr/src/linux-2.4.20-8 如果与当前的版本号(uname -r)不一致就会出错。
第二个:
查看gcc版本,我安装过4.1.2版本的gcc,所以默认是4.1.2,在/usr/local/bin/目录下:
[root@localhost globalmem]# gcc -v
Using built-in specs.
Target: i386-redhat-linux
Configured with: /usr/local/src/gcc-4.1.2/configure --enable-threads=posix --disable-checking --enable--long-long --host=i386-redhat-linux --with-system-zlib --enable-languages=c,c++,java
Thread model: posix
gcc version 4.1.2
[root@localhost globalmem]# which gcc
/usr/local/bin/gcc
查看/usr/bin下的gcc版本,也就是系统装的时候带的版本。
[root@localhost globalmem]# /usr/bin/gcc -v
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.2.2/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --disable-checking --with-system-zlib --enable-__cxa_atexit --host=i386-redhat-linux
Thread model: posix
gcc version 3.2.2 20030222 (Red Hat Linux 3.2.2-5)
由于自己不知道怎么改gcc版本,怎么改为3.2.2的,就做了一个软链接,
[root@localhost globalmem]# cd /usr/local/bin/
[root@localhost bin]# mv gcc gcc.old
[root@localhost bin]# ln -sf /usr/bin/gcc /usr/local/bin/gcc
这样弄完后查看gcc版本:
[root@localhost bin]# gcc -v
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.2.2/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --disable-checking --with-system-zlib --enable-__cxa_atexit --host=i386-redhat-linux
Thread model: posix
gcc version 3.2.2 20030222 (Red Hat Linux 3.2.2-5)
在make, [root@localhost globalmem]# insmod globalmem.ko 就可以成功了。
redhat9 下怎么改gcc版本 继续google中。。。