Thread: gcc error, undefined reference

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    25

    gcc error, undefined reference

    EDIT: IGNORE THIS. MODS PLEASE LOCK. PROBLEM SOLVED. READING IS POWER
    Hello! Getting an undefined reference error. I've got two .c files at the moment.

    pair is a struct with these contents:
    char* word;
    int last_index;
    int list_size;
    char** file_list;
    this is its .c file:
    Code:
    #include "pair.h"
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    Pair pair_construct(char* key, char* file)
    {
      Pair p = malloc(sizeof(struct pair));
      p->word = key;
      p->file_list = calloc(1, sizeof(char*));
      p->file_list[0] = file;
      p->last_index = 1;
      p->list_size = 1;
    }
    
    /* Purpose: free memory associated with a Pair
     * Input: A Pair
     * Output: Freed memory
     */
    void free_pair(Pair p)
    {
      int i;
      for(i=0; i < p->list_size; i++)
        free(p->file_list[i]);
      free(p->word);
      free(p);
    }
    
    /* Purpose: Double the size of a pair's file list
     * to be used when the current list is full.
     * Input: A pair
     * Output: A pair with its file_list doubled in length
     */
    void increase_file_list_length(Pair p)
    {
      p->list_size *= 2;
      p->file_list = realloc(p->file_list, p->list_size*sizeof(char*));
    }
    
    /* Purpose: Add a file to a pair's file list
     * Input: A file name and pair
     * Output: The pair's file list updated
     */
    void add_file(Pair p, char* file)
    {
      if(p->last_index == p->list_size)
        increase_file_list_length(p);
      p->file_list[p->last_index++] = file;
    }
    the second file is a linked list. It utilizes nodes with the following structure:
    Pair p;
    Node next;

    The function below spouts issues:
    Code:
    /* Purpose: Free memory associated with a list
     * Input: Head of a list
     * Output: Freed memory
     */
    void list_free(Node head)
    {
      Node curr = head;
      Node prev = NULL;
      while( curr != NULL )
      {
        prev = curr;
        curr = curr->next;
        pair_free(prev->p);  //undefined reference error
        free(prev);
      }
    }
    prev->p is a pair, and pair_free is a defined function taking a pair as its parameter, but for some reason I'm getting a compile error. Ive used pair functions in my test main in list.c with no problem, but this one is barking at me.

    compiling with "gcc pair.c list.c" gives the following error:

    /tmp/ccnS9s0p.o: In function `list_free':
    list.c.text+0x137): undefined reference to `pair_free'


    Any help is much appreciated

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    That's a linker issue. Since you call pair functions from list code, try reversing the order of files:
    gcc list.c pair.c

    EDIT: nevermind, just saw your edit

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Also... in one file you have free_pair() and in the other you call it as pair_free()

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling GNU MP
    By mattnp12 in forum C Programming
    Replies: 3
    Last Post: 06-23-2011, 03:58 PM
  2. C and Cpp GCC Compiler Issues
    By Bangonkali in forum C Programming
    Replies: 15
    Last Post: 12-12-2010, 11:01 PM
  3. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  4. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  5. Problem with OpenGL tutorial
    By 2Biaz in forum Windows Programming
    Replies: 18
    Last Post: 09-16-2004, 11:02 AM