【发布时间】:2015-02-19 23:34:16
【问题描述】:
我试图在网上搜索那个错误,但所有帖子都是针对 C++ 的。
这是消息:
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
超级简单的代码,看不懂是什么问题
#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;
...
现在这是头文件 dict.h
#ifndef _DICT_H_
#define _DICT_H_
#include "hash.h"
pHash CreateDictionary();
...
#endif
这里是dict.c
#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;
}
如果你想检查 hash.h
#ifndef _HASH_H_
#define _HASH_H_
//type defintions//
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
如果我把文件给你这里会更容易吗?
无论如何,我很乐意提供有关如何理解问题的提示
【问题讨论】:
-
在C和C++中也是同样的问题,链接器找不到
CreateDictionary。在您的情况下,可能是因为您定义了CreateDectionary... -
你能解释一下吗?我定义了什么而不是什么?
-
你有一个错字。字典不是 Dectionary.
标签: c compilation linker