Swapna
Swapna

Reputation: 23

Error:Undefined reference to function call

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

int main(int argc , char **argv)
{
  struct ListNode *ListHead=NULL;
  FILE *fp;
  fp = fopen("input","r");
  int nChunks,i,length,elements,values,t;
  /*insert*/
 if(fscanf(fp,"%d",&nChunks)!=EOF)
    {
      for(i=0;i<nChunks;i++)
    {
      fscanf(fp,"%d",&length);
     testCreateList(ListHead,length);

    }
    }
  while(fscanf(fp,"%d",&elements)!=EOF)
    {
      fprintf(stdout,"%d",elements);
    }
  return 0;
}

this is my main file. i am not able to find out where is the error. plus i want to create ListHead as much as "i". and now able to think how to maintain more than one ListHead.

this is mylist.h

#ifndef __MYLIST__H__
#define __MYLIST__H__

#include<stddef.h>

typedef struct
{
  struct MyNode *left,*right;
  int chunk;
}MyNode;

struct ListNode
{
  struct ListNode *ListHead;
  int nLength;
};

void testCreateList(struct ListNode *ListHead,int length);

#endif

now my CreateList.c file

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

void testCreatetList(struct ListNode *ListHead,int length)
{
  struct ListNode *node ,*temp;
  node = (struct ListNode *)malloc(sizeof(struct ListNode));
  node->nLength = length;
  ListHead = node;

}

error:

gcc testList.c
/tmp/cchD3tuF.o: In function `main':
testList.c:(.text+0x80): undefined reference to `testCreateList'
collect2: error: ld returned 1 exit status

Upvotes: 0

Views: 439

Answers (1)

jpw
jpw

Reputation: 44931

First, only include header (.h) files containing declaration, don't include .c files with definitions.

That said, the reason it's failing is that you have a typo in the CreateList.c file. testCreatetList should be testCreateList (notice the surplus t).

You might consider declaring the void testCreateList(struct ListNode *ListHead,int length) in a header and include that instead of the .c file.

There might be other issues with the code - I really didn't look that long at it.

Upvotes: 2

Related Questions