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.cfile from that kernel version, you’ll notice the twoirq_to_descimplementations are wrapped in mutually exclusive preprocessor macros (like#ifdef/#elseblocks tied to kernel configuration options, e.g.,CONFIG_SMPor architecture-specific flags). - During the kernel build process, the preprocessor first evaluates these macros based on the active
.configfile. 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
相关产品推荐
相关产品推荐

