GRAPH.h from "Algorithms in C"

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • roflmfao
    New Member
    • Jun 2010
    • 6

    GRAPH.h from "Algorithms in C"

    Hey, I recently bought "Algorithms in C" by Robert Sedgewick

    In his programms (Part 5) he refers to "graph.h"

    On the website, I found an ftp Server with those files on it:

    ftp://ftp.cs.princeton.edu/pub/cs226/map/c/

    Now, I downloaded graph.h, graph.c, point.h, point.c, pqindex.h, pqindex.c from there.

    I opened these in Dev-C++, and tried to compile "graph.c" but I get multiple errors saying "undefined reference to /something/ "

    Am I not meant to compile graph.c? Do I need a different compiler?

    The functions which it claims are undefined all seem to be defined in the downloaded files. They are all in the same directory (Desktop). Does Dev-C++ want them somewhere else?

    EDIT:

    Code:
    #include <stdio.h>
    #include "GRAPH.h"
    #include "point.h"
    #include "pqindex.h"
    
    int main(){
        getchar();
        return 0;
    }
    This works, but the examples from the book don't:

    Code:
    #include <stdlib.h>
    #include "GRAPH.h"
    
    "This code is from "Algorithms in C, Third Edition,"
                    by Robert Sedgewick, Addison-Wesley, 2002."
    
    
    
    typedef struct node *link;
    struct node { int v; link next; };
    struct graph { int V; int E; link *adj; };
    link NEW(int v, link next)
      { link x = malloc(sizeof *x);
        x->v = v; x->next = next;     
        return x;                         
      }
    Graph GRAPHinit(int V)
      { int v;
        Graph G = malloc(sizeof *G);
        G->V = V; G->E = 0;
        G->adj = malloc(V*sizeof(link));
        for (v = 0; v < V; v++) G->adj[v] = NULL;
        return G;
      }
    void GRAPHinsertE(Graph G, Edge e)
      { int v = e.v, w = e.w;
        G->adj[v] = NEW(w, G->adj[v]);
        G->adj[w] = NEW(v, G->adj[w]); 
        G->E++;
      }
    int GRAPHedges(Edge a[], Graph G)
      { int v, E = 0; link t;  
        for (v = 0; v < G->V; v++)
          for (t = G->adj[v]; t != NULL; t = t->next)
            if (v < t->v) a[E++] = EDGE(v, t->v); 
        return E;
      }
    Would this overwrite graph.c? Or be seperate?
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    "undefined reference to" is a linker error not a compiler error.

    Compiling operates on a single source file. It checks the code syntax and semantics and then produces an object file which contains the machine code implementation of all the functions and data in the file compiled.

    Linking operates on multiple files and produces the actual program by taking all the objects and combining them together with any libraries. During linking all symbols (functions and variables) must be defined. It is possible at the compilation stage to have indicated that a function or variable the source file refers to will be found in a different object or library. The linkers job is to resolve all these external references in the individual object files.

    When the link finds a symbol that is referenced in an object file as existing else where but then can not find that symbol anywhere else it produces an "undefined reference to" or "unresolved external" error.


    I suspect what you have done from your description is tried to compile and link a single source file (graph.c) individually. You need to compile all the supplied source files and link them all together. You IDE should sort this out for you if you just add them all to the same project.

    Comment

    • roflmfao
      New Member
      • Jun 2010
      • 6

      #3
      :)

      Ok, it now works to some extend. See the EDIT from post #1

      I can compile the whole thing, but can you help with the second code-snippet? Is this intended to be compiled along with the 6 files above, or replace one of them (it is basically a Graph implemented as adjacency list)

      I would just like to run these examples myself, but it won't work :/

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        I have no idea since I am not familiar with the code you have download.

        However assume graph is a class the code posted uses that class rather than defining it so that needs to be compiled along side it not over-writing it.

        Comment

        Working...