editline/history.h and editline/readline.h not found/working on OSX when trying to compile with developer tools installed already

天涯浪子 提交于 2019-12-02 21:08:52

Include only

#include <editline/readline.h>

which should exist if the command line tools are installed. This file contains the "readline wrapper" for libedit, including the history functions as well. An include file <editline/history.h> does not exist on OS X.

I tested your code with that modification, and it compiled and ran without problems.

Using OSX Yosemite. I removed #include<editline/history.h>

and then used cc -std=c99 -Wall test.c -ledit -o test

Works fine now

I'm on Ubuntu 14.04.

try this:

sudo apt-get install libeditline-dev

and include like this:

#include <editline.h>

finally compile like this:

add -leditline in the flag

I hope this can help.

copio

I'm on OSX Mavericks and removing the line worked for me:

#include <editline/history.h>

The solution for those following along on FreeBSD (might work on other Unices as well):

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

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

...

And run:

$ cc test.c -Wall -std=c99 -lreadline -o test

Without "-lreadline" in the compile step it is not linked in and you will get errors about undefined reference to "readline" function.

I started in on Build your own list and ran into the same problem. None of the above answers worked for me. After a little research I found out that macOs doesn't have the gnu readline library that provides the readline functions, Different versions of MacOs provide emulation of readline using a library called editline. to begin...

man editline

#include <histedit.h>

Ok, editline gives you some structs for line input and history, and functions to operate on them. First you have to instantiate these structs. The documentation for editline is not very helpful because it doesn't contain any examples. Apple makes the header file available so that helps a little. http://www.opensource.apple.com/source/libedit/libedit-13/src/histedit.h

I am new to this and it was still pretty confusing to me. there is some version of the source code to libedit available as a debian package. Fortunately someone wiser than I has already dug into it and implemented a command line using lbedit. His code is here: https://www.cs.utah.edu/~bigler/code/libedit.html. I took Mr Bigler's code, and the code from Build your own list, and put them together to get this.

/* repl-macos.c
 * Repl code example from builyourownlisp.com
 * Modified by NB aug 2017
 * Code example for editline from
 * www.cs.utah.edu/~bigler/code/libedit.html
 */

#include <stdio.h>
#include <string.h>
#include <histedit.h>

char* prompt(EditLine *e){
return "lispy> ";
}

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

    EditLine *el; // Line editor state
    History *herstory; // the rest is history

    // Temp Variables   
    int count;
    const char *usrin;
    int keepreading = 1;
    HistEvent ev;

    // Initialize the editline state
    el = el_init(argv[0], stdin, stdout, stderr);
    el_set(el, EL_PROMPT, &prompt);
    el_set(el, EL_EDITOR, "emacs");

    // Initialize history
    herstory = history_init();
    if(!herstory){
        fprintf(stderr, "Couldn't initialize history\n");
        return 1;
    }

    //set history size
    history(herstory, &ev, H_SETSIZE, 800);
    // Set up the call back functions for history functionality
    el_set(el, EL_HIST, history, herstory);

    puts("Begin moLisp interpreter");
    puts("Type 'exit' at prompt to exit");

    while(keepreading){
        usrin = el_gets(el, &count);

    // add the command to the history, and echo it back to the user
        if(count > 0){
            history(herstory, &ev, H_ENTER, usrin);
            if(strcmp(usrin, "exit\n"))
                printf("No, You're a %s", usrin);
            else{
                puts("bye");
                --keepreading;
            }
        }   
    }

  // Clean up memory 
  // by freeing the memory pointed to within the structs that
  // libedit has created.
  history_end(herstory);
  el_end(el);


  return 0;
}

Notice: The instantiation of the structs that are used happens outside of the while loop, and so do the functions that free the memory those structs are using. Because of this, I added the command to exit, otherwise I think there's a memory leak if the only way to exit the while loop is by interrupting the program. To compile:

gcc repl-macos.c -ledit -Wall -o repl-edit

-ledit is needed to link editline

If it has any relevance, I am using macOs 10.4.11 and here's my compiler, output of gcc --version

powerpc-apple-darwin8-gcc-4.0.0 (GCC) 4.0.0 20041026 (Apple Computer, Inc. build 4061)

Now the only problem with this, and the book points this out, is that c-code is supposed to be portable and this isn't. The next step would be to add preprocessor directives so that it uses readline on linux and editline on macos.

Rachel

I'm on El Capitan, Remove #include <editline/history.h>, and use cc -std=c99 -Wall test.c -ledit -o test works for me.
Add the flag -ledit before the output flad, it's a linking process, allows the compiler to directly embed calls to editline in your program. Or, you'll get the below error message,

Undefined symbols for architecture x86_64:
  "_add_history", referenced from:
      _main in prompt-086f90.o
  "_readline", referenced from:
      _main in prompt-086f90.o
ld: symbol(s) not found for architecture x86_64
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!