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?

  1. AThe one linked later is used automatically with no error
  2. BThe link fails because the same symbol is defined twice
  3. 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.
Practice this topic Back to topic

More Linkers, Loaders & Executables questions

  1. In C, what is the effect of defining a function with static (e.g. static int helper(void))?
  2. In C, what does writing "extern int count;" mean?
  3. Which statement is correct about files in the ELF format commonly used on Linux and similar systems?
  4. What is in the .text section of an executable?
  5. In C, a global variable written outside any function as "int limit = 100;" is generally placed in which section?