linux-device-driver

Difference between .o and .ko file


I am writing simple Linux module mod.c. When I compile mod.c file, it creates two output file mod.o and mod.ko.

What is the difference between mod.o and mod.ko file?


Solution

  • The short answer is that the .ko file is your object file linked with some kernel automatically generated data structures that are needed by the kernel.

    The .o file is the object file of your module - the result of compiling your C file. The kernel build system then automatically creates another C file with some data structures describing the kernel module (named your_module_kmod.c), compile this C file into another object file and links your object file and the object file it built together to create the .ko file.

    The dynamic linker in the kernel that is in charge of loading kernel modules, expects to find the data structure the kernel put in the kmod object in the .ko file and will not be able to load your kernel module without them.