c - linux内核编程找不到头文件
ringa_lee
ringa_lee 2017-04-17 13:53:01
0
3
608

学校的操作系统课程设计, Ubuntu14.04升级新内核并从新内核启动(4.2.6stable)
后来要求编写一个字符设备驱动, 代码里面

#include<sys/stat.h>
#include<sys/types.h>
. . . 

结果make的时候提示找不到这些头, 我的makefile里面定义的KDIR是/lib/include/4.2.6/build

驱动代码和makefile都是参照网上的例程的, 以前没有内核开发经验

我自己尝试find types.h头, 后来发现它的上一级目录并不是sys, 有人能详细解说一下么
包括-generic源码里面也没有这样的目录结构

编译完新内核相应的内核树不是也已经建立好了么, 为什么目录结构不一样呢

我的makefile(参照《Linux设备驱动》里面的)

# If KERNELRELEASE is defined, we've been invoked from the
# kernel build system and can use its language.
ifneq ($(KERNELRELEASE),)
    obj-m := char_dev.o
# Otherwise we were called directly from the command
# line; invoke the kernel build system.
else
    #INCLUDE ?= /usr/src/linux-4.2.6/include/linux
    KERNELDIR ?= /lib/modules/3.13.0-24-generic/build
    #/usr/include/x86_64-linux-gnu/
    #/usr/src/linux-4.2.6/include/linux
    PWD := $(shell pwd)
default:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) modules

endif

包含的几个头如下:

#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>


#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <asm/uaccess.h>
#include <linux/wait.h>
#include <linux/semaphore.h>
ringa_lee
ringa_lee

ringa_lee

reply all(3)
Ty80

Your description may be more detailed so that more people can diagnose your problem.

To compile the kernel on Ubuntu, it is recommended to use the following two simple methods:
The first one: download the source code from kernel.org, but use Ubuntu’s related toolkits to compile: you can refer to the link: https:// wiki.ubuntu.com/KernelTeam/GitKernelBuild

The second type: Use Ubuntu’s official packaged kernel source code and toolkit. It is based on the kernel.org source code, with patches and various Ubuntu own package configurations added. You can refer to the link: https:// wiki.ubuntu.com/Kernel/BuildYourOwnKernel

Using the above method, you can simply apply the newly compiled kernel to the system, and also help you install the kernel header files. In /usr/src/linux-headers-xxx, you can directly boot into the system with the new kernel. , you can start driver development, and the header file will be found automatically.

The method mentioned in the book does not necessarily use the above method. The methods introduced in the book are usually native methods. Generally, the source code on kernel.org is used directly and the manual operation is performed according to the method introduced in the kernel.org document. However, please note that you can download the kernel source code to any local location by yourself. Compile, but remember to install the kernel header file into the system directory. You can use the command:

make headers_install

Generally, it will be installed under /usr/src/, and then switched to the new kernel system, and driver development can be carried out directly without configuring any header file path.

So you need to make sure that your current system has the kernel header files installed. If you don't understand this, I suggest you use another simple method: the complete Linux kernel code already includes driver compilation. Replace the file in the driver directory corresponding to the source code with your own driver code and compile it directly. Modules can also be compiled, and the effect is the same. You can also add new driver files and slightly modify the relevant MakeFile to achieve the goal.

Peter_Zhu

The address of the header file required by the poster is /usr/include/sys/
If it is the header file required by the driver, /usr/src/linux-headers-xxxxx/include/linux/ (xxxx is your Kernel version number )

洪涛

Does the poster know the directory of the header file? To make it easier, you can specify the search directory directly during compilation -I

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!