C编译: collect2: error: ld returned 1 exit status

标签 c compilation linker

我试图在网上搜索那个错误,但所有帖子都是针对 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

这是 super 简单的代码,我不明白问题是什么:

#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 definitions //
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

如果我在这里给你文件,也许会更容易?

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

最佳答案

您的问题是函数 CreateDectionary() 中的拼写错误。您应该将其更改为 CreateDictionary()。

collect2: error: ld returned 1 exit status 在 C 和 C++ 中都是同样的问题。通常这意味着您有未解析的符号。在你的情况下,这是我之前提到的错字。

关于C编译: collect2: error: ld returned 1 exit status,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27593029/

相关文章:

linux - 查找共享库中变量的地址

c - Void 函数段错误中的字符串错误

c - 将文件读入缓冲区,但调用 fseek 后程序在 fread 处崩溃

java - 如何正确编译这2个java文件?

compilation - ELF、PIE ASLR 以及介于两者之间的所有内容,特别是在 Linux 中

C 中的编译问题

ios - libz.dylib 与 libz.1.2.3.dylib 与 libz.1.2.5.dylib

无法链接到 macOS 上的 C 标准库

带字符串操作的 C EXC_BAD_ACCESS

将大端 MIPS 转换为 C