这是一个长而简单的基本问题。所以任何熟悉的人都可以回答我的问题。 我的CentOS 6.4系统下面有一个简单的程序。 (有没有〜/测试) 我想测试insmod和rmmod。
#include <linux/module.h>
static int __init hello_world( void )
{
printk( "hello world!\n" );
return 0;
}
static void __exit goodbye_world( void )
{
printk( "goodbye world!\n" );
}
module_init( hello_world );
module_exit( goodbye_world );
当我做的时候
gcc -o hello_world hello_world.c
我得到了
hello_world.c:1:26: error: linux/module.h: No such file or directory
hello_world.c:3: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'hello_world'
hello_world.c:9: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'goodbye_world'
hello_world.c:14: warning: data definition has no type or storage class
hello_world.c:14: warning: parameter names (without types) in function declaration
hello_world.c:15: warning: data definition has no type or storage class
hello_world.c:15: warning: parameter names (without types) in function declaration
所以我认为包含路径设置不正确。我搜索了这个linux / module.h所在的位置,发现linux / module.h位于/usr/src/kernels/2.6.32-358.2.1.el6.x86_64/include/linux/module.h.
(当我给'uname -a'时,我得到了
Linux stph45.etri.re.kr 2.6.32-358.2.1.el6.x86_64#1 SMP Wed Mar 13 00:26:49 UTC 2013 x86_64 x86_64 x86_64 GNU / Linux
所以这个/usr/src/kernels/2.6.32-358.2.1.el6.x86_64/include是正确的内核头目录。)
然后我再次尝试了这个
gcc -I/usr/src/kernels/2.6.32-358.2.1.el6.x86_64/include/ -o hello_world hello_world.c
得到了
In file included from /usr/src/kernels/2.6.32-358.2.1.el6.x86_64/include/linux/list.h:7,
from /usr/src/kernels/2.6.32-358.2.1.el6.x86_64/include/linux/module.h:9,
from hello_world.c:1:
/usr/src/kernels/2.6.32-358.2.1.el6.x86_64/include/linux/prefetch.h:14:27: error: asm/processor.h: No such file or directory
/usr/src/kernels/2.6.32-358.2.1.el6.x86_64/include/linux/prefetch.h:15:23: error: asm/cache.h: No such file or directory
... more lines ...
我在/usr/src/kernels/2.6.32-358.2.1.el6.x86_64/arch/x86/include/asm/processor.h中找到了这个体系结构相关的头文件。所以这次我做了
gcc -I/usr/src/kernels/2.6.32-358.2.1.el6.x86_64/arch/x86/include -I/usr/src/kernels/2.6.32-358.2.1.el6.x86_64/include -o hello_world hello_world.c
为arch依赖文件提供单独的标头路径。现在我有这些错误..
In file included from /usr/src/kernels/2.6.32-358.2.1.el6.x86_64/arch/x86/include/asm/percpu.h:45,
from /usr/src/kernels/2.6.32-358.2.1.el6.x86_64/arch/x86/include/asm/current.h:5,
from /usr/src/kernels/2.6.32-358.2.1.el6.x86_64/arch/x86/include/asm/processor.h:15,
from /usr/src/kernels/2.6.32-358.2.1.el6.x86_64/include/linux/prefetch.h:14,
from /usr/src/kernels/2.6.32-358.2.1.el6.x86_64/include/linux/list.h:7,
from /usr/src/kernels/2.6.32-358.2.1.el6.x86_64/include/linux/module.h:9,
from hello_world.c:1:
/usr/src/kernels/2.6.32-358.2.1.el6.x86_64/include/linux/kernel.h:949:2: warning: #warning Attempt to use kernel headers from user space, see http:
In file included from /usr/src/kernels/2.6.32-358.2.1.el6.x86_64/arch/x86/include/asm/processor.h:15,
from /usr/src/kernels/2.6.32-358.2.1.el6.x86_64/include/linux/prefetch.h:14,
from /usr/src/kernels/2.6.32-358.2.1.el6.x86_64/include/linux/list.h:7,
from /usr/src/kernels/2.6.32-358.2.1.el6.x86_64/include/linux/module.h:9,
from hello_world.c:1:
/usr/src/kernels/2.6.32-358.2.1.el6.x86_64/arch/x86/include/asm/current.h:12: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'struct'
.... more lines ....
我认为在这种情况下,“尝试用户内核头文件”是可以的。我必须在“struct”错误之前找到“错误:预期'=',',',';','asm'或'属性'的原因。
我认为我的gcc(我想它与CentOS一起出现。)似乎有些问题。谁能告诉我我的gcc安装有什么问题?看到下面的消息,有一些不匹配,我不知道治疗的程序。 (害怕破坏依赖于gcc的整个开发工具链)
ckim@stph45:~/testprog] echo "" | gcc -o /tmp/tmp.o -v -x c -
Using built-in specs.
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre --enable-libgcj-multifile --enable-java-maintainer-mode --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --disable-libjava-multilib --with-ppl --with-cloog --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC)
COLLECT_GCC_OPTIONS='-o' '/tmp/tmp.o' '-v' '-mtune=generic'
/usr/libexec/gcc/x86_64-redhat-linux/4.4.7/cc1 -quiet -v - -quiet -dumpbase - -mtune=generic -auxbase - -version -o /tmp/ccWAQshz.s
ignoring nonexistent directory "/usr/lib/gcc/x86_64-redhat-linux/4.4.7/include-fixed"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../x86_64-redhat-linux/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/local/include
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/include
/usr/include
End of search list.
GNU C (GCC) version 4.4.7 20120313 (Red Hat 4.4.7-4) (x86_64-redhat-linux)
compiled by GNU C version 4.4.7 20120313 (Red Hat 4.4.7-4), GMP version 4.3.1, MPFR version 2.4.1.
warning: GMP header version 4.3.1 differs from library version 4.3.2.
warning: MPFR header version 2.4.1 differs from library version 2.4.2.
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 11481e4aa93ef024f1be70ed47ae45e3
COLLECT_GCC_OPTIONS='-o' '/tmp/tmp.o' '-v' '-mtune=generic'
as -V -Qy -o /tmp/ccTYQoBw.o /tmp/ccWAQshz.s
GNU assembler version 2.20.51.0.2 (x86_64-redhat-linux) using BFD version version 2.20.51.0.2-5.36.el6 20100205
COMPILER_PATH=/usr/libexec/gcc/x86_64-redhat-linux/4.4.7/:/usr/libexec/gcc/x86_64-redhat-linux/4.4.7/:/usr/libexec/gcc/x86_64-redhat-linux/:/usr/lib/gcc/x86_64-redhat-linux/4.4.7/:/usr/lib/gcc/x86_64-redhat-linux/:/usr/libexec/gcc/x86_64-redhat-linux/4.4.7/:/usr/libexec/gcc/x86_64-redhat-linux/:/usr/lib/gcc/x86_64-redhat-linux/4.4.7/:/usr/lib/gcc/x86_64-redhat-linux/
LIBRARY_PATH=/usr/lib/gcc/x86_64-redhat-linux/4.4.7/:/usr/lib/gcc/x86_64-redhat-linux/4.4.7/:/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../lib64/:/lib/../lib64/:/usr/lib/../lib64/:/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-o' '/tmp/tmp.o' '-v' '-mtune=generic'
/usr/libexec/gcc/x86_64-redhat-linux/4.4.7/collect2 --eh-frame-hdr --build-id -m elf_x86_64 --hash-style=gnu -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o /tmp/tmp.o /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../lib64/crt1.o /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../lib64/crti.o /usr/lib/gcc/x86_64-redhat-linux/4.4.7/crtbegin.o -L/usr/lib/gcc/x86_64-redhat-linux/4.4.7 -L/usr/lib/gcc/x86_64-redhat-linux/4.4.7 -L/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../.. /tmp/ccTYQoBw.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/x86_64-redhat-linux/4.4.7/crtend.o /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../lib64/crtn.o
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../lib64/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: ld returned 1 exit status
答案 0 :(得分:1)
要编译内核模块,您应该编写一个Makefile来设置内核路径和其他环境变量。
您可以使用下面的Makefile来构建内核模块
obj-m := hello_world.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
将上述内容复制到与源文件相同的目录中的文件名Makefile。
只需输入 $ make 命令即可构建模块。输出将在同一目录中 hello_world.ko 。
答案 1 :(得分:1)
关于标头/库不匹配
warning: GMP header version 4.3.1 differs from library version 4.3.2.
warning: MPFR header version 2.4.1 differs from library version 2.4.2.
这可能不是问题。我认为GCC是用GMP 4.3.1和MPFR 2.4.1编译的,但之后,GMP和MPFR共享库分别升级到ABI兼容版本4.3.2和2.4.2。如果是这种情况,则允许这样做。