C编译:collect2:错误:ld返回1退出状态

我尝试在线搜索该bug,但所有post都是针对C ++的。

这是消息:

test1.o:在函数ReadDictionary': /home/johnny/Desktop/haggai/test1.c:13: undefined reference to home / johnny / Desktop / haggai / test1.c: ReadDictionary': /home/johnny/Desktop/haggai/test1.c:13: undefined reference to CreateDictionary的ReadDictionary': /home/johnny/Desktop/haggai/test1.c:13: undefined reference to ‘collect2:error:ld返回1退出状态make:*** [test1]错误1

超级简单的代码,无法理解是什么问题

 #include  #include  #include  #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  #include  #include  #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 

如果我在这里给你这些文件会更容易吗?

无论如何,我很乐意提供有关如何理解问题的提示

您的问题是函数CreateD e ctionary()中的拼写错误。您应该将其更改为CreateD i ctionary()。 collect2:error:ld返回1退出状态在C和C ++中都是同样的问题,通常意味着你有未解析的符号。 在你的情况下是我之前提到的拼写错误。

我遇到了这个问题,并尝试了很多方法来解决它。 最后,事实certificate, make clean并再次解决它。 原因是:我将源代码与先前使用旧gcc版本编译的目标文件一起获得。 当我的新gcc版本想要链接那些旧的目标文件时,它无法解析那里的某些function。 我发生了好几次,源代码经销商在打包之前没有清理,因此make clean节省了一天。

有时会出现此错误,因为无法在任何构建的中间编译。 尝试的最佳方法是make clean并再次制作整个代码。

安装这个

 sudo apt install libgl-dev libglu-dev libglib2.0-dev libsm-dev libxrender-dev libfontconfig1-dev libxext-dev 

http://www.qtcentre.org/threads/69625-collect2-error-ld-returned-1-exit-status

编译程序时,还需要包含dict.c,例如:

gcc -o test1 test1.c dict.c

另外你在CreateDictionary dict.c定义中有一个拼写错误,它说CreateDectionarye而不是i

一般来说,这个问题发生在我们调用了一个尚未在程序文件中定义的函数时,所以要解决这个问题检查你是否调用过程序文件中尚未定义的函数。

  1. 转到计算机属性中的“高级系统设置”
  2. 单击“高级”
  3. 单击环境变量
  4. 选择路径选项
  5. 将路径选项更改为dev c的bin文件夹
  6. 应用并保存
  7. 现在将代码重新保存在开发人员c的bin文件夹中