c - editline/history.h 和 editline/readline.h 在 macOS 上找不到/在



我正在编写本教程,以构建您自己的LISP(http://www.buildyourownlisp.com/chapter4_interactive_prompt),出于某种原因,当我尝试编译时,我得到了这个:

REPL.c:4:10: fatal error: 'editline/readline.h' file not found
#include <editline/history.h>
^
1 error generated.

我已经安装了macOS开发人员工具,并且brew显示已安装readline,当我尝试brew安装editline时,它不知道该怎么做。

这是我的代码:

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <editline/readline.h>
  4 #include <editline/history.h>
  5 
  6 int main(int argc, char** argv) {
  7      
  8   /* version/exit info */
  9   puts("Edward Version 0.0.1");
 10   puts("Press Ctrl+c to Exitn");
 11          
 12   /* endless loop for main REPL */
 13   while (1) { 
 14     /* output prompt and read line */
 15     char* input = readline("lispy> ");
 16                   
 17     /* put input in history  */
 18     add_history(input);
 19                       
 20     /* Echo input back */                          
 21     printf("No you're a %sn", input);
 22                       
 23     /* free input */
 24     free(input);
 25   }                           
 26   return 0;
 27 } 

这显然是非常基本的,但我真的很想让这个项目滚动,所以我希望我能解决这个问题。这是我用来编译的:

cc -std=c99 -Wall REPL.c -ledit -o REPL

仅包含

#include <editline/readline.h>

如果安装了命令行工具,则应该存在。此文件包含libedit的"readline wrapper",包括历史函数。OS X 上不存在包含文件<editline/history.h>

我用这种修改测试了你的代码,它编译和运行没有问题。

使用 OSX Yosemite。我删除了#include<editline/history.h>

然后使用cc -std=c99 -Wall test.c -ledit -o test

现在工作正常

我在El Capitan,删除#include <editline/history.h> ,并使用cc -std=c99 -Wall test.c -ledit -o test对我有用。
在输出 flad 之前添加标志-ledit,这是一个链接过程,允许编译器直接在程序中嵌入对 editline 的调用。或者,您将收到以下错误消息,

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

我在 Ubuntu 14.04 上。

试试这个:

sudo apt-get install libeditline-dev

并像这样包含:

#include <editline.h>

最后像这样编译:

在标志中添加-leditline

我希望这能有所帮助。

我在OSX Mavericks上,删除该行对我有用:

#include <editline/history.h>

那些在 FreeBSD 上关注的人的解决方案(可能也适用于其他 Unices):

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

并运行:

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

编译步骤中没有"-lreadline",它不会链接,你会得到关于对"readline"函数的未定义引用的错误。

我从构建自己的列表开始并遇到了同样的问题。以上答案都不适合我。 经过一番研究,我发现macO没有提供readline函数的gnureadline库,不同版本的MacO使用称为editline的库提供readline的模拟。开始...

man editline

#include <histedit.h>

好的,编辑行为您提供了一些用于行输入和历史记录的结构,以及对其进行操作的功能。首先,您必须实例化这些结构。editline 的文档不是很有帮助,因为它不包含任何示例。Apple使头文件可用,因此有所帮助。http://www.opensource.apple.com/source/libedit/libedit-13/src/histedit.h

我是新手,这对我来说仍然很困惑。 有一些版本的源代码可以作为 Debian 软件包使用。幸运的是,比我更聪明的人已经深入研究并使用 lbedit 实现了命令行。他的代码在这里:https://www.cs.utah.edu/~bigler/code/libedit.html。我拿了比格勒先生的代码,以及"建立你自己的列表"中的代码,把它们放在一起得到这个。

/* 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 historyn");
        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, "exitn"))
                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;
}

注意:所用结构的实例化发生在while 循环,释放这些结构正在使用的内存的函数也是如此。因此,我添加了退出命令,否则如果退出 while 循环的唯一方法是中断程序,我认为存在内存泄漏。要编译:

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

-ledit 需要链接编辑行

如果它有任何相关性,我正在使用macOS 10.4.11这是我的编译器,输出 gcc --version

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

现在唯一的问题,书中指出了这一点,是C代码应该是可移植的,但事实并非如此。下一步是添加预处理器指令,以便它在 Linux 上使用 readline,在 macos 上使用 editline。

如果你在 ubuntu 上,请添加 editline 库 sudo apt-get install libtedit-dev

在 Debian Buster 10 上,我必须安装软件包:

sudo apt install libeditline-dev 

而不是:

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

我只是包括:

#include <editline.h>

使用-leditline标志运行程序并完美运行。

最新更新