user1849043
user1849043

Reputation: 399

readline in C doesn't work

I've never encountered such a problem before. I was writing simple C program on Mac and compiled as usual with gcc.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <readline/readline.h>
#include <readline/history.h>

#define MAXLINES 5

char *lineptr[MAXLINES];
void writel(char *lineptr[], int nlines);
void quicksort(char *lineptr[], int left, int right);
void swap(char *v[], int i, int j);

/* sort input lines */
int main() {
  int nlines; /* number of lines to read */
  int i = 0;

  /* saves lines in the array lineptr */
  while (i < MAXLINES) {
    lineptr[i] = readline("Enter a line: \n");
    i++;
  }

  quicksort(lineptr, 0, MAXLINES-1);
  writel(lineptr, MAXLINES);
  return 0;
}

It seems that readline was causing the trouble. Once I commented out lineptr[i] = readline("Enter a line: \n"); it compiled okay. But I don't understand what is wrong with readline here... The error is:

Undefined symbols for architecture x86_64: "_readline", referenced from: _main in cckHOwOt.o ld: symbol(s) not found for architecture x86_64

Thanks for anyone who can give some advice. Thanks!

Upvotes: 4

Views: 2847

Answers (1)

perreal
perreal

Reputation: 97918

Compile your code with -lreadline. Of course you also need readline-devel package installed on your system.

Upvotes: 8

Related Questions