Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

readline in C doesn't work

Tags:

c

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!

like image 412
user1849043 Avatar asked Oct 24 '25 20:10

user1849043


1 Answers

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

like image 149
perreal Avatar answered Oct 26 '25 11:10

perreal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!