0

I have to make a linked list of words and analyze all the words that show up in a txt file and print out the words and how many times each word appears. I have three files: WordList.h, WordList.c, and 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);
    }
}

TextAnalyzer.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;
}

Output:

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)

I have no idea how to fix the output. The code is not finished and all I want to know is how to fix these errors in the output.

-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)

I am using Microsoft VS code.

vnguyen56
  • 55
  • 6
  • What did you type to compile & link it? You've shown us the output from that, but not the input (which probably started with `gcc`). Did you remember to tell the compiler about `WordList.c`, not only `TextAnalyzer.c`? – librik Oct 08 '18 at 00:59
  • i've put my input in the comments below – vnguyen56 Oct 08 '18 at 01:12
  • RE: `#ifndef _WordList_H`: Note that identifiers that start with an underscore and then another underscore or capital letter are reserved everywhere in C ([*this includes the preprocessor*](https://stackoverflow.com/a/3817087/211160)). You should avoid using them, even for include guards. Never too soon to learn the rules :-) especially since this may be a convention you saw somewhere and then copied it. Tell whoever showed you that if so. – HostileFork says dont trust SE Oct 08 '18 at 03:44

1 Answers1

-1

How do you compile these?

It seems like that you didn’t compile your source code file wordList.c

If you have gcc installed, how about trying this:

cc -o analyzer wordList.c TextAnalyzer.c

Edited:

I've tried the command above and it works just fine. No linker error generated. I'm using macOS Mojave with built-in gcc compiler. The log as below:

enter image description here

Here is the verbose(with --verbose option)

/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
Xin
  • 113
  • 3
  • 9
  • gcc -c WordList.c -o WordList.o – vnguyen56 Oct 08 '18 at 01:03
  • gcc -c TextAnalyzer.c -o TextAnalyzer.o – vnguyen56 Oct 08 '18 at 01:03
  • @vnguyen56 yeah, you can definitely separately compile these and link the object files generated. I’m just saying he could just do it in one line :) – Xin Oct 08 '18 at 01:08
  • yeah ik i was just showing you how i did it. i tried yours and still get the same error. ive tried multiple inputs into the terminal but nothing works – vnguyen56 Oct 08 '18 at 01:11
  • @vnguyen56 i kind of remembered that the order matters, just like link a static library, you may need the object files’ dependencies :) just a saying – Xin Oct 08 '18 at 01:16
  • @vnguyen56 gcc -c WordList.c gcc -c TextAnalyzer.c and then gcc -o analyzer TextAnalyzer.o WordList.o – Xin Oct 08 '18 at 01:19
  • @vnguyen56 There is a button for editing your questions. So when asked to add detail to your question (such as your compilation and linking) please add it to the original post. – HostileFork says dont trust SE Oct 08 '18 at 03:49
  • so if there is a linker error, do you know what steps i should take to resolve it? All of my classmates say theyve had no problem with it and my professor doesn't seem to know how to resolve it as well, so its definitely exclusive to my system – vnguyen56 Oct 08 '18 at 05:56
  • @vnguyen56 you could use —verbose option to see the details – Xin Oct 08 '18 at 06:35
  • @vnguyen56 the file ‘analyzer’ isn’t an object file, it’s an executable one, you can’t link it against; – Xin Oct 09 '18 at 23:59