简体   繁体   English

linux内核模块编译先决条件

[英]linux kernel module compilation pre-requisites

I've just started to learn about linux kernel modules and the book I'm referring to says: 我刚刚开始学习Linux内核模块,而我所指的书说:

"For this [compilation] to work, the kernel source has to be suitably prepared; in particular it has to have a configuration file (.config in the main kernel source directory) and proper dependencies setup" “要使此[编译]起作用,必须适当准备内核源;特别是它必须具有配置文件(主内核源目录中的.config)和适当的依赖项设置”

However, as far as I know (and have tried), the .config file is generated by the make menuconfig (or any of the equivalent make config commands) - and that doesn't seem to be enough for my module files to compile. 但是,据我所知(并且已经尝试过),. config文件是由make menuconfig (或任何等效的make config命令)生成的-似乎还不足以编译我的模块文件。 What's the bare minimum I need to do in the kernel source directory? 我需要在内核源目录中执行的最低要求是什么?

make modules ? make modules

Yes, the .config file is generated using make *config . 是的,.config文件是使用make *config生成的。

Here are some of them: 这里是其中的一些:

make defconfig creates the default configuration for your architecture. make defconfig为您的体系结构创建默认配置。

make config is the most primitive method, it prompts on every configuration. make config是最原始的方法,它会在每个配置上提示。

make menuconfig is ncurses config menu. make menuconfig是ncurses配置菜单。 That's the one I prefer if I'm not editing .config file directly. 如果我不直接编辑.config文件,那是我更喜欢的一种。

make gconfig is like menuconfig, but using gtk+. make gconfig类似于menuconfig,但使用gtk +。

Don't forget that make oldconfig should be called after modifying the .config file yourself. 不要忘记在自己修改.config文件之后应调用make oldconfig

Your current config might also be stored somewhere on your disk. 您当前的配置也可能存储在磁盘上的某个位置。 For many linux versions, it's location is /boot/config-$(uname -r) If it exists, you can start with it. 对于许多Linux版本,它的位置是/boot/config-$(uname -r)如果存在),可以从其开始。 If not, your best bet is make defconfig , then editing the config file to suit your needs. 如果不是,最好的选择是make defconfig ,然后编辑配置文件以适合您的需求。

After configuration: 配置后:

Before building modules, you might want to compile the kernel since your modules will not be used by the current kernel and even if you make your current kernel use those modules, it'll most probably cause a panic since symbol tables will not be in the order that your compiled modules assumes. 在构建模块之前,您可能要编译内核,因为当前内核将不会使用您的模块,即使您使当前内核使用这些模块,也很可能会引起恐慌,因为符号表将不在编译模块假定的顺序。 make -jN is the most used method for compiling, N being twice your CPU core count. make -jN是最常用的编译方法,N是CPU内核数的两倍。 This also compiles modules, but creates .ko files for them, instead of embedding into the vmlinuz file. 这也会编译模块,但是会为它们创建.ko文件,而不是嵌入到vmlinuz文件中。

After that, you can sudo make install to install your kernel. 之后,您可以sudo make install来安装内核。 This usually wraps the kernel object you've just compiled into a suitable format and puts under /boot (it doesn't have to be /boot, actually). 通常,这会将您刚刚编译的内核对象包装为合适的格式,并将其放在/boot (实际上,它不一定是/ boot)。

Then you sudo make modules_install to copy the created .ko files into /lib/modules/$(uname -r) . 然后,您sudo make modules_install将创建的.ko文件复制到/lib/modules/$(uname -r) This builds all modules. 这将构建所有模块。

After doing that, you might prefer only building your own module, instead of all of them. 完成此操作后,您可能更喜欢仅构建自己的模块,而不是所有模块。 When on the kernel tree root, you may make M=your_modules_relative_path to only build your module. 在内核树根目录上时,可以make M=your_modules_relative_path仅构建模块。

I don't know which book you're reading, but if you're building a module externally, you still have to perform the work above. 我不知道您正在阅读哪本书,但是如果您在外部构建模块,则仍然必须执行上述工作。 After that, you may use LDD examples as a starting point for your makefiles. 之后,您可以使用LDD示例作为makefile的起点。

See https://github.com/duxing2007/ldd3-examples-3.x 参见https://github.com/duxing2007/ldd3-examples-3.x

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM