0

I met a error when I make file

Undefined symbols for architecture x86_64:
  "_extendArray", referenced from:
      _main in lineSort-b1083a.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [lineSort] Error 1

I've looked around for a few answers, but none really make sense to me. I am have no idea what is wrong with my code. My code as the following:

#include <stdio.h>
#include <stdlib.h>
#include "utilityFunction.h"

int main(int argc, char**argv){
    FILE *filename;
    size_t len = 0;
    char *line = NULL;
    int index;
    int countLine = 0, i = 0;
    char** new_array = malloc(16 * sizeof(char*));

    for(index = 1; index < argc; index++){ //loop for all files name you input in the command line
        filename = fopen(argv[index], "r");
        if(filename == NULL){
            printf("The file '%s' did not exist.\n", argv[index]);
        }else{
            while(getline(&line, &len, filename)!=EOF){
                if(new_array == NULL){
                    perror("Failed to allocate");
                    exit(1);
                }

                if(i<=16){
                    new_array[i] = line;
                    i++;
                }else{
                    char** extended_array = extendArray(new_array, 16, countLine);
                    extended_array[i] = line;
                }
                printf("%s\n", new_array[i]);

                countLine++;
            }
            //print line result after end of file
            printf("The file '%s' had %d lines.\n", argv[index], countLine);
        }
    }
}

utilityFunction.c

#include "utilityFunction.h"

char **extendArray(char **oldArray, int oldLen, int newLen){ 
    char **newptr = malloc(newLen);
    if(newptr == NULL){
        perror("Failed to allocate");
        exit(1);
    }
    memcpy(newptr, oldArray, oldLen);
    free(oldArray);
    return newptr;
}

And a utilityFunction.h file

#ifndef UTILITYFUNCTION_H
#define UTILITYFUNCTION_H

char **extendArray(char **oldArray, int oldLen, int newLen);

#endif // UTILITYFUNCTION_H
HxH
  • 77
  • 2
  • 11

1 Answers1

0

It might be that you're importing a header and it doesn't link to the proper library.

This question & answer might help with you answer.

Undefined symbols for architecture armv7

Freshnuts
  • 26
  • 4