You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Linux内核2.6.34:handle.c中重复的irq_to_desc函数为何能编译通过?

Why does kernel/irq/handle.c in Linux 2.6.34 compile with duplicate irq_to_desc function definitions?

Great question! This might look like a violation of C's one-definition rule at first glance, but the key here lies in preprocessor conditional compilation—the two identical function definitions are never actually presented to the compiler at the same time.

Let’s break it down:

  • If you look closely at the handle.c file from that kernel version, you’ll notice the two irq_to_desc implementations are wrapped in mutually exclusive preprocessor macros (like #ifdef/#else blocks tied to kernel configuration options, e.g., CONFIG_SMP or architecture-specific flags).
  • During the kernel build process, the preprocessor first evaluates these macros based on the active .config file. It will discard one of the two function definitions entirely, leaving only a single implementation for the compiler to process.
  • The reason the two functions are identical is likely a leftover from code evolution: originally, the branches might have had different logic, but over time the implementations were unified while keeping the conditional compilation structure intact.

To put it in concrete terms, the code structure looks something like this (simplified):

#ifdef CONFIG_SOME_CONFIG_OPTION
struct irq_desc *irq_to_desc(unsigned int irq) {
    return radix_tree_lookup(&irq_desc_tree, irq);
}
#else
struct irq_desc *irq_to_desc(unsigned int irq) {
    return radix_tree_lookup(&irq_desc_tree, irq);
}
#endif

Since only one of these blocks makes it past the preprocessor, the compiler never sees a duplicate definition—so no compilation error occurs. This pattern is common in the Linux kernel to handle varying hardware configurations, feature sets, or compatibility requirements.

内容的提问来源于stack exchange,提问作者Antiver

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:17:12