[历史归档]本文原发布于 cstriker1407.info 个人博客,内容为历史存档,仅供参考。
发布时间:2015-01-17| 标题:GNU Readline Library编译及简单分析|分类:编程 / C && C++ |标签:readline
GNU Readline Library编译及简单分析
- 编译:
- 使用:
GNU Readline 是一个开源的程序库,可以实现交互式的文本编辑功能。这里简单的笔记下如何编译和使用它。
官网:http://cnswww.cns.cwru.edu/php/chet/readline/rltop.html
编译:
作者编译使用的linux版本是Ubuntu14.04x64版本。
首先从官网上下载下来压缩文件。这里作者下载的版本是readline-6.3.tar.gz。
根据INSTALL文件的介绍,输入命令如下:
cstriker1407@ubuntu1404x64:~$tar-zxfreadline-6.3.tar.gz#解压缩tar包cstriker1407@ubuntu1404x64:~$cdreadline-6.3/ cstriker1407@ubuntu1404x64:~/readline-6.3$ ./configure#生成头文件cstriker1407@ubuntu1404x64:~/readline-6.3$makeeverything#编译所有文件cstriker1407@ubuntu1404x64:~/readline-6.3$sudomakeinstall#使用root权限,将编译好的库拷贝到系统中。如果过程中出现了错误
/usr/bin/ld: cannot find -ltermcap可以安装一个软件,命令如下:
cstriker1407@ubuntu1404x64:~/readline-6.3$sudoapt-getinstalllibncurses5-dev使用:
压缩包中附带了很多例子,这里看一个最简单的:
cstriker1407@ubuntu1404x64:~/readline-6.3$cdexamples/ cstriker1407@ubuntu1404x64:~/readline-6.3/examples$lsautoconf hist_erasedups histexamp.o Makefile rl.c rlcat.c rlfe rltest.c rlwrap-0.30.tar.gz excallback.c hist_erasedups.c hist_purgecmd Makefile.in rl-callbacktest rlcat.o rl-fgets.c rltest.o fileman hist_erasedups.o hist_purgecmd.c manexamp.c rl-callbacktest.c rlevent rl.o rlversion fileman.c histexamp hist_purgecmd.o readlinebuf.h rl-callbacktest.o rlevent.c rlptytest.c rlversion.c fileman.o histexamp.c Inputrc rl rlcat rlevent.o rltest rlversion.o cstriker1407@ubuntu1404x64:~/readline-6.3/examples$ ./rlversion6.3这里分析下rlversion的源码,有删改:
#ifdefined(HAVE_CONFIG_H)#include<config.h>#endif#include<stdio.h>#include<sys/types.h>#include"posixstat.h"#ifdefHAVE_STDLIB_H#include<stdlib.h>#elseexternvoidexit();#endif#ifdefREADLINE_LIBRARY#include"readline.h"#else#include<readline/readline.h>#endifmain(){printf("%s", rl_library_version ? rl_library_version : "unknown");exit(0);}由于是下载的源码编译,这里宏HAVE_CONFIG_H和READLINE_LIBRARY在makefile里面被定义了。
如果是直接安装编译好的readline库,应该可以省掉。
来看下examples下面的makefile,有删改
RL_LIBRARY_VERSION = 6.3 SHELL = /bin/sh RM = rm -f prefix = /usr/local exec_prefix = ${prefix} datarootdir = ${prefix}/share bindir = ${exec_prefix}/bin srcdir = . datadir = ${datarootdir} top_srcdir = .. #BUILD_DIR = . BUILD_DIR = /home/cstriker1407/readline-6.3 installdir = $(datadir)/readline INSTALL = /usr/bin/install -c INSTALL_PROGRAM = ${INSTALL} INSTALL_DATA = ${INSTALL} -m 644 EXEEXT = OBJEXT = o # Support an alternate destination root directory for package building DESTDIR = DEFS = -DHAVE_CONFIG_H CC = gcc CFLAGS = -g -O LOCAL_CFLAGS = -DREADLINE_LIBRARY -DRL_LIBRARY_VERSION='"$(RL_LIBRARY_VERSION)"' CPPFLAGS = INCLUDES = -I$(srcdir) -I$(top_srcdir) -I.. CCFLAGS = $(DEFS) $(LOCAL_CFLAGS) $(CPPFLAGS) $(INCLUDES) $(CFLAGS) LDFLAGS = -g -L.. -L./lib/termcap PURIFY = READLINE_LIB = ../libreadline.a HISTORY_LIB = ../libhistory.a TERMCAP_LIB = -ltermcap .c.o: ${RM} $@ $(CC) $(CCFLAGS) -c $< SOURCES = rlversion.c EXECUTABLES = rlversion$(EXEEXT) OBJECTS = rlversion.o OTHEREXE = rlptytest$(EXEEXT) OTHEROBJ = rlptytest.o all: $(EXECUTABLES) everything: all check: rlversion$(EXEEXT) @echo Readline version: `rlversion$(EXEEXT)` installdirs: -$(SHELL) $(top_srcdir)/support/mkdirs $(DESTDIR)$(installdir) install: installdirs @for f in $(SOURCES); do \ $(RM) $(DESTDIR)$(installdir)/$$f ; \ $(INSTALL_DATA) $(srcdir)/$$f $(DESTDIR)$(installdir) ; \ done uninstall: @for f in $(SOURCES); do \ $(RM) $(DESTDIR)$(installdir)/$$f ; \ done -rmdir $(DESTDIR)$(installdir) rlversion$(EXEEXT): rlversion.o $(READLINE_LIB) $(CC) $(LDFLAGS) -o $@ rlversion.o $(READLINE_LIB) $(TERMCAP_LIB) clean mostlyclean: $(RM) $(OBJECTS) $(OTHEROBJ) $(RM) $(EXECUTABLES) $(OTHEREXE) *.exe distclean maintainer-clean: clean $(RM) Makefile rlversion.o: rlversion.c rlversion.o: $(top_srcdir)/readline.h