Linkers, Loaders & Executables
Linking a C program gives "undefined reference to `foo'". What is the most likely cause?
- AThere is a grammar error inside foo
- Bfoo is defined in two files
- 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.