0

I'm writing an implementation for Hash Tables in C, but I'm getting bogged down with some stuff. I have written the functions and the data definitions in a separate file. I have the header file Hashes.h which looks like:

#ifndef HASHES_H_INCLUDED
#define HASHES_H_INCLUDED

int accumulation(long int);

struct entry_s {
    char *key;
    char *value;
    struct entry_s *next;
};

typedef struct entry_s entry_t;

struct hashtable_s {
    int size;
    struct entry_s **table;
};

typedef struct hashtable_s hashtable_t;

hashtable_t * ht_create( int );

#endif // HASHES_H_INCLUDED

and I have the Hashes.c file which looks like:

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "Hashes.h"

/*Creates the hashtable*/

hashtable_t *ht_create( int size ) {

    hashtable_t * hashtable = NULL;
    int i;

    if( size < 1 ) return NULL;

    /* Allocate the table itself. */
    if ( ( hashtable = malloc( sizeof( hashtable_t ) ) ) == NULL ) {
        return NULL;
    }

    /* Allocate pointers to the head nodes. */
    if ( ( hashtable->table = malloc( sizeof( entry_t * ) * size ) ) == NULL ) {
        return NULL;
    }

    for( i = 0; i < size; i++ )
        {
        hashtable->table[i] = NULL;
        }

    hashtable->size = size;

    return hashtable;
}


int accumulation (long int x)
{
    int hash = 0;
    int i = 0;

    while (x != 0)
    {
        hash += pow(33, i) + x % 10;
        x /= 10;
    }

    return hash;
}

and in the main I have :

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


int main()
{
    hashtable_t* myHashTable = ht_create(10000);
    return 0;
}

I compiled all the files with no error or warning, but when I run the program I receive the error "Undefined reference to 'ht_create'". If some one has a clue I would deeply appreciate that.

P.S.: I am using CodeBlocks IDE.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Dragos2795
  • 29
  • 1
  • 9
  • What's the name of your main file, main.c or main.cpp ? – Paul R Jan 09 '16 at 16:10
  • "Undefined reference to *xxx*" is not a run-time error - it's a *link error*. Yet you say it compiles without a problem. ...Are you sure about that? – Jongware Jan 09 '16 at 16:13
  • what command(s) are you using to compile? – UnholySheep Jan 09 '16 at 16:13
  • It's a linker command. You should use a command like this: `cc -o hash_example main.o Hashes.o` – user3159253 Jan 09 '16 at 16:16
  • @UnholySheep since I'm using an IDE I do not use any specific command, I'm just pressing the "Build" button and the IDE makes all the work. – Dragos2795 Jan 09 '16 at 16:18
  • 1
    @Dragos2795 and apparently it's doing it wrong, else you wouldn't have a linker error. So please check what commands your IDE is issuing for the compilation. – UnholySheep Jan 09 '16 at 16:19
  • 1
    You have two options: 1. make sure that both of your source files are listed in the `Projects` tab of the "Management" toolview. The second, less convenient but more reliable option is to switch to hand-written `Makefile`s, as explained [here](http://wiki.codeblocks.org/index.php/Code::Blocks_and_Makefiles) – user3159253 Jan 09 '16 at 16:32
  • I suspect `Hashes.c` was not being compiled – David C. Rankin Jan 09 '16 at 16:36
  • @UnholySheep I'm am not able to find those commands ... even thogh I've entered the compiler settings menu... – Dragos2795 Jan 09 '16 at 16:50
  • @Dragos2795 did you go to compiler settings in your project's build options? As others have mentioned the most likely case is that something is wrong with your build settings which is why `Hashes.c` is not being compiled/linked. You need to either fix that (which involves understanding how to work with code blocks build settings) or manually compile (either through makefiles or by calling the compiler by hand) – UnholySheep Jan 09 '16 at 16:56
  • 1
    The basic problem is that you need to link both `main.o` and `Hashes.o` together to create an executable. Most programs are made of multiple files (plus libraries), so the [Code::Blocks](http://codeblocks.org/) IDE will have a way to allow you to specify that — though it may expect you to specify the source file names rather than the object file names. You could also use a `makefile` and `make` from the command line, of course. But you need to find out how to link programs built from multiple source files in the IDE. – Jonathan Leffler Jan 09 '16 at 18:27
  • 1
    Incidentally, your hash algorithm is a little peculiar. `pow(33,i)` always returns `1.000` (it's a floating point function) because `i` is permanently 0. It's quicker to write `1`. Also, `pow(33,9)` would give you integer overflow unless you were using `long long`. The algorithm as written boils down to 'sum of digits plus count of digits', which is not gong to be a particularly good hash. – Jonathan Leffler Jan 09 '16 at 18:33

0 Answers0