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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27593029/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 11:38:09  来源:igfitidea点击:

C compile : collect2: error: ld returned 1 exit status

ccompilationlinker

提问by JohnnyF

I tried to search for that bug online but all the posts are for C++.

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

This is the message:

这是消息:

test1.o: In function ReadDictionary': /home/johnny/Desktop/haggai/test1.c:13: undefined reference toCreateDictionary' collect2: error: ld returned 1 exit status make: *** [test1] Error 1

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

super simple code and can't understand what's the problem

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

#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;
...

now this is the header dict.h

现在这是标题 dict.h

#ifndef _DICT_H_
#define _DICT_H_

#include "hash.h"

pHash CreateDictionary();
...

#endif

and here is the dict.c

这是 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;
}

and if you wanna check hash.h

如果你想检查 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

Maybe it will be easier if I give you the files here?

如果我把文件给你,也许会更容易些?

Any way, I will be happy for tips on how to understand the problem

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

采纳答案by SVN

Your problem is the typo in the function CreateDectionary().You should change it to CreateDictionary(). collect2: error: ld returned 1 exit status is the same problem in both C and C++, usually it means that you have unresolved symbols. In your case is the typo that i mentioned before.

您的问题是函数 CreateD ectionary() 中的错字。您应该将其更改为 CreateD ictionary()。collect2: error: ld returns 1 exit status 在 C 和 C++ 中都是相同的问题,通常这意味着您有未解析的符号。在你的情况下是我之前提到的错字。

回答by iHad 169

install this

安装这个

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

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

回答by biocyberman

I got this problem, and tried many ways to solve it. Finally, it turned out that make cleanand makeagain solved it. The reason is: I got the source code together with object files compiled previously with an old gcc version. When my newer gcc version wants to link that old object files, it can't resolve some function in there. It happens to me several times that the source code distributors do not clean up before packing, so a make cleansaved the day.

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

回答by yashvardhan

sometimes this error came beacause failed to compile in middlest of any build. The best way to try is by doing make clean and again make the whole code .

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

回答by user7878441

generally this problem occurred when we have called a function which has not been define in the program file, so to sort out this problem check whether have you called such function which has not been define in the program file.

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

回答by Ajinkya Khandar

If you are using Dev C++ then your .exeor mean to say your program already running and you are trying to run it again.

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

回答by Innocent Bystander

When compiling your program, you need to include dict.c as well, eg:

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

gcc -o test1 test1.c dict.c

gcc -o test1 test1.c dict.c

Plus you have a typo in dict.c definition of CreateDictionary, it says CreateDectionary(einstead of i)

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

回答by Kashyap Rj

  1. Go to Advanced System Settings in the computer properties
  2. Click on Advanced
  3. Click for the environment variable
  4. Choose the path option
  5. Change the path option to bin folder of dev c
  6. Apply and save it
  7. Now resave the code in the bin folder in developer c
  1. 转到计算机属性中的高级系统设置
  2. 点击高级
  3. 点击环境变量
  4. 选择路径选项
  5. 将路径选项更改为 dev c 的 bin 文件夹
  6. 应用并保存
  7. 现在将代码重新保存在 developer c 中的 bin 文件夹中