Let me be blunt: If you haven’t yet seen an undefined reference error, you truly aren’t a C programmer. In fact, the more of these messages you see, the longer you’ve been coding in C. Undefined reference errors are a badge of honor.
The short explanation of an undefined reference error is that the code contains a fictitious function. It could be a non-existent function, a typo, or it could be a function that requires a library that wasn’t linked in. Other mayhem ensues with this type of error as they often generate compiler warnings as a byproduct. Consider the following code:
2025_08_30-Lesson.c
#include <stdio.h>
int main()
{
char string[] = "Judge me by my size, will you?";
int len;
len = size(string);
printf("The string \"%s\" is %d characters long.\n",
string,
len
);
return 0;
}
This code contains the size() function, which has no definition. When built, the code generates a warning from the compiler:
0830.c:19:8: warning: implicit declaration of function 'size' is invalid in C99 [-Wimplicit-function-declaration]
len = size(string);
^
1 warning generated.
This warning indicates a missing prototype; the compiler cannot confirm that the size() function is of the correct data type or that the arguments are properly typed. It assumes that everything is an integer, which is why the code compiles.
Next comes the undefined reference error, generated by the linker:
/usr/bin/ld: /tmp/0830-3db658.o: in function `main':
0830.c:(.text+0x54): undefined reference to `size'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
This message is an error, meaning that though a program wasn’t built. The undefined reference is to “size,” a function that doesn’t exist.
At this point, you need to address both the warning and the error. For the warning, the function must be prototyped: Add the proper header file or write the prototype directly. For example, if the size() function is present in the source code file after the main() function, add its protype before the function is called:
int size(char *);
This step addresses the compiler warning.
The second step is to tell the linker where to find the function. Or, when the compiler doesn’t generate a warning, and all you see is the linker’s undefined reference error, you must link in the proper library to make the function “funct.” I encounter this problem often in Linux where the math library must be linked in specifically. (This problem doesn’t exist in Windows.)
Beyond forgetting to link in a library, undefined reference errors are generated by typos. In this case you see both the warning and error: The compiler doesn’t recognize the typo and neither does the linker. This happens often with me when I type print() instead of printf().
For multi-module programs, I set local function prototypes in a project header file, which is included for all source code files. Doing so ensures that the functions are recognized in whatever module they’re called.
Undefined reference errors are common, but easily fixed. Don’t let them annoy you. Instead, use the references provided in the warning/error messages, fix the problem, then build your program. And wear this error as a badge of honor!