473,385 Members | 1,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include "GRAPH.h"
  3. #include "point.h"
  4. #include "pqindex.h"
  5.  
  6. int main(){
  7.     getchar();
  8.     return 0;
  9. }
  10.  
This works, but the examples from the book don't:

Expand|Select|Wrap|Line Numbers
  1. #include <stdlib.h>
  2. #include "GRAPH.h"
  3.  
  4. "This code is from "Algorithms in C, Third Edition,"
  5.                 by Robert Sedgewick, Addison-Wesley, 2002."
  6.  
  7.  
  8.  
  9. typedef struct node *link;
  10. struct node { int v; link next; };
  11. struct graph { int V; int E; link *adj; };
  12. link NEW(int v, link next)
  13.   { link x = malloc(sizeof *x);
  14.     x->v = v; x->next = next;     
  15.     return x;                         
  16.   }
  17. Graph GRAPHinit(int V)
  18.   { int v;
  19.     Graph G = malloc(sizeof *G);
  20.     G->V = V; G->E = 0;
  21.     G->adj = malloc(V*sizeof(link));
  22.     for (v = 0; v < V; v++) G->adj[v] = NULL;
  23.     return G;
  24.   }
  25. void GRAPHinsertE(Graph G, Edge e)
  26.   { int v = e.v, w = e.w;
  27.     G->adj[v] = NEW(w, G->adj[v]);
  28.     G->adj[w] = NEW(v, G->adj[w]); 
  29.     G->E++;
  30.   }
  31. int GRAPHedges(Edge a[], Graph G)
  32.   { int v, E = 0; link t;  
  33.     for (v = 0; v < G->V; v++)
  34.       for (t = G->adj[v]; t != NULL; t = t->next)
  35.         if (v < t->v) a[E++] = EDGE(v, t->v); 
  36.     return E;
  37.   }
  38.  
Would this overwrite graph.c? Or be seperate?
Jul 2 '10 #1

✓ answered by Banfa

"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.

3 5274
Banfa
9,065 Expert Mod 8TB
"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.
Jul 2 '10 #2
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 :/
Jul 2 '10 #3
Banfa
9,065 Expert Mod 8TB
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.
Jul 2 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

16
by: Maciej Kalisiak | last post by:
I use this simple test in Python: def foo(i): print i foo(i+1) import sys sys.setrecursionlimit(1000000) foo(0) Now, my understanding is that I get the segfault when Python overruns the C
5
by: Jordan Rastrick | last post by:
Hi everyone, Just a little issue that I've come across in Python where I'd be interested to read the thoughts and opinions of the great thinkers and programmers who frequent this newsgroup. ...
87
by: ziliath | last post by:
I recently tried out the Google "top coder" contest, as a C++ coder. I noticed immediately that they expected me to know STL. To which I say, what the fuck?! I may be missing something, but at...
4
by: ben | last post by:
has anyone got or read this book: robert sedgewick "algorithms in c parts 1-4"? i'm having an absolute nightmare with the book. amoung other things i'm trying to construct the necessary support...
33
by: Jacob Oost | last post by:
Should I get some more general books, like "advanced self-teaching," or can I start on specialized books like "Linux game programming?" Any book recommendations? -- ----- BEGIN GEEK CODE...
10
by: paulw | last post by:
Hi Please give problems that "HAS TO" to use recursion (recursive calls to itself.) Preferrably real world examples, not knights tour. I'm thinking about eliminating the use of stack... ...
1
by: Dean Slindee | last post by:
Can anyone point me to a code example or repository that would allow me to provide some "sounds like" comparison capability when doing a search for LastName? Thanks, Dean Slindee
37
by: jht5945 | last post by:
For example I wrote a function: function Func() { // do something } we can call it like: var obj = new Func(); // call it as a constructor or var result = Func(); // call it as...
33
by: Snis Pilbor | last post by:
With the "as if" rule in play, doesn't that effectively render the "register" keyword completely useless? Example: I make a silly compiler which creates code that goes out of its way to take a...
169
by: JohnQ | last post by:
(The "C++ Grammer" thread in comp.lang.c++.moderated prompted this post). It would be more than a little bit nice if C++ was much "cleaner" (less complex) so that it wasn't a major world wide...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.