<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/how-to-write-a-linux-kernel-module-that-actually-builds-zoyt3jduk" -->

---
title: How to Write a Linux Kernel Module That Actually Builds
description: A minimal 22-line Linux kernel module is used to walk through everything the build actually produces beyond the source code: object files, MODPOST symbol...
canonical: https://daily.dev/posts/how-to-write-a-linux-kernel-module-that-actually-builds-zoyt3jduk
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: How to Write a Linux Kernel Module That Actually Builds | daily.dev
og:description: A minimal 22-line Linux kernel module is used to walk through everything the build actually produces beyond the source code: object files, MODPOST symbol...
og:url: https://daily.dev/posts/how-to-write-a-linux-kernel-module-that-actually-builds-zoyt3jduk
og:image: https://api.daily.dev/og/posts/ZoYT3jdUk.png
og:image:alt: How to Write a Linux Kernel Module That Actually Builds
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# How to Write a Linux Kernel Module That Actually Builds

**[freeCodeCamp](https://daily.dev/sources/freecodecamp)** · 16 min read · 0 upvotes · 0 comments

## Summary

A minimal 22-line Linux kernel module is used to walk through everything the build actually produces beyond the source code: object files, MODPOST symbol checks, the .ko ELF format, debug information bloat (106KB vs 4.8KB stripped), and undefined symbols like __fentry__ and __x86_return_thunk that every module picks up automatically. It covers vermagic version matching and why modules must be rebuilt per kernel (and why DKMS exists), module parameters via module_param, Secure Boot and kernel lockdown restrictions, kernel taint flags, and the four most common build/load errors with their causes. It also explains why older kernel module tutorials (using SUBDIRS=, init_module/cleanup_module, or optional MODULE_LICENSE) no longer compile on current kernels.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.freecodecamp.org/news/how-to-write-a-linux-kernel-module-that-actually-builds>

## Questions this post answers

### why does my Linux kernel module fail to load with 'Invalid module format'

This error means the module's vermagic string does not match the running kernel. Vermagic covers the kernel release, whether it is SMP, whether module unloading is compiled in, and whether symbol versioning is enabled; a mismatch on any of these causes the kernel to refuse loading. Compare modinfo ./module.ko | grep vermagic against uname -r, then rebuild against the matching kernel headers to fix it.

_daily.dev surfaces kernel internals like this for developers chasing module load failures._

### why is my Linux kernel module so much larger than expected in file size

A minimal kernel module compiled with default settings can be around 106,000 bytes even though only about 4,864 bytes is actual code; the rest is DWARF debug information used by tools like crash and gdb for post-panic analysis. Stripping debug info with strip --strip-debug removes it, and since the kernel does not load debug sections at insmod time, the runtime memory cost matches the smaller stripped size, not the on-disk size.

_understanding build output size helps developers debugging kernel module memory footprint, a topic covered on daily.dev._

### why does an old Linux kernel module tutorial from years ago fail to compile today

Several conventions changed: printk(KERN_INFO ...) still works but pr_info is now standard, init_module/cleanup_module as bare function names were replaced by module_init/module_exit, MODULE_LICENSE went from optional to load-bearing since it gates GPL-only exported symbols, the Makefile argument SUBDIRS= was renamed to M= and the old spelling was removed entirely, and header paths moved away from /usr/src/linux to per-kernel headers packages.

_developers modernizing legacy kernel module code can track these breaking build changes on daily.dev._

## Similar posts on daily.dev

- [The Linux kernel is just a program](https://daily.dev/posts/the-linux-kernel-is-just-a-program-stujpves5) · Hacker News · 6 upvotes · 0 comments
- [Linux 6.19 Merges "klp-build" As New Livepatch Module Generation Solution](https://daily.dev/posts/linux-6-19-merges-klp-build-as-new-livepatch-module-generation-solution-qdjqvvznq) · Phoronix · 0 upvotes · 0 comments
- [rnhmjoj/modetc: Move your dotfiles from kernel space](https://daily.dev/posts/rnhmjoj-modetc-move-your-dotfiles-from-kernel-space-dzm9blhrh) · Lobsters · 0 upvotes · 0 comments

---

Tags: [#linux](https://daily.dev/tags/linux), [#c](https://daily.dev/tags/c)

[View this post on daily.dev](https://daily.dev/posts/how-to-write-a-linux-kernel-module-that-actually-builds-zoyt3jduk)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"How to Write a Linux Kernel Module That Actually Builds","url":"https://daily.dev/posts/how-to-write-a-linux-kernel-module-that-actually-builds-zoyt3jduk","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/how-to-write-a-linux-kernel-module-that-actually-builds-zoyt3jduk"},"datePublished":"2026-09-15T22:21:09.085Z","dateModified":"2026-09-15T22:21:34.223Z","description":"A minimal 22-line Linux kernel module is used to walk through everything the build actually produces beyond the source code: object files, MODPOST symbol...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/08909804bd70ca0fc651fec21aac083d?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/08909804bd70ca0fc651fec21aac083d?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"freeCodeCamp","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"freeCodeCamp","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1628412854/logos/freecodecamp","url":"https://daily.dev/sources/freecodecamp"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/how-to-write-a-linux-kernel-module-that-actually-builds-zoyt3jduk","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"linux,c","timeRequired":"PT16M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"freeCodeCamp","item":"https://daily.dev/sources/freecodecamp"},{"@type":"ListItem","position":3,"name":"How to Write a Linux Kernel Module That Actually Builds"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/how-to-write-a-linux-kernel-module-that-actually-builds-zoyt3jduk#faq","mainEntity":[{"@type":"Question","name":"why does my Linux kernel module fail to load with 'Invalid module format'","acceptedAnswer":{"@type":"Answer","text":"This error means the module's vermagic string does not match the running kernel. Vermagic covers the kernel release, whether it is SMP, whether module unloading is compiled in, and whether symbol versioning is enabled; a mismatch on any of these causes the kernel to refuse loading. Compare modinfo ./module.ko | grep vermagic against uname -r, then rebuild against the matching kernel headers to fix it. daily.dev surfaces kernel internals like this for developers chasing module load failures."}},{"@type":"Question","name":"why is my Linux kernel module so much larger than expected in file size","acceptedAnswer":{"@type":"Answer","text":"A minimal kernel module compiled with default settings can be around 106,000 bytes even though only about 4,864 bytes is actual code; the rest is DWARF debug information used by tools like crash and gdb for post-panic analysis. Stripping debug info with strip --strip-debug removes it, and since the kernel does not load debug sections at insmod time, the runtime memory cost matches the smaller stripped size, not the on-disk size. understanding build output size helps developers debugging kernel module memory footprint, a topic covered on daily.dev."}},{"@type":"Question","name":"why does an old Linux kernel module tutorial from years ago fail to compile today","acceptedAnswer":{"@type":"Answer","text":"Several conventions changed: printk(KERN_INFO ...) still works but pr_info is now standard, init_module/cleanup_module as bare function names were replaced by module_init/module_exit, MODULE_LICENSE went from optional to load-bearing since it gates GPL-only exported symbols, the Makefile argument SUBDIRS= was renamed to M= and the old spelling was removed entirely, and header paths moved away from /usr/src/linux to per-kernel headers packages. developers modernizing legacy kernel module code can track these breaking build changes on daily.dev."}}]}
```

