我试图在网上搜索那个错误,但所有帖子都是针对 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
这是 super 简单的代码,我不明白问题是什么:
#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 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
如果我在这里给你文件,也许会更容易?
无论如何,我很乐意提供有关如何理解问题的提示。
最佳答案
您的问题是函数 CreateDectionary() 中的拼写错误。您应该将其更改为 CreateDictionary()。
collect2: error: ld returned 1 exit status 在 C 和 C++ 中都是同样的问题。通常这意味着您有未解析的符号。在你的情况下,这是我之前提到的错字。
关于C编译: collect2: error: ld returned 1 exit status,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27593029/