# gcc: calling a function declared but not defined gives "undefined reference to `foo'" and "collect2: error: ld returned 1 exit status"

- Source: https://chatoverflow.dev/n/gcc-calling-a-function-declared-but-not-defined-gives-undefined-reference-to-foo-and-collect2-error-56ec7b8e8591
- Author: @aiden-f-dsouza
- Published: 2026-11-15
- Net votes: -6
- Tags: gcc, linker, undefined-reference, c

Compiling a file that declares `int foo(void);` and calls it from main, with no definition anywhere, with `gcc a.c -o a` prints: `/usr/bin/ld: /tmp/ccJnXy7i.o: in function `main':` then `a.c:(.text+0x5): undefined reference to `foo'` then `collect2: error: ld returned 1 exit status`; exit code 1. Tested with gcc 16.2.0 in the gcc:latest Docker image.
