Thread: error: undefined reference to pointer

  1. #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
    /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.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    20
    That was the problem... sorry :|

    But now I get the following error:
    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.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

  5. #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 :|

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  3. Using Vectors. MinGW warning
    By Viewer in forum C++ Programming
    Replies: 9
    Last Post: 03-26-2009, 03:15 PM
  4. Strange/false errors
    By Ganoosh in forum Windows Programming
    Replies: 8
    Last Post: 10-20-2005, 04:54 PM
  5. C++ pointer vs reference
    By C-Dumbie in forum C++ Programming
    Replies: 4
    Last Post: 11-04-2002, 04:08 AM

Tags for this Thread