Search code examples
ccompilationlinker

C compile : collect2: error: ld returned 1 exit status


I tried to search for that bug online, but all the posts are for C++.

This is the message:

test1.o: In function `ReadDictionary':
/home/johnny/Desktop/haggai/test1.c:13: undefined reference to `CreateDictionary'
collect2: error: ld returned 1 exit status
make: *** [test1] Error 1

It is super simple code, and I can't understand what the problem is:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dict.h"
#include "hash.h"


pHash ReadDictionary() {
    /* This function reads a dictionary line by line from the standard input. */
    pHash dictionary;
    char entryLine[100] = "";
    char *word, *translation;

    dictionary = CreateDictionary();
    while (scanf("%s", entryLine) == 1) { // Not EOF
        word = strtok(entryLine, "=");
        translation = strtok(NULL, "=");
        AddTranslation(dictionary, word, translation);
    }
    return dictionary;
}

int main() {
    pHash dicti;
...

Now this is the header file dict.h:

#ifndef _DICT_H_
#define _DICT_H_

#include "hash.h"

pHash CreateDictionary();
...

#endif

And here is the dict.c file:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hash.h"
#include "dict.h"


pHash CreateDectionary()
{
    pHash newDict;
    newDict = HashCreate(650, HashWord, PrintEntry, CompareWords, GetEntryKey, DestroyEntry);
    return newDict;
}

And if you want to check file hash.h:

#ifndef _HASH_H_
#define _HASH_H_

// Type definitions //
typedef enum {FAIL = 0, SUCCESS} Result;
typedef enum {SAME = 0, DIFFERENT} CompResult;

typedef struct _Hash Hash, *pHash;

typedef void* pElement;
typedef void* pKey;

// Function types //
typedef int (*HashFunc) (pKey key, int size);
typedef Result (*PrintFunc) (pElement element);
typedef CompResult (*CompareFunc) (pKey key1, pKey key2);
typedef pKey (*GetKeyFunc) (pElement element);
typedef void (*DestroyFunc)(pElement element);
...

// Interface functions //

#endif

Maybe it will be easier if I give you the files here?

Anyway, I will be happy for tips on how to understand the problem.


Solution

  • Your problem is the typo in the function CreateDectionary(). You should change it to CreateDictionary().

    collect2: error: ld returned 1 exit status is the same problem in both C and C++. Usually it means that you have unresolved symbols. In your case, it is the typo that I mentioned before.