c

C- Readline() undefined reference


i am trying to run this code but the compiler fails : undefined reference to ' readline ' and undefined reference to ' add_history ' .I am using CodeBlocks .This is my code :

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

#include <readline/readline.h>
#include <readline/history.h>

int main()
{
    char *buf;



    while((buf = readline("\n >> "))!=NULL)
    {
        if (strcmp(buf,"quit")==0)
            break;

        printf("[%s]\n",buf);

        if (buf[0]!=0)
            add_history(buf);
    }

    free(buf);

    return 0;
}

Solution

  • Ok, just ensure the development files for readline are installed for which you need to run this as the root user

    # apt-get install libreadline-dev
    

    as someone mentioned in the comments.

    Next, you go to the Project->Build Options menu and a dialog pops up Build Options Dialog

    then go to the Linker Settings tabLinker Settings Tab

    now just click the Add button, and type readline in the dialog that pops up enter image description here

    click Ok, and try building now, it should work.