Linkers, Loaders & Executables
Both a.c and b.c define a non-static global function void init(void) with the same name, and they are linked. What happens?
- AThe one linked later is used automatically with no error
- BThe link fails because the same symbol is defined twice
- CThe two functions are merged automatically and both bodies run in turn
Show answer and explanation
Answer: B. The link fails because the same symbol is defined twice
A non-static global function shares a single name across the whole program. With two definitions of the same name, the linker cannot decide which one a reference should bind to, so it reports a multiple-definition error. If a function is used only inside its file, making it static hides it from other files, and the same name elsewhere no longer collides.
- A. For ordinary (strong) symbols the linker does not pick one.
- B. Correct. It is a multiple-definition error.
- C. Functions are never merged this way.