{"id":7129,"date":"2025-08-30T00:01:40","date_gmt":"2025-08-30T07:01:40","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=7129"},"modified":"2025-08-23T12:13:57","modified_gmt":"2025-08-23T19:13:57","slug":"the-story-of-the-undefined-reference-error","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=7129","title":{"rendered":"The Story of the Undefined Reference Error"},"content":{"rendered":"<p>Let me be blunt: If you haven&#8217;t yet seen an undefined reference error, you truly aren&#8217;t a C programmer. In fact, the more of these messages you see, the longer you&#8217;ve been coding in C. Undefined reference errors are a badge of honor.<br \/>\n<!--more--><br \/>\nThe 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&#8217;t linked in. Other mayhem ensues with this type of error as they often generate compiler warnings as a byproduct. Consider the following code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_08_30-Lesson.c\" rel=\"noopener\" target=\"_blank\">2025_08_30-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    char string[] = \"Judge me by my size, will you?\";\r\n    int len;\r\n\r\n    len = size(string);\r\n    printf(\"The string \\\"%s\\\" is %d characters long.\\n\",\r\n            string,\r\n            len\r\n          );\r\n\r\n    return 0;\r\n}<\/pre>\n<p>This code contains the <em>size()<\/em> function, which has no definition. When built, the code generates a warning from the compiler:<\/p>\n<p><code>0830.c:19:8: warning: implicit declaration of function 'size' is invalid in C99 [-Wimplicit-function-declaration]<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;len = size(string);<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;^<br \/>\n1 warning generated.<\/code><\/p>\n<p>This warning indicates a missing prototype; the compiler cannot confirm that the <em>size()<\/em> 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.<\/p>\n<p>Next comes the undefined reference error, generated by the linker:<\/p>\n<p><code>\/usr\/bin\/ld: \/tmp\/0830-3db658.o: in function `main':<br \/>\n0830.c:(.text+0x54): undefined reference to `size'<br \/>\nclang: error: linker command failed with exit code 1 (use -v to see invocation)<\/code><\/p>\n<p>This message is an error, meaning that though a program wasn&#8217;t built. The undefined reference is to &#8220;size,&#8221; a function that doesn&#8217;t exist.<\/p>\n<p>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 <em>size()<\/em> function is present in the source code file after the <em>main()<\/em> function, add its protype before the function is called:<\/p>\n<p><code>int size(char *);<\/code><\/p>\n<p>This step addresses the compiler warning.<\/p>\n<p>The second step is to tell the linker where to find the function. Or, when the compiler doesn&#8217;t generate a warning, and all you see is the linker&#8217;s undefined reference error, you must link in the proper library to make the function &#8220;funct.&#8221; I encounter this problem often in Linux where the math library must be linked in specifically. (This problem doesn&#8217;t exist in Windows.)<\/p>\n<p>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&#8217;t recognize the typo and neither does the linker. This happens often with me when I type <em>print()<\/em> instead of <em>printf(<\/em>).<\/p>\n<p>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&#8217;re called.<\/p>\n<p>Undefined reference errors are common, but easily fixed. Don&#8217;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!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s one of the most common and unpopular errors in C programming! <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7129\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-7129","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7129","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7129"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7129\/revisions"}],"predecessor-version":[{"id":7150,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7129\/revisions\/7150"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7129"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7129"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}