0

I am new to the C language and keep getting this error whenever I compile my C code with the command cc prompt.c. I get this error:

Undefined symbols for architecture x86_64:

"_add_history", referenced from:

  _main in prompt-66f61f.o

"_readline", referenced from:

  _main in prompt-66f61f.o

ld: symbol(s) not found for architecture x86_64

clang: error: linker command failed with exit code 1 (use -v to see invocation)

Here is my code :

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

#include <editline/readline.h>


int main(int argc, char** argv) {

  /* Print Version and Exit Information */
  puts("Lispy Version 0.0.0.0.1");
  puts("Press Ctrl+c to Exit\n");

  /* In a never ending loop */
  while (1) {

    /* Output our prompt and get input */
    char* input = readline("lispy> ");

    /* Add input to history */
    add_history(input);

    /* Echo input back to user */    
    printf("No you're a %s\n", input);

    /* Free retrieved input */
    free(input);

  }

  return 0;
}

I am writing this program on a Macbook Air running OSX 10.10.3, if that helps.

I am just starting to learn the C language so don't judge me if this question is really simple, there were no results when I searched for it.

Any help would be greatly appreciated. Thanks!

Emil Laine
  • 41,598
  • 9
  • 101
  • 157
TheBestCoder
  • 43
  • 1
  • 8

1 Answers1

1

You need to link your program with the editline library in order for your linker to find the definition of the readline and add_history functions.

You can do so by specifying the library with the -l flag in your compilation command:

cc prompt.c -ledit
Emil Laine
  • 41,598
  • 9
  • 101
  • 157
  • I tried what you said and got the same error. – TheBestCoder Jun 10 '15 at 01:33
  • @TheBestCoder Did you follow the instructions on the [`editline` website](http://thrysoee.dk/editline/)? It seems you also need to include `-lcurses`. – Emil Laine Jun 10 '15 at 01:39
  • @TheBestCoder Also if you have `pkg-config` you can simply do `\`pkg-config --libs --cflags libedit\`` instead of the `-l` flags, which will expand to the appropriate flags. – Emil Laine Jun 10 '15 at 01:47
  • @TheBestCoder Btw I get the same errors as you, but they go away and the code builds cleanly after I add `-ledit`, so I can't really reproduce your behavior. – Emil Laine Jun 10 '15 at 01:52