【问题标题】:Undefined symbols from architecture, C来自架构的未定义符号,C
【发布时间】:2018-10-08 00:51:32
【问题描述】:

我必须制作一个单词的链接列表并分析出现在 txt 文件中的所有单词并打印出单词以及每个单词出现的次数。我有三个文件:WordList.h、WordList.c 和 TextAnalyzer.c

WordList.h:

#ifndef _WordList_H
#define _WordList_H

#include <stdbool.h>

typedef struct wordListNode *wordListNodePtr;

typedef struct wordListNode
{
    char word[20];
    int count;
    wordListNodePtr next;
} WordListNode;



bool listIsEmpty();

int length();

wordListNodePtr addWords(const char *word);

wordListNodePtr findWord(const char *word);

int numericalSort();

int insertWord();

void sortedInsert();

int printList();

void printWord(wordListNodePtr word);

#endif

WordList.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "WordList.h"

wordListNodePtr head = {NULL};

bool listIsEmpty()
{
    if (head == NULL)
    {
        return true;
    }
    else
    {
        return false;
    }
}

int length()
{
    wordListNodePtr current;
    int count = 0;
    for (current = head; current != NULL; current = current->next)
    {
        count++;
    }

    return count;
}

wordListNodePtr addWords(const char *word) 
{
    wordListNodePtr newWord = (wordListNodePtr)malloc(sizeof(WordListNode));
    strcpy(newWord->word, word);
    //adding to head
    if (listIsEmpty()) {
        head = newWord;
        head->next = NULL;
    }
    else {
        wordListNodePtr current = head;
        if (current->word == word) {
            current->count += 1;
        }
        else {
            insertWord(newWord);
        }
    }
    return newWord;
}

wordListNodePtr findWord (const char *word)
{
    wordListNodePtr current = head;
    wordListNodePtr foundNode;

    while (current!=NULL) {
        if (strcmp(word, current->word)==0)
        {
            foundNode = current;
        }
        else 
        {
            current = current->next;
        }
    }

    return foundNode;
}

int insertWord(const wordListNodePtr node) 
{
    wordListNodePtr current = head;
    // if only head exists
    if (current->next == NULL) {
        if (strcmp(current->word, node->word) > 0) {
            node->next = current;
            head = node;
        }
        else 
        {
            head->next = current;
        }
    }

    while (current->next != NULL) {
        if (strcmp(current->next->word, node->word) > 0)
        {
            node->next = current->next;
            current->next = node;
        }
        current = current->next;
    }
    return 0;
}

int numericalSort()
{
    wordListNodePtr sorted = NULL;

    wordListNodePtr current = head;

    while (current != NULL) 
    {
        sortedInsert(&sorted, current);
        current = current->next;
    }
    head = sorted;
    return 0;
}

void sortedInsert (wordListNodePtr head, wordListNodePtr newNode)
{
    wordListNodePtr current;
    if (head==NULL || head->count >= newNode->count)
    {
        newNode->next = head;
        head = newNode;
    }
    else 
    {
        current = head;
        while (current != NULL && current->next->count < newNode->count)
        {
            current = current->next;
        }
        newNode->next = current->next;
        current->next = newNode;
    }
}

int printList()
{
    wordListNodePtr current = head;
    if (listIsEmpty())
    {
        printf("List is empty\n");
    }
    while (current!=NULL) {
        printf("%s || %d\n", current->word, current->count);
    }
    return 0;
}

void printWord(wordListNodePtr word)
{
    if (word == NULL)
    {
        printf("Word does not exist\n");
    }
    else 
    {
        printf("Word: %s\n", word->word);
        printf("Count: %i\n", word->count);
    }
}

文本分析器.c

#include "WordList.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
//call with TextAnalyzer filename

int main(int argc, char const *argv[])
{
    FILE *ptr_file;
    char buf[100];
    ptr_file = fopen("Haiku.txt", "r");
    if (!ptr_file)
    {
        printf("Error opening file!\n");
        return 1;
    }

    //read words into linked list
    while(fscanf(ptr_file, "%s", buf)!=EOF) {
        addWords(buf);
    }

    if (listIsEmpty())
    {
        printf("List is empty.\n");
    }
    else
    {
        //print list
        printList();
    }


    return 0;
}

输出:

Undefined symbols for architecture x86_64:
  "_addWords", referenced from:
      _main in TextAnalyzer-34dd21.o
  "_listIsEmpty", referenced from:
      _main in TextAnalyzer-34dd21.o
  "_printList", referenced from:
      _main in TextAnalyzer-34dd21.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

我不知道如何修复输出。代码还没有完成,我只想知道如何修复输出中的这些错误。

-verbose

    Vinhs-Macbook-Pro:project-1-vnguyen56 vinhnguyen$ gcc -v analyzer
Apple LLVM version 9.1.0 (clang-902.0.39.2)
Target: x86_64-apple-darwin17.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
 "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -lto_library/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/libLTO.dylib -dynamic -arch x86_64 -macosx_version_min 10.13.0 -o a.out analyzer -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/9.1.0/lib/darwin/libclang_rt.osx.a
ld: can't link with a main executable file 'analyzer' for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

我正在使用 Microsoft VS 代码。

【问题讨论】:

  • 你输入了什么来编译和链接它?您已经向我们展示了其中的输出,但没有向我们展示输入(可能以gcc 开头)。你记得告诉编译器WordList.c,而不仅仅是TextAnalyzer.c吗?
  • 我已将我的输入放在下面的 cmets 中
  • RE:#ifndef _WordList_H:请注意,以下划线开头然后是另一个下划线或大写字母的标识符在 C (this includes the preprocessor) 中的任何地方都保留。你应该避免使用它们,即使是包含守卫。永远不要太早学习规则 :-) 特别是因为这可能是您在某处看到然后复制它的约定。如果是这样,请告诉给你看的人。

标签: c linked-list


【解决方案1】:

你如何编译这些?

你的源代码文件wordList.c好像没有编译

如果你安装了 gcc,试试这个怎么样:

cc -o analyzer wordList.c TextAnalyzer.c

已编辑:

我已经尝试了上面的命令,它工作得很好。没有生成链接器错误。我正在使用带有内置 gcc 编译器的 macOS Mojave。 日志如下:

这里是详细的(带有--verbose 选项)

/Library/Developer/CommandLineTools/usr/bin/ld" -demangle -lto_library /Library/Developer/CommandLineTools/usr/lib/libLTO.dylib -no_deduplicate -dynamic -arch x86_64 -macosx_version_min 10.14.0 -syslibroot /Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk -o main /var/folders/nt/pp7ktwh571v02f5y4dhzp66h0000gn/T/wordList-9145c7.o /var/folders/nt/pp7ktwh571v02f5y4dhzp66h0000gn/T/main-9e285c.o -L/usr/local/lib -lSystem /Library/Developer/CommandLineTools/usr/lib/clang/10.0.0/lib/darwin/libclang_rt.osx.a

【讨论】:

  • gcc -c WordList.c -o WordList.o
  • gcc -c TextAnalyzer.c -o TextAnalyzer.o
  • @vnguyen56 是的,您绝对可以单独编译这些并链接生成的目标文件。我只是说他可以在一行中做到这一点:)
  • 是的,我只是向您展示我是如何做到的。我试过你的,但仍然得到同样的错误。我尝试了多个输入终端,但没有任何效果
  • @vnguyen56 我记得顺序很重要,就像链接静态库一样,您可能需要目标文件的依赖关系:) 只是一个说法
猜你喜欢
  • 2016-06-20
  • 1970-01-01
  • 2015-06-15
  • 1970-01-01
  • 2014-05-14
  • 2015-06-17
  • 2016-03-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多