Joh*_*nyF 15 c linker compilation
我尝试在线搜索该bug,但所有帖子都是针对C++的.
这是消息:
test1.o:在函数
ReadDictionary': /home/johnny/Desktop/haggai/test1.c:13: undefined reference to
CreateDictionary'collect2:error:ld返回1退出状态make:***[test1]错误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;
...
Run Code Online (Sandbox Code Playgroud)
现在这是标题dict.h
#ifndef _DICT_H_
#define _DICT_H_
#include "hash.h"
pHash CreateDictionary();
...
#endif
Run Code Online (Sandbox Code Playgroud)
这是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;
}
Run Code Online (Sandbox Code Playgroud)
如果你想检查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
Run Code Online (Sandbox Code Playgroud)
如果我在这里给你这些文件会更容易吗?
无论如何,我很乐意提供有关如何理解问题的提示
小智 13
你的问题是函数CreateD e ctionary()中的拼写错误.你应该将它改为CreateD i ctionary().collect2:error:ld返回1退出状态在C和C++中都是同样的问题,通常意味着你有未解析的符号.在你的情况下是我之前提到的拼写错误.
我遇到了这个问题,并尝试了很多方法来解决它。最后,事实证明,make clean
并make
再次解决了它。原因是:我把源代码和之前用旧的 gcc 版本编译的目标文件放在一起。当我较新的 gcc 版本想要链接旧的目标文件时,它无法解析其中的某些功能。我有好几次遇到源代码分发者在打包前没有清理的情况,所以make clean
挽救了一天。