I am using the file ex3test.c to run the following program, but the UNIX gives the errors:

[38] $ gcc -o ex3test ex3test.c
Undefined first referenced
symbol in file
discrete_uniform /tmp/scratch.hYayTQ/ccRDoT1o.o
printFirst /tmp/scratch.hYayTQ/ccRDoT1o.o
addToList /tmp/scratch.hYayTQ/ccRDoT1o.o
printSecond /tmp/scratch.hYayTQ/ccRDoT1o.o
disparity /tmp/scratch.hYayTQ/ccRDoT1o.o
clearList /tmp/scratch.hYayTQ/ccRDoT1o.o
ld: fatal: Symbol referencing errors. No output written to ex3test
collect2: ld returned 1 exit status

how can I fix this?

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

/* Step 1: define your struct here */
typedef struct _Node {
  int data1, data2;
  struct _Node* next;
  struct _Node* next2;
} Node;

typedef struct _List {
  Node* front;
  Node* front2;
  Node* rear;
  Node* rear2;
} List;

static List list;

/* Step 2: put code in the functions below to make the list work. */

void clearList ()
{
  while (list.front)
  {
    Node* tmp = list.front;
    list.front = list.front->next;
    free(tmp);
  }

  list.front = list.front2 = list.rear = list.rear2 = NULL;
}

void addData1(Node* newNode, int data1)
{
  Node* prev = NULL;
  Node* iter = list.front;

  newNode->data1 = data1;

  while (iter && iter->data1 < data1)
  {
    prev = iter;
    iter = iter->next;
  }
  
  if (list.front == NULL)
  {
    list.front = list.rear = newNode;
    newNode->next = NULL;
  }
  else
    if (prev == NULL)
    {
      newNode->next = list.front;
      list.front = newNode;
    }
    else {
      newNode->next = iter;
      prev->next = newNode;
      if (prev == list.rear)
        list.rear = newNode;
    }
}

void addData2(Node* newNode, int data2)
{
  Node* prev = NULL;
  Node* iter = list.front2;

  newNode->data2 = data2;
  while (iter && iter->data2 < data2)
  {
    prev = iter;
    iter = iter->next2;
  }
  
  if (list.front2 == NULL)
  {
    list.front2 = list.rear2 = newNode;
    newNode->next2 = NULL;
  }
  else
    if (prev == NULL)
    {
      newNode->next2 = list.front2;
      list.front2 = newNode;
    }
    else {
      newNode->next2 = iter;
      prev->next2 = newNode;
      if (prev == list.rear2)
        list.rear2 = newNode;
    }
}

void addToList (int data1, int data2)
{
  Node* newNode = (Node*)malloc(sizeof(Node));

  addData1(newNode, data1);
  addData2(newNode, data2);
}

void printFirst ()
{
  Node* iter = list.front;

  while (iter)
  {
    printf("(%d %d)\n", iter->data1, iter->data2);
    iter = iter->next;
  }
}

void printSecond ()
{
  Node* iter = list.front2;

  while (iter)
  {
    printf("(%d %d)\n", iter->data1, iter->data2);
    iter = iter->next2;
  }
}

int findPos2(int data1, int data2)
{
  int pos = 0;
  Node* iter = list.front2;

  while (iter && !(iter->data1 == data1 && iter->data2 == data2))
  {
    ++pos;
    iter = iter->next2;
  }

  return pos;
}

int disparity ()
{
  int result = 0;
  Node* iter = list.front;
  int pos = 0;

  while (iter)
  {
    result += abs(findPos2(iter->data1, iter->data2) - pos);

    iter = iter->next;
    ++pos;
  }

  return result;
}

Recommended Answers

All 7 Replies

I am using the file ex3test.c to run the following program, but the UNIX gives the errors:

[38] $ gcc -o ex3test ex3test.c


Do you mean when you compile ex3text.c it gives you errors

I am using the file ex3test.c to run the following program, but the UNIX gives the errors:

[38] $ gcc -o ex3test ex3test.c


Do you mean when you compile ex3text.c it gives you errors

yes

First of all, try something like this:

gcc -Wall -Wextra -o exe3test exe3test.c uniform.c -lm

The -Wall & -Wextra switches will give you a little more info about what you have done wrong in your code, and where. As you can see, I added the uniform.c file to the compile - if you are going to depend on functions found in another file, then you need to add that when compiling. Finally, if you are going to #include <math.h>, then you need to add the -lm switch at the end of your compiler command.

BTW, this will not clean up your compiler output (it will actually look worse), or produce an executable, but at least you will know what you have to start fixing in order to get there.

thank you for the wall extra information.
I had used the -lm earlier. I had compiled the ex3.c ex3test.c uniform.c before I wrote any line of code :/

those are the errors I am getting:

$ gcc -Wall -Wextra -o ex3test ex3test.c
ex3test.c:17: warning: type defaults to `int' in declaration of `discrete_uniform'
ex3test.c: In function `addElements':
ex3test.c:37: warning: implicit declaration of function `malloc'
ex3test.c: In function `test':
ex3test.c:53: warning: unused variable `avgDisp'
ex3test.c: At top level:
ex3test.c:75: warning: unused parameter 'argc'
ex3test.c:75: warning: unused parameter 'argv'
ex3test.c: In function `main':
ex3test.c:85: warning: control reaches end of non-void function
Undefined first referenced
symbol in file
discrete_uniform /tmp/scratch.hYayTQ/ccwzHMjg.o
printFirst /tmp/scratch.hYayTQ/ccwzHMjg.o
addToList /tmp/scratch.hYayTQ/ccwzHMjg.o
printSecond /tmp/scratch.hYayTQ/ccwzHMjg.o
disparity /tmp/scratch.hYayTQ/ccwzHMjg.o
clearList /tmp/scratch.hYayTQ/ccwzHMjg.o
ld: fatal: Symbol referencing errors. No output written to ex3test
collect2: ld returned 1 exit status

anyone?

I fixed some of the problems I had before and my program is compiling. However, when I run the program this is the error I am getting:
...
ERROR in uniform.discrete_uniform(): a=100 b=99
ERROR in uniform.discrete_uniform(): a=100 b=99
ERROR in uniform.discrete_uniform(): a=100 b=99
ERROR in uniform.discrete_uniform(): a=100 b=99
ERROR in uniform.discrete_uniform(): a=100 b=99
ERROR in uniform.discrete_uniform(): a=100 b=99
ERROR in uniform.discrete_uniform(): a=100 b=99
ERROR in uniform.discrete_uniform(): a=100 b=99
ERROR in uniform.discrete_uniform(): a=100 b=99
ERROR in uniform.discrete_uniform(): a=100 b=99
ERROR in uniform.discrete_uniform(): a=100 b=99
ERROR in uniform.discrete_uniform(): a=100 b=99
ERROR in uniform.discrete_uniform(): a=100 b=99
ERROR in uniform.discrete_uniform(): a=100 b=99
ERROR in uniform.discrete_uniform(): a=100 b=99
ERROR in uniform.discrete_uniform(): a=100 b=99
ERROR in uniform.discrete_uniform(): a=100 b=99
ERROR in uniform.discrete_uniform(): a=100 b=99
...

I got it!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.