#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    152
    Rep Power
    39

    Gcc isn't finding my header files


    Hello,

    I have two simple files, list.c and list.h both in the current directory. I am trying to compile using the following:

    Code:
    > gcc -o list list.c
    But all of my functions declared in list.h can't be found by gcc:

    ": undefined reference to `AddItem' "

    In list.c I have included the list.h file as follows:

    Code:
    #include "list.h"
    Any help?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    May 2004
    Posts
    3,417
    Rep Power
    886
    If it's not complaining about not being able to open list.h, then it's finding it. You may have to post some code for us to be able to help you.
    I no longer wish to be associated with this site.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    152
    Rep Power
    39
    It's code straight from an old book...

    list.c
    Code:
    #include <stdio.h>
    #include <stdlib.h> /* prototype for exit() */
    #include "list.h"
    
    void showmovies(Item item);
    
    int main(void)
    {
            List movies;
            Item temp;
    
            /*initialize */
            InitializeList(&movies);
            if (ListIsFull(&movies))
            {
                    fprintf(stderr, "No memory available! Bye!\n");
                    exit(1);
            }
    
            /* gather and store */
            puts("Enter first movie title:");
            while (gets(temp.title) != NULL && temp.title[0] != '\0')
            {
                    puts("Etner your rating <0-10>:");
                    scanf("%d", &temp.rating);
                    while (getchar() != '\n')
                            continue;
                    if (AddItem(temp, &movies) == false)
                    {
                            fprintf(stderr, "Problem allocating memory\n");
                            break;
                    }
                    if (ListIsFull(&movies))
                    {
                            puts("The list is now full.");
                            break;
                    }
                    puts("Enter next movie title (empty line to stop:");
            }
    
            /*display*/
            if (ListIsEmpty(&movies))
                    printf("No data entered.");
            else
            {
                    printf("Here is the movie list:\n");
                    Traverse(&movies, showmovies);
            }
            printf("You entered %d movies.\n", ListItemCount(&movies));
    
            /* clean up */
            EmptyTheList(&movies);
            printf("Bye!\n");
    
            void showmovies(Item item)
            {
                    printf("Movie: %s  Rating: %d\n", item.title, item.rating);
            }
    }
    and list.h
    Code:
    #ifndef LIST_H_
    #define LIST_H_
    #include <stdbool.h>  /* C99 feature */
    
    #define TSIZE 50 /* size of array to hold title */
    struct film
    {
            char title[TSIZE];
            int rating;
    };
    
    typedef struct film Item;
    
    typedef struct node
    {
            Item item;
            struct node * next;
    } Node;
    
    typedef Node * List;
    
    /* function prototypes */
    
    /* operation:           initialize a list */
    /* preconditions:       plist points to a list */
    /* postconditions:      the list is initialized to empty */
    void InitializeList(List * plist);
    
    /* operation:           determine if list is empty */
    /*                                      plist points to an initialized list */
    /* postcondition:       function returns True if list is empty
     *                                      and returns False otherwise */
    bool ListIsEmpty(const List *plist);
    
    /* operation:           determine if list is full */
    /*                                      plist point to an initialized list */
    /* postcondition:       function returns True if list is full
     *                                      and returns False otherwise */
    bool ListisFull(const List *plist);
    
    /* operation:           determine number of items in a list */
    /*                                      plist points to an initialized list */
    /* postconditions:      function returns number of items in list */
    unsigned int ListItemCount(const List *plist);
    
    /* operation:           add item to end of list */
    /* precondition:        item is an item to be added to list */
    /*                                      plist points to an initialized list */
    /* postcondition:       if possible, function adds item to end of
     *                                      list and returns True; otherwise the function
     *                                      returns False */
    bool AddItem(Item item, List * plist);
    
    /* operation:           apply a function to each item in list */
    /*                                      plist points to an initialized list
     *                              pfun points to a function that takes an
     *                              Item argument and has no return value */
    /* postcondition:       the function pointed to by pfun is executed
     *                                      once for each item in the list */
    void Traverse(const List *plist, void (* pfun)(Item item) );
    
    /* operation:           free allocated memory, if any */
    /*                                      plist points to an initialized list */
    /* postcondition:       any memory allocated for the list is freed
     *                                      and the list is set to empty */
    void EmptyTheList(List * plist);
    
    #endif
    and a verbose gcc...
    Code:
    > gcc -v -o list list.c
    Using built-in specs.
    Target: i386-undermydesk-freebsd
    Configured with: FreeBSD/i386 system compiler
    Thread model: posix
    gcc version 4.2.1 20070719  [FreeBSD]
     /usr/libexec/cc1 -quiet -v -D_LONGLONG list.c -quiet -dumpbase list.c -auxbase list -version -o /var/tmp//ccsooByW.s
    ignoring duplicate directory "/usr/include"
    #include "..." search starts here:
    #include <...> search starts here:
     /usr/include
    End of search list.
    GNU C version 4.2.1 20070719  [FreeBSD] (i386-undermydesk-freebsd)
            compiled by GNU C version 4.2.1 20070719  [FreeBSD].
    GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
    Compiler executable checksum: 8eba69496b6a742e69eae65d9cb92841
     /usr/bin/as -o /var/tmp//cc8ugWT5.o /var/tmp//ccsooByW.s
     /usr/bin/ld --eh-frame-hdr -V -dynamic-linker /libexec/ld-elf.so.1 -o list /usr/lib/crt1.o /usr/lib/crti.o /usr/lib/crtbegin.o -L/usr/lib -L/usr/lib /var/tmp//cc8ugWT5.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/crtend.o /usr/lib/crtn.o
    GNU ld version 2.15 [FreeBSD] 2004-05-23
      Supported emulations:
       elf_i386_fbsd
    /var/tmp//cc8ugWT5.o(.text+0x1ca): In function `main':
    : warning: warning: this program uses gets(), which is unsafe.
    /var/tmp//cc8ugWT5.o(.text+0x1b): In function `main':
    : undefined reference to `InitializeList'
    /var/tmp//cc8ugWT5.o(.text+0x26): In function `main':
    : undefined reference to `ListIsFull'
    /var/tmp//cc8ugWT5.o(.text+0x168): In function `main':
    : undefined reference to `AddItem'
    /var/tmp//cc8ugWT5.o(.text+0x1a1): In function `main':
    : undefined reference to `ListIsFull'
    /var/tmp//cc8ugWT5.o(.text+0x1e5): In function `main':
    : undefined reference to `ListIsEmpty'
    /var/tmp//cc8ugWT5.o(.text+0x20b): In function `main':
    : undefined reference to `showmovies'
    /var/tmp//cc8ugWT5.o(.text+0x216): In function `main':
    : undefined reference to `Traverse'
    /var/tmp//cc8ugWT5.o(.text+0x221): In function `main':
    : undefined reference to `ListItemCount'
    /var/tmp//cc8ugWT5.o(.text+0x23c): In function `main':
    : undefined reference to `EmptyTheList'
    Last edited by MRAB54; October 3rd, 2009 at 04:04 PM.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    152
    Rep Power
    39
    Ahh, cripes I'm an idiot lol. I haven't flipped to the next page to actually flesh out the functions. This thread can get deleted.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    May 2004
    Posts
    3,417
    Rep Power
    886
    Yup. It was the linker that was complaining, not the compiler.
    I no longer wish to be associated with this site.

IMN logo majestic logo threadwatch logo seochat tools logo