c - 安装的 gcc 包含路径似乎不正确并且报告库和 header 不匹配

标签 c linux gcc build kernel-module

这是一个很长但很简单的基础问题。所以任何熟悉的人都可以回答我的问题。 我的 CentOS 6.4 系统上有一个简单的程序如下。 (把它放在 ~/test 下) 我想测试 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 ....

我认为这种“尝试用户内核头文件”在这种情况下是可以的。我必须找到“错误:在‘结构’之前预期‘=’、‘、’、‘;’、‘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

最佳答案

要编译内核模块,您应该编写一个 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

关于c - 安装的 gcc 包含路径似乎不正确并且报告库和 header 不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25499246/

相关文章:

c - 带字符串的多行内联汇编宏

c - 硬件仿真的正确实现是什么?

c - 如何避免 MSVC 警告 C4701 : potentially uninitialised local variable

c++ - OpenCl 代码在一台机器上工作,但我在另一台机器上得到 CL_INVALID_KERNEL_ARGS

c - 如何用简单的 jmp 程序集实现 import lib?

c++ - 将条码扫描仪输入重定向到 Linux 中的特定小部件

linux - 在嵌入式 Linux 平台上将 libc 与 JNI 代码链接 (GuruPlug)

node.js - 使用 npm 在 linuxmint 操作系统上安装大写模块的问题

c - 包含 header 但仍然缺少声明?

c++ - 为什么需要引用的函数使用指针?