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

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

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

我已经安装了osx开发者工具,brew正在显示readline已经安装,当我尝试brew install editline时它不知道该怎么办。

这是我的代码:

  1 #include  2 #include  3 #include  4 #include  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 

仅包括

 #include  

如果安装了命令行工具,它应该存在。 该文件包含libedit的“readline包装器”,包括历史记录function。 OS X上不存在包含文件

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

使用OSX Yosemite。 我删除了#include

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

现在工作正常

我在Ubuntu 14.04上。

试试这个:

sudo apt-get install libeditline-dev

并包括这样的:

#include

最后像这样编译:

在标志中添加-leditline

我希望这可以提供帮助。

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

 #include  

我在El Capitan,删除#include ,并使用cc -std=c99 -Wall test.c -ledit -o test对我cc -std=c99 -Wall test.c -ledit -o test
在输出-ledit之前添加标记-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 

对于那些跟随FreeBSD的人的解决方案(也可以在其他Unices上工作):

 #include  #include  #include  #include  ... 

并运行:

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

在编译步骤中没有“-lreadline”它没有链接,你将得到关于未定义引用“readline”函数的错误。

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

man editline

#include

好的,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先生的代码,以及Build your own list中的代码,并将它们组合在一起以获得此代码。

 /* 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  #include  #include  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

-ledit需要链接editline

如果它有任何相关性,我使用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。