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

时间:2014-12-21 19:39:37

标签: c compilation linker

我尝试在线搜索该错误,但所有帖子都是针对C ++的。

这是消息:

  

test1.o:在函数ReadDictionary': /home/johnny/Desktop/haggai/test1.c:13: undefined reference to CreateDictionary'中   collect2:错误: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;
...

现在这是标题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

如果我在这里提供文件,可能会更容易吗?

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

9 个答案:

答案 0 :(得分:13)

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

答案 1 :(得分:2)

安装此

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

答案 2 :(得分:1)

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

答案 3 :(得分:1)

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

答案 4 :(得分:0)

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

gcc -o test1 test1.c dict.c

另外,你在CreateDictionary的dict.c定义中有一个拼写错误,它说CreateDectionarye代替i

答案 5 :(得分:0)

一般来说,当我们调用了一个尚未在程序文件中定义的函数时会出现这个问题,所以要解决这个问题,检查是否有人调用了程序文件中尚未定义的函数。

答案 6 :(得分:0)

如果您使用的是Dev C ++,则您的.exe或说您的程序已经在运行,并且您试图再次运行它。

答案 7 :(得分:0)

当您尝试运行其他(或相同)程序时有一个正在运行的控制台时,会出现此问题。

我在 Sublime Text 上执行程序时遇到了这个问题,而我已经在 DevC++ 上运行了另一个程序。

答案 8 :(得分:-4)

  1. 转到计算机属性中的高级系统设置
  2. 点击高级
  3. 单击环境变量
  4. 选择路径选项
  5. 将路径选项更改为dev c的bin文件夹
  6. 申请并保存
  7. 现在将代码重新保存在developer c
  8. 的bin文件夹中
相关问题