【问题标题】:C compile : collect2: error: ld returned 1 exit statusC编译:collect2:错误:ld返回1退出状态
【发布时间】: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


【解决方案1】:

您的问题是函数 CreateDectionary() 中的拼写错误。您应该将其更改为 CreateDictionary()。 collect2: error: ld returned 1 exit status 在 C 和 C++ 中都是一样的问题,通常这意味着你有未解析的符号。你的情况是我之前提到的错字。

【讨论】:

  • 这里的'ld'是否表示链接器有错误?
【解决方案2】:

我遇到了这个问题,并尝试了很多方法来解决它。最后,原来make cleanmake又解决了。原因是: 我得到了源代码以及以前用旧 gcc 版本编译的目标文件。当我较新的 gcc 版本想要链接旧的目标文件时,它无法解决其中的某些功能。有好几次源代码分发者在打包前不清理,所以make clean 救了一天。

【讨论】:

  • 谢谢!刚刚升级了我的电脑并安装了我所有的包,这就是最后一步'make clean'
【解决方案3】:

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

【讨论】:

    【解决方案4】:

    安装这个

    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

    【讨论】:

    • 这个问题与Qt无关,所以这个答案是完全无关的。仅仅因为该线程中的人需要其中一些库来编译并不意味着每个收到链接错误的人都需要相同的库。
    • 完全同意@Evan
    【解决方案5】:

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

    gcc -o test1 test1.c dict.c

    另外,您在CreateDictionary 的dict.c 定义中有一个错字,它显示CreateDectionarye 而不是i

    【讨论】:

    • 我做了,还是同样的问题
    • gcc -Wall -Wextra -g test1.c dict.c -o test1编译
    【解决方案6】:

    这个问题一般是在我们调用了程序文件中没有定义的函数时出现的,所以要解决这个问题,请检查您是否调用了程序文件中没有定义的函数。

    【讨论】:

      【解决方案7】:

      如果您使用的是 Dev C++,那么您的 .exe 或表示您的程序已经在运行并且您正在尝试再次运行它。

      【讨论】:

        【解决方案8】:

        如果您在尝试运行其他(或相同)程序时正在运行控制台,则会出现此问题。

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

        【讨论】:

          【解决方案9】:
          1. 转到计算机属性中的高级系统设置
          2. 点击高级
          3. 点击获取环境变量
          4. 选择路径选项
          5. 将路径选项更改为 dev c 的 bin 文件夹
          6. 应用并保存
          7. 现在在 developer c 的 bin 文件夹中重新保存代码

          【讨论】:

          • 我不知道你认为这些步骤会做什么,但它们肯定不会修复这个编译错误。
          • 我和原来的问题和这个答案有同样的问题,虽然完全跑题了,让我真的笑了!感谢这个^^
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-12-12
          • 2014-09-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多