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;
}

Dani AI

Generated

Short diagnosis and a reliable fix.

The linker message "Undefined … first referenced" means the object produced from ex3test.c refers to symbols the linker can't find. In this thread those symbols (for example discrete_uniform) live in other translation units. was right to suggest adding the other source that implements them. The compilation warnings you posted (implicit declaration of discrete_uniform and malloc, “type defaults to int”, “control reaches end of non-void function”) are the compiler telling you there are missing prototypes/headers and a missing return — fix those first so the compiler and linker can catch real problems.

Concrete steps to follow:

  • Put public prototypes in headers (e.g. uniform.h) and #include them from every .c that calls those functions. That removes implicit-declaration warnings and ensures correct link signatures.
  • Compile and link all required objects. Example workflow:
    # create object files
    gcc -g -std=c99 -Wall -Wextra -c ex3.c uniform.c ex3test.c
    # link them together (math lib if uniform uses math.h)
    gcc -o ex3test ex3.o uniform.o ex3test.o -lm
  • Add return 0; to main or otherwise fix the "control reaches end of non-void function" warning.

Runtime error note (the ERROR in uniform.discrete_uniform(): a=100 b=99 messages): that shows discrete_uniform was called with an invalid range (a > b). Either fix the caller so it passes the arguments in the right order, or make discrete_uniform defensive (swap or return an error). Also check that you actually included and used the prototype for discrete_uniform so you don't get wrong-typed calls.

Small safety tips:

Node *n = malloc(sizeof *n);
if (!n) { perror("malloc"); exit(EXIT_FAILURE); }
n->next = n->next2 = NULL;

Check malloc returns and initialize link fields before linking nodes.

Summary: include headers for prototypes, compile/link every .c that provides definitions, fix the a>b call site (or make the function defensive), and enable -Wall -Wextra to catch issues early. This addresses the linker warnings saw and follows up on ’s advice.

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.