Hi,

I wrote a program in C and when I compiled, got the following message (word for word):

Error: Unresolved external '_EDGE' referenced from C:\Graph\Test1.OBJ

The funny thing is that the error is not the usual compiling error that tells me which line of my program has the error. That kind of error usually accumulates and then my compiler will tell me how many errors are there in total. But this one is not like that. No other errors were found.

Graph is the name of my folder and Test1.c is the name of the program. After compiling it (with the above error message), I tried to run the program (with 2 arguments) by typing

test1 10 20

at the command line but the message was

"test1 is not recognized as an internal or external command, operable program or batch file"

Please, any help would be greatly appreciated.

>Error: Unresolved external '_EDGE' referenced from C:\Graph\Test1.OBJ
That's a linker error. It's telling you that you declared and used the name _EDGE somewhere in the program but neglected to defined it. When it comes to linker errors, the source files will compile, but they can't be linked together nito an executable.

>"test1 is not recognized as an internal or external command, operable program or batch file"
That's because test1.exe doesn't exist yet. There was an error before an executable could be made.

Thanks for the reply. I did some checking on my code but still can't figure out where the missing declaration or definition is at. I have enclosed my code below, so if anyone could help me check where did I go wrong?

Thanks.

#include<stdlib.h>
#include<stdio.h>

typedef struct { int v; int w; } Edge;
Edge EDGE(int, int);

typedef struct graph *Graph;
Graph GRAPHinit(int);
void GRAPHinsertE(Graph, Edge);
void GRAPHremoveE(Graph, Edge);
int GRAPHedges(Edge [], Graph G);
Graph GRAPHcopy(Graph);
void GRAPHdestroy(Graph);

int **MATRIXint(int r, int c, int val)
{ int i, j;
  int **t = malloc(r * sizeof(int *));
  for (i = 0; i < r; i++)
       t[i] = malloc(c * sizeof(int));
  for (i = 0; i < r; i++)
  for (j = 0; j < c; j++)
       t[i][j] = val;
       return t;
      }

struct graph { int V; int E; int **adj; };

Graph GRAPHinit(int V)
{ Graph G = malloc(sizeof *G);
  G->V = V; G->E = 0;
  G->adj = MATRIXint(V, V, 0);
  return G;
      }

void GRAPHinsertE(Graph G, Edge e)
{ int v = e.v, w = e.w;
  if (G->adj[v][w] == 0)
        G->E++;
        G->adj[v][w] = 1;
        G->adj[w][v] = 1;
      }

void GRAPHremoveE(Graph G, Edge e)
{ int v = e.v, w = e.w;
  if (G->adj[v][w] == 1)
        G->E--;
        G->adj[v][w] = 0;
        G->adj[w][v] = 0;
      }

int GRAPHedges(Edge a[], Graph G)
{ int v, w, E = 0;
  for (v = 0; v < G->V; v++)
  for (w = v+1; w < G->V; w++)
        if (G->adj[v][w] == 1)
            a[E++] = EDGE(v, w);
            return E;
      }

void GRAPHshow(Graph G)
{ int i, j;
  printf("%d vertices, %d edges\n", G->V, G->E);
  for (i = 0; i < G->V; i++)
      {
        printf("%2d:", i);
        for (j = 0; j < G->V; j++)
        if (G->adj[i][j] == 1) printf(" %2d", j);
        printf("\n");
                 }
       }

Graph GRAPHrand(int V, int E)
{ int i, j;
  double p = 2.0*E/V/(V-1);
  Graph G = GRAPHinit(V);
  for (i = 0; i < V; i++)
  for (j = 0; j < i; j++)
        if (rand() < p*RAND_MAX)
        GRAPHinsertE(G, EDGE(i, j));
        return G;
       }

main(int argc, char *argv[])
{ int V = atoi(argv[1]), E = atoi(argv[2]);
  Graph G = GRAPHrand(V, E);
  if (V < 20)
     GRAPHshow(G);
     else printf("Too many vertices.\n");
                             }

Thanks again!

Edited 3 Years Ago by Dani: Formatting fixed

>still can't figure out where the missing declaration or definition is at.
First you need to learn the difference between a declaration and a definition. A declaration says to the compiler "Here is what I'm called. This is my type, but I don't really exist yet". A definition says "I exist now, give me memory and stuff so I can do my job".

Let's have a concrete example. This is a declaration:

Graph GRAPHinit(int);

When you use GRAPHinit in your code, there must also be a corresponding definition or you'll get a linker error. This is the definition that goes with the declaration:

void GRAPHinsertE(Graph G, Edge e)
{ int v = e.v, w = e.w;
if (G->adj[v][w] == 0)
G->E++;
G->adj[v][w] = 1;
G->adj[w][v] = 1;
}

Now, take a look at this declaration, and find the definition in your code.

Edge EDGE(int, int);

That's your error.

>still can't figure out where the missing declaration or definition is at.
First you need to learn the difference between a declaration and a definition. A declaration says to the compiler "Here is what I'm called. This is my type, but I don't really exist yet". A definition says "I exist now, give me memory and stuff so I can do my job".

Let's have a concrete example. This is a declaration:

Graph GRAPHinit(int);

When you use GRAPHinit in your code, there must also be a corresponding definition or you'll get a linker error. This is the definition that goes with the declaration:

void GRAPHinsertE(Graph G, Edge e)
{ int v = e.v, w = e.w;
if (G->adj[v][w] == 0)
G->E++;
G->adj[v][w] = 1;
G->adj[w][v] = 1;
}

Now, take a look at this declaration, and find the definition in your code.

Edge EDGE(int, int);

That's your error.

So the error is that I "declared"
EDGE(int,int)
but I didn't define it?

So I have declared
"Edge EDGE(int,int)"

but did not define it?

>So I have declared
>"Edge EDGE(int,int)"
>but did not define it?
In the code you posted, EDGE is never given a body. You never tell the compiler what EDGE does, you only tell it that EDGE is a function taking two integers and returning an Edge. When the linker tries to link with the function that corresponds to that declaration, it doesn't find anything and bitches about it.

Clear?

This article has been dead for over six months. Start a new discussion instead.