为什么我在C中得到一个未定义的引用错误来实现单链表

为什么我在C中得到一个未定义的引用错误来实现单链表,c,compiler-errors,C,Compiler Errors,我得到了一个这样的错误 $ gcc -o app llistdriver.c llistlib.c -Wall llistdriver.c:7: warning: return type defaults to `int' llistdriver.c: In function `printinsts': llistdriver.c:15: warning: control reaches end of non-void function llistlib.c: In function `

我得到了一个这样的错误

$ gcc -o app llistdriver.c llistlib.c -Wall
 llistdriver.c:7: warning: return type defaults to `int'
 llistdriver.c: In function `printinsts':
 llistdriver.c:15: warning: control reaches end of non-void function
 llistlib.c: In function `insert':
 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 reference to `_search'
 collect2: ld returned 1 exit status
                 struct node *first;
              int mark = 0;
我不明白为什么我会收到这个错误消息,因为我写了一个包含链表方法的库文件

  int search( struct node *front, int val)// 
 {
     while(front != NULL)
    {
       if( front->modmark == val ) //this is to find the value 
       return 1;  // found it

    front=front->next;
    }
    return 0;  // Not found
  }
我假设它正在抱怨主文件中唯一的引用

        case 5: 
                printf("please enter a search value?\n");
                scanf("%i",&mark);
                choice = search(first,mark);//here
                if (choice == -1)
                {

                    printf("the modmark was not found\n");
                }
                else 
                {   
                    printf("the first occurence of the mark is at the %i th index of the list",choice); 
                };
                    break;
第一个和mark是这样定义的变量

$ gcc -o app llistdriver.c llistlib.c -Wall
 llistdriver.c:7: warning: return type defaults to `int'
 llistdriver.c: In function `printinsts':
 llistdriver.c:15: warning: control reaches end of non-void function
 llistlib.c: In function `insert':
 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 reference to `_search'
 collect2: ld returned 1 exit status
                 struct node *first;
              int mark = 0;
如果能在这方面提供任何帮助,我将不胜感激。我花了数小时试图调试这个错误。编写C代码是目前我生活中的一大祸害

Ps我已经检查了所有包含在llistlib.c文件和llistdriver.c文件中的#include“llistlib.h”

好吧,也许这会让事情变得更清楚

//c-实现listlib.h文件中定义的函数的库 #包括 #包括 #包括“llistlib.h”

然后,lsitdriver.c文件

#include <stdio.h>
#include <stdlib.h>
#include "llistlib.h"


printinsts()
{
printf("Enter:- \n");
printf("\t0 to exit\n");
printf("\t1 to initialise list\n");
printf("\t2 to insert item at front of list\n");
printf("\t3 to delete item from front of list\n");
printf("\t4 to traverse list\n");
printf("\t5 to search through the list\n");
}




int main()
{
int choice;
struct node *first;
int mark = 0;

printinsts();
scanf("%d",&choice);

while (1)
{
    switch (choice)
    {
        case 0 : finish(&first);
                    exit(0);
                    break;

        case 1 : initialise(&first);
                    break;

        case 2 : if (insert(&first)== -1)
                    {
                        printf("insert not successful");
                        choice = 0;
                     };
                    break;

        case 3 : delete(&first);
                    break;

        case 4 : traverse(first);
                     break;
        case 5: 
                printf("please enter a search value?\n");
                scanf("%i",&mark);
                choice = search(first,mark);
                if (choice == -1)
                {

                    printf("the modmark was not found\n");
                }
                else 
                {   
                    printf("the first occurence of the mark is at the %i th index of the list",choice); 
                };
                    break;

        default : printf("Invalid choice of %d \n", choice);

    };  // end of switch

    printinsts();
    scanf("%d", &choice);

}  // end of while

}

我还没有发现任何代码中存在明显的问题

只需在您的
llistlib.c
文件中包含
llistlib.h
,如下所示:

#包括“llistlib.h”

并使用以下命令进行编译:

gcc-o应用程序llistdriver.c llistlib.c


希望这有帮助…

您正在使用默认的GCC设置进行编译,即“大致遵循C99标准,然后添加非标准GNU-goo”。C99标准以及当前的C标准不允许函数具有默认类型。这就是为什么在-Wall中会出现警告,返回类型必须是显式的

printinsts(){}
不是有效的C。您应该声明/定义它为
void printinsts(void)
而不是其他内容


将来,请确保始终使用-std=c99或-std=c11进行编译,以确保您将代码编译为标准C语言,而不是其他语言。

您的
finish
函数缺少右大括号{。并且
search
有一个额外的右大括号。

第一个错误:

 llistdriver.c:7: warning: return type defaults to `int'
 llistdriver.c: In function `printinsts':
 llistdriver.c:15: warning: control reaches end of non-void function
该函数如下所示:

printinsts() {}
请给这个函数一个返回类型,如果它不是void,就返回它

第二:

llistlib.c: In function `insert':
llistlib.c:25: warning: statement with no effect
这条线是:

newnode -> next == NULL; // sets the next value to nothing
注释不正确,此行不设置任何内容,因为它使用相等运算符,而不是赋值运算符

(这在一定程度上是品味的问题,但我不喜欢这样的注释,它们只是试图说明与代码本身相同的事情。在这种情况下,它们会误导您,您可能会阅读并相信注释,而不是在查找bug时解析代码。)

然后:

这又是指:

int insert(struct node **fn)
{
printf("insert function\n");
struct node *newnode;
newnode =  (struct node*) malloc(sizeof (struct node)); //make new node and give it memory
if (newnode == NULL) //if fsiled return to the program saying it failed
    return -1;
...
}
除了-1之外,这个函数没有任何返回。不管发生什么,它都应该返回一些东西

最后:

/tmp/ccqYmK3M.o:llistdriver.c:(.text+0x157): undefined reference to `_search'
因为你错过了finish()结尾的一个右括号


不要因为代码已编译而忽略警告。仅修复错误是不够的!

Q:哪个源文件包含您的
int search()
?如果它不是llistdriver.c或llistlib.c,那就是你的问题:)还有,你为什么要尝试编译头文件和源文件?还有,如果你已经编写了一个库,为什么不链接它,而不是编译它的c文件和主代码?@palsm4 int search()是在llistlib.c文件中定义的。所以我完全糊涂了。@Manav抱歉,我不明白你的意思?@Digi Faria-这是一个很好的建议。请尝试一下,并报告发生的情况。@paulsm4我仍然尝试了完全相同的错误,我发布了我所有的代码,这可能有助于更清楚地了解情况。
/tmp/ccqYmK3M.o:llistdriver.c:(.text+0x157): undefined reference to `_search'