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

后端 未结 8 971
情深已故
情深已故 2021-02-02 07:37

I am working on this tutorial on building your own LISP (http://www.buildyourownlisp.com/chapter4_interactive_prompt) and for some reason when I try to compile I get this:

相关标签:
8条回答
  • 2021-02-02 08:01

    On Debian Buster 10, I had to install the package with:

    sudo apt install libeditline-dev 
    

    Instead of:

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

    I just included:

    #include <editline.h>
    

    ran the program with -leditline flag and worked perfectly.

    0 讨论(0)
  • 2021-02-02 08:03

    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.

    0 讨论(0)
  • 2021-02-02 08:05

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

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

    Works fine now

    0 讨论(0)
  • 2021-02-02 08:09

    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.

    0 讨论(0)
  • 2021-02-02 08:16

    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.

    0 讨论(0)
  • 2021-02-02 08:18

    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.

    0 讨论(0)
提交回复
热议问题