C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-13-2008, 03:14 PM   #1
Registered User
 
Join Date: May 2008
Posts: 20
Question error: undefined reference to pointer

Hi all,

I'm developing a compiler and at this stage I'mhaving problems compiling one of the files ("tabela_simbolos.c"). Here's the .c and .h and the error messages:

tabela_simbolos.c
Code:
#include "tabela_simbolos.h"

#include <malloc.h>
#include <string.h>
#include <stdio.h>

extern elemento_tabela* simbolo_tabela;

//Insere um novo identificador na cauda de uma lista ligada de símbolos
elemento_tabela *insere_elemento(char *str, tipo_basico t)
{
	elemento_tabela *novo_simbolo=(elemento_tabela*) malloc(sizeof(elemento_tabela));
	elemento_tabela *aux;
	elemento_tabela* anterior;

	strcpy(novo_simbolo->nome, str);
	novo_simbolo->tipo=t;
	novo_simbolo->proximo=NULL;

	if(simbolo_tabela)	//Se a tabela já tem elementos
	{	//Procura cauda da lista e verifica se simbolo ja existe (NOTA: assume-se uma tabela de simbolos globais!)
		for(aux=simbolo_tabela; aux; anterior=aux, aux=aux->proximo)
			if(strcmp(aux->nome, str)==0)
				return NULL;
		
		anterior->proximo=novo_simbolo;	//adiciona ao final da lista
	}
	else	//simbolo_tabela tem um elemento -> o novo símbolo
		simbolo_tabela=novo_simbolo;

	return novo_simbolo;
}

void mostra_tabela()
{
	elemento_tabela *aux;
	printf("\n");

	for(aux=simbolo_tabela; aux; aux=aux->proximo)
		printf("símbolo %s, tipo %d\n", aux->nome, aux->tipo);
}

//Procura um identificador, devolve 0 caso não exista
elemento_tabela *procura_elemento(char *str)
{
	elemento_tabela *aux;

	for(aux=simbolo_tabela; aux; aux=aux->proximo)
		if(strcmp(aux->nome, str)==0)
			return aux;

	return NULL;
}
tabela_simbolos.h
Code:
#ifndef TABELA_SIMBOLOS_H
#define TABELA_SIMBOLOS_H

typedef enum {INTEIRO, FLOAT, CARACTER} tipo_basico;

typedef struct _t1
{
	char nome[32];
	tipo_basico tipo;
	struct _t1 *proximo;
} elemento_tabela;

elemento_tabela *insere_elemento(char *str, tipo_basico t);
void mostra_tabela();
elemento_tabela *procura_elemento(char *str);

#endif
Quote:
/tmp/cc2MnpKE.o: In function `insere_elemento':
tabela_simbolos.c.text+0x3b): undefined reference to `simbolo_tabela'
tabela_simbolos.c.text+0x44): undefined reference to `simbolo_tabela'
tabela_simbolos.c.text+0x90): undefined reference to `simbolo_tabela'
/tmp/cc2MnpKE.o: In function `mostra_tabela':
tabela_simbolos.c.text+0xb2): undefined reference to `simbolo_tabela'
/tmp/cc2MnpKE.o: In function `procura_elemento':
tabela_simbolos.c.text+0xf0): undefined reference to `simbolo_tabela'
collect2: ld returned 1 exit status
If anybody has an idea of the cause of the error, I'd appreciate to know your opinion :|

Many thanks,
João Fernandes

Last edited by blacknail; 09-13-2008 at 03:16 PM.
blacknail is offline   Reply With Quote
Old 09-13-2008, 03:22 PM   #2
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
You have this:
Code:
extern elemento_tabela* simbolo_tabela;
This says that somewhere, other than here, there is a elemento_tabela* called simbolo_tabela. You must somewhere define that variable.
tabstop is offline   Reply With Quote
Old 09-14-2008, 03:59 PM   #3
Registered User
 
Join Date: May 2008
Posts: 20
That was the problem... sorry :|

But now I get the following error:
Quote:
In file included from kpython.y:4:
tabela_simbolos.h:4: error: expected identifier before numeric constant
(file included from kpython.y:4 is "tabela_simbolos.h")

The code is as posted above or the following:

Code:
#ifndef TABELA_SIMBOLOS_H
#define TABELA_SIMBOLOS_H

typedef enum {INTEIRO, FLOAT, CARACTER} tipo_basico;

typedef struct _t1
{
	char nome[32];
	tipo_basico tipo;
	struct _t1 *proximo;
} elemento_tabela;

elemento_tabela *insere_elemento(char *str, tipo_basico t);
void mostra_tabela();
elemento_tabela *procura_elemento(char *str);

#endif
Thanks for the help. Any hints with this one?

Last edited by blacknail; 09-14-2008 at 04:15 PM.
blacknail is offline   Reply With Quote
Old 09-14-2008, 04:14 PM   #4
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
Well. I don't get that error, when I try to compile that file. Are one of INTEIRO, FLOAT, or CARACTER #defined somewhere else, perhaps? Somehow something on that line is apparently getting pre-processed into a number.
tabstop is offline   Reply With Quote
Old 09-14-2008, 04:27 PM   #5
Registered User
 
Join Date: May 2008
Posts: 20
The weird stuff is that I also have no errors when compiling the file alone. but when I compile with the other files of the compiler that error appears... of course it must have to do with the other files but I don't know where it must be the problem :|
blacknail is offline   Reply With Quote
Old 09-14-2008, 04:33 PM   #6
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
I would look for those three words in your other files (maybe tipo_basico too). You could also add one file at a time to the project and see which one it's in.
tabstop is offline   Reply With Quote
Old 09-14-2008, 11:31 PM   #7
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
malloc is in stdlib.h, not malloc.h
Also, see the FAQ on casting the result of malloc.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Old 09-15-2008, 04:46 AM   #8
Registered User
 
Join Date: May 2008
Posts: 20
For me, the weirdest thing is this.

The script I use for compiling is:

Code:
#!/bin/bash
clear
lex kpython.l
yacc -d kpython.y
gcc -o kpython lex.yy.c y.tab.c estruturas.h funcoes.c funcoes.h semantica.c semantica.h tabela_simbolos.c tabela_simbolos.h
When I delete the last line of the script the error is gone. It goes like this:
Code:
#!/bin/bash
clear
lex kpython.l
yacc -d kpython.y
The ironic thing is, the error is supposed to occur on kpython.y (in line #include "tabela_simbolos.h"), that still is compiled in this second version of the script. From what I've come to, I think the problem must have to do with y.tab.c, a file automatically generated when compiling the yacc file (kpython.y), and that was featured on the gcc line I deleted in the second script version... :\

Last edited by blacknail; 09-15-2008 at 04:49 AM.
blacknail is offline   Reply With Quote
Reply

Tags
error, pointer, reference, undefined

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
sorting number Leslie C Programming 8 05-20-2009 04:23 AM
Double Pointer to a data type that is already a pointer! nicm C Programming 5 04-04-2009 03:45 AM
Using Vectors. MinGW warning Viewer C++ Programming 9 03-26-2009 03:15 PM
Strange/false errors Ganoosh Windows Programming 8 10-20-2005 04:54 PM
C++ pointer vs reference C-Dumbie C++ Programming 4 11-04-2002 04:08 AM


All times are GMT -6. The time now is 03:08 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22