Linkers, Loaders & Executables

Linking a C program gives "undefined reference to `foo'". What is the most likely cause?

  1. AThere is a grammar error inside foo
  2. Bfoo is defined in two files
  3. Cfoo is called, but the file or library that defines foo is not included in the link
Show answer and explanation

Answer: C. foo is called, but the file or library that defines foo is not included in the link

If the function is declared in a header, compilation succeeds. But at link time, if no object file or library defining foo is found, the address for the call cannot be determined, and this error appears. Common causes are forgetting to link the defining file, forgetting a library option (such as -lm), or a spelling or signature mismatch that produces a different name.

  • A. A grammar error would stop compilation earlier.
  • B. Two definitions give a multiple-definition error.
  • C. Correct. There is a declaration but no definition.
Practice this topic Back to topic

More Linkers, Loaders & Executables questions

  1. 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?
  2. In C, what is the effect of defining a function with static (e.g. static int helper(void))?
  3. In C, what does writing "extern int count;" mean?
  4. Which statement is correct about files in the ELF format commonly used on Linux and similar systems?
  5. What is in the .text section of an executable?