{"id":1607,"date":"2022-08-30T15:17:56","date_gmt":"2022-08-30T15:17:56","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/27\/why-am-i-getting-an-undefined-reference-error-in-c-for-my-implementation-of-a-singly-linked-list-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:17:56","modified_gmt":"2022-08-30T15:17:56","slug":"why-am-i-getting-an-undefined-reference-error-in-c-for-my-implementation-of-a-singly-linked-list-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/why-am-i-getting-an-undefined-reference-error-in-c-for-my-implementation-of-a-singly-linked-list-collection-of-common-programming-errors\/","title":{"rendered":"why am I getting an undefined reference error in C for my implementation of a singly linked list-Collection of common programming errors"},"content":{"rendered":"<p>I am getting an error that reads like this<\/p>\n<pre><code>$ gcc -o app llistdriver.c llistlib.c -Wall\n llistdriver.c:7: warning: return type defaults to `int'\n llistdriver.c: In function `printinsts':\n llistdriver.c:15: warning: control reaches end of non-void function\n llistlib.c: In function `insert':\n llistlib.c:25: warning: statement with no effect\n llistlib.c:41: warning: control reaches end of non-void function\n \/tmp\/ccqYmK3M.o:llistdriver.c:(.text+0x157): undefined reference to `_search'\n collect2: ld returned 1 exit status\n<\/code><\/pre>\n<p>and I cannot figure out why I am getting this error message have written a library file that contains the methods for the linked list.<\/p>\n<pre><code>  int search( struct node *front, int val)\/\/ \n {\n     while(front != NULL)\n    {\n       if( front-&gt;modmark == val ) \/\/this is to find the value \n       return 1;  \/\/ found it\n\n    front=front-&gt;next;\n    }\n    return 0;  \/\/ Not found\n  }\n<\/code><\/pre>\n<p>I assume that it is complaining about the only reference in the main file<\/p>\n<pre><code>        case 5: \n                printf(\"please enter a search value?\\n\");\n                scanf(\"%i\",&amp;mark);\n                choice = search(first,mark);\/\/here\n                if (choice == -1)\n                {\n\n                    printf(\"the modmark was not found\\n\");\n                }\n                else \n                {   \n                    printf(\"the first occurence of the mark is at the %i th index of the list\",choice); \n                };\n                    break;\n<\/code><\/pre>\n<p>and first and mark are variables that are defined like this<\/p>\n<pre><code>                 struct node *first;\n              int mark = 0;\n<\/code><\/pre>\n<p>any help on this would be much appreciated I have spent hours trying to debug this error. Writing C code is of the the banes of my life at the moment.<\/p>\n<p>Ps I have checked all the #include &#8220;llistlib.h&#8221; there are included in the llistlib.c file and the llistdriver.c file.<\/p>\n<p>Ok maybe this will make it much clearer<\/p>\n<p>\/\/ llistlib.c &#8211; the library which implements the functions defined in the listlib.h file #include #include #include &#8220;llistlib.h&#8221;<\/p>\n<pre><code>void initialise(struct node **ps)\n{\n\nprintf(\"initialise function\\n\");\n\n*ps = NULL;   \/\/initialise list to empty\n\n}\n\n\nint insert(struct node **fn)\n{\nprintf(\"insert function\\n\");\nstruct node *newnode;\nnewnode =  (struct node*) malloc(sizeof (struct node)); \/\/make new node and give it memory\nif (newnode == NULL) \/\/if fsiled return to the program saying it failed\n    return -1;\nif (*fn == NULL) \/\/ for the first of the list\n{\n    newnode -&gt; next == NULL; \/\/ sets the next value to nothing\n    printf(\"what mark do you want to enter\\n\");\n    scanf(\"%i\",&amp;newnode -&gt; modmark); \/\/ this gets the modmark\n    *fn = newnode; \/\/the head of the list is set to the new node\n}\nelse\n{\n    newnode -&gt; next = *fn; \/\/ sets the newnodes next value to the head node\n    printf(\"what mark do you want to enter\\n\");\n    scanf(\"%i\",&amp;newnode -&gt; modmark); \/\/ gets the modmark\n    *fn = newnode; \/\/ the head becomes the new node\n\n\n    printf(\"%p %i\",newnode,newnode-&gt;modmark); \/\/ just for debugging\n\n}\n }\n\n\n\nvoid traverse(struct node *ps)\n{   \n\nif (ps != NULL) \/\/ if the current node is null \n{\n    printf(\"%p - %i\\n\",ps,ps -&gt; modmark);\n    traverse(ps -&gt; next); \n\n}\nelse\n{\n    printf(\"your list is empty\\n\");\n}\n}\n\n\nvoid delete(struct node **dn)\n{\nprintf(\"delete function\\n\");\nstruct node *delnode;\ndelnode = *dn; \/\/sets the head node so that it can be deleted later\nif (delnode != NULL)\n{\n    *dn = delnode -&gt; next; \/\/ sets the head to the next node\n    free(delnode); \/\/deletes the previous head node\n}\n\n}\n\n\nvoid finish(struct node **ps) \/\/this function goes through the list and removes all items\n{\nprintf(\"finish function\\n\");\nstruct node *finishnode;\nfinishnode = *ps;\nwhile (finishnode != NULL)\n{\n    *ps = finishnode -&gt; next;\n    free(finishnode);\n    finishnode = *ps;\n}\n\nint search(struct node *front,int val)\n{\n while(front != NULL)\n {\n    if( front-&gt;modmark == val ) \/\/ Assuming struct node has val member which you are trying to compare to\n       return 1;  \/\/ found it\n\n    front=front-&gt;next;\n }\n  return 0;  \/\/ Not found\n}\n\n}\n<\/code><\/pre>\n<p>and then the llistdriver.c file is<\/p>\n<pre><code>#include \n#include \n#include \"llistlib.h\"\n\n\nprintinsts()\n{\nprintf(\"Enter:- \\n\");\nprintf(\"\\t0 to exit\\n\");\nprintf(\"\\t1 to initialise list\\n\");\nprintf(\"\\t2 to insert item at front of list\\n\");\nprintf(\"\\t3 to delete item from front of list\\n\");\nprintf(\"\\t4 to traverse list\\n\");\nprintf(\"\\t5 to search through the list\\n\");\n}\n\n\n\n\nint main()\n{\nint choice;\nstruct node *first;\nint mark = 0;\n\nprintinsts();\nscanf(\"%d\",&amp;choice);\n\nwhile (1)\n{\n    switch (choice)\n    {\n        case 0 : finish(&amp;first);\n                    exit(0);\n                    break;\n\n        case 1 : initialise(&amp;first);\n                    break;\n\n        case 2 : if (insert(&amp;first)== -1)\n                    {\n                        printf(\"insert not successful\");\n                        choice = 0;\n                     };\n                    break;\n\n        case 3 : delete(&amp;first);\n                    break;\n\n        case 4 : traverse(first);\n                     break;\n        case 5: \n                printf(\"please enter a search value?\\n\");\n                scanf(\"%i\",&amp;mark);\n                choice = search(first,mark);\n                if (choice == -1)\n                {\n\n                    printf(\"the modmark was not found\\n\");\n                }\n                else \n                {   \n                    printf(\"the first occurence of the mark is at the %i th index of the list\",choice); \n                };\n                    break;\n\n        default : printf(\"Invalid choice of %d \\n\", choice);\n\n    };  \/\/ end of switch\n\n    printinsts();\n    scanf(\"%d\", &amp;choice);\n\n}  \/\/ end of while\n\n}\n<\/code><\/pre>\n<p>and the header file llistlib.h reads as<\/p>\n<pre><code>struct node {\nint     modmark;\nstruct node *next;\n };\n\n\nvoid initialise(struct node **ps);\n\n\nint insert(struct node **fn);\n\n\nvoid traverse(struct node *ps);\n\n\nvoid delete(struct node **dn);\n\n\nvoid finish(struct node **ps);\n\n\nint search(struct node *front,int val);\n<\/code><\/pre>\n<p>I havent been able to spot a obvious problem among any of the code yet<\/p>\n<ol>\n<li>\n<p>Your <code>finish<\/code> function is missing a closing brace {. And <code>search<\/code> has an extra closing brace.<\/p>\n<\/li>\n<li>\n<p>Just include <code>llistlib.h<\/code> in your <code>llistlib.c<\/code> file as follows:<\/p>\n<p><code>#include \"llistlib.h\"<\/code><\/p>\n<p>and use folloeing command for compilation:<\/p>\n<p><code>gcc -o app llistdriver.c llistlib.c<\/code><\/p>\n<p>Hope this helps&#8230;<\/p>\n<\/li>\n<li>\n<p>First error:<\/p>\n<pre><code> llistdriver.c:7: warning: return type defaults to `int'\n llistdriver.c: In function `printinsts':\n llistdriver.c:15: warning: control reaches end of non-void function\n<\/code><\/pre>\n<p>The function looks like:<\/p>\n<pre><code>printinsts() {}\n<\/code><\/pre>\n<p>Please give this function a return type, and if it&#8217;s not void, return it.<\/p>\n<p>Secondly:<\/p>\n<pre><code>llistlib.c: In function `insert':\nllistlib.c:25: warning: statement with no effect\n<\/code><\/pre>\n<p>That line is:<\/p>\n<pre><code>newnode -&gt; next == NULL; \/\/ sets the next value to nothing\n<\/code><\/pre>\n<p>The comment is not true, this line does not set anything, as it uses the equality operator, not the assignment operator.<\/p>\n<p>(it&#8217;s partly a matter of taste, but I don&#8217;t like comments like these, which simply try to state the same thing as the code itself. In cases like these, they become misleading, as you may read, and believe, the comment, rather than parsing the code when looking for a bug.)<\/p>\n<p>Then:<\/p>\n<pre><code>llistlib.c:41: warning: control reaches end of non-void function\n<\/code><\/pre>\n<p>Which again refers to:<\/p>\n<pre><code>int insert(struct node **fn)\n{\nprintf(\"insert function\\n\");\nstruct node *newnode;\nnewnode =  (struct node*) malloc(sizeof (struct node)); \/\/make new node and give it memory\nif (newnode == NULL) \/\/if fsiled return to the program saying it failed\n    return -1;\n...\n}\n<\/code><\/pre>\n<p>There are no returns from this function other than that -1. It should return something no matter what happens.<\/p>\n<p>Lastly:<\/p>\n<pre><code>\/tmp\/ccqYmK3M.o:llistdriver.c:(.text+0x157): undefined reference to `_search'\n<\/code><\/pre>\n<p>Because you missed a closing brace from the end of finish();<\/p>\n<p>Do NOT ignore warnings just because your code compiles. Fixing just the errors is not enough!<\/p>\n<\/li>\n<li>\n<p>You are compiling with the default GCC setting, which is &#8220;roughly follow the C99 standard, then add non-standard GNU goo&#8221;. The C99 standard, as well as the current C standard, do not allow functions to have a default type. That is why you get a warning with -Wall, the return type must be explicit.<\/p>\n<p><code>printinsts() {}<\/code> is not valid C. You should declare\/define this as <code>void printinsts (void)<\/code> and nothing else.<\/p>\n<p>In the future, make sure to always compile with -std=c99 or -std=c11, to ensure that you are compiling the code as standard C language and not something else.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-27 12:01:31. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I am getting an error that reads like this $ gcc -o app llistdriver.c llistlib.c -Wall llistdriver.c:7: warning: return type defaults to `int&#8217; llistdriver.c: In function `printinsts&#8217;: llistdriver.c:15: warning: control reaches end of non-void function llistlib.c: In function `insert&#8217;: llistlib.c:25: warning: statement with no effect llistlib.c:41: warning: control reaches end of non-void function \/tmp\/ccqYmK3M.o:llistdriver.c:(.text+0x157): undefined [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1607","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1607","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1607"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1607\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1607"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1607"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1607"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}