Fundamentals 4 min read

Why the Linux Kernel Discourages Using typedef

The article explains the Linux kernel's warning against new typedefs, citing Linus Torvalds' preference for struct usage, coding style guidelines, and practical reasons such as readability and maintainability in a large codebase.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Why the Linux Kernel Discourages Using typedef

When developing Linux drivers, developers may encounter a warning: WARNING: do not add new typedefs. Although only a warning, it becomes a required fix for contributions to the kernel repository.

1. Linus Torvalds' Attitude

Linus Torvalds has expressed that he does not want a "typedef culture" to dominate the kernel, but he does not forbid occasional typedefs. He prefers using struct mystruct because it makes the actual type clearer, though it is not mandatory.

Torvalds recommends the struct mystruct style.

Typedefs can obscure the real type, making code harder to understand.

Given the kernel's massive architecture, typedefs defined for one architecture may not be portable to others.

Original source: https://lkml.indiana.edu/hypermail/linux/kernel/0206.1/0402.html

2. Kernel Coding Style Guidelines

The kernel coding style outlines specific situations where typedefs are acceptable:

Completely opaque objects to hide internal implementation.

Explicit integer types (e.g., u8, u16, u32) to avoid confusion between int and long.

Creating new types that match standard C99 types in special cases.

Types intended for use in user space.

Full guidelines: https://www.kernel.org/doc/html/v4.10/process/coding-style.html

3. Personal Viewpoint

For large projects like the Linux kernel, avoiding excessive typedefs helps prevent type proliferation and improves code readability. In small personal projects, developers can use typedefs at will, as the impact on maintainability is minimal.

Community discussions:

Why use typedef frequently in C? https://stackoverflow.com/questions/252780/why-should-we-typedef-a-struct-so-often-in-c

Why does the Linux kernel coding style discourage typedef? https://www.reddit.com/r/C_Programming/comments/dan8vr/why_does_the_linux_kernel_coding_style_guide/

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Linux kernelmaintainabilityC programmingcoding styletypedef
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.