在尝试使用已安装的开发人员工具进行编译时,未找到/正在使用editline / history.h和editline / readline.h

时间:2014-04-05 20:38:42

标签: c macos readline editline

我正在编写关于构建自己的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.

我已经安装了osx开发者工具,brew正在显示readline已经安装,当我尝试brew install 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 Exit\n");
 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 %s\n", input);
 22                       
 23     /* free input */
 24     free(input);
 25   }                           
 26   return 0;
 27 } 

这显然是非常基本的,但我真的想让这个项目滚动,所以我希望我能搞清楚这一点。这就是我用来编译的内容:

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

8 个答案:

答案 0 :(得分:34)

仅包括

#include <editline/readline.h>
如果安装了命令行工具,则应该存在

。这个文件包含 &#34; readline包装&#34;对于libedit,包括历史功能。 OS X上不存在包含文件<editline/history.h>

我使用该修改测试了您的代码,并且编译并运行没有问题。

答案 1 :(得分:6)

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

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

现在正常工作

答案 2 :(得分:4)

我在Ubuntu 14.04上。

试试这个:

sudo apt-get install libeditline-dev

并包括这样的内容:

#include <editline.h>

最终像这样编译:

在标志

中添加-leditline

我希望这可以提供帮助。

答案 3 :(得分:2)

我是OSX Mavericks并删除了为我工作的路线:

#include <editline/history.h>

答案 4 :(得分:1)

我在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

答案 5 :(得分:1)

我开始使用构建您自己的列表并遇到了同样的问题。 上述答案都不适合我。经过一些研究后我发现macOs没有提供readline函数的gnu readline库。不同版本的MacO使用名为editline的库提供readline仿真。开始...

man editline

#include <histedit.h>

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

我是新手,这对我来说仍然很混乱。有一些版本的libedit源代码可用作debian包。幸运的是,有人比我更聪明,已经挖了它并使用lbedit实现了一个命令行。他的代码在这里:https://www.cs.utah.edu/~bigler/code/libedit.html。 我接受了Bigler先生的代码,以及来自构建您自己的列表的代码,并将它们放在一起以获得此代码。

/* 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;
}

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

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

链接editline需要

-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。

答案 6 :(得分:0)

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”函数的错误。

答案 7 :(得分:0)

在Debian Buster 10上,我必须使用以下命令安装软件包:

sudo apt install libeditline-dev 

代替:

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

我刚刚加入了

#include <editline.h>

使用-leditline标志运行该程序,并且运行良好。