好记性不如铅笔头

C && C++, 编程

GNU Readline Library编译及简单分析

GNU Readline 是一个开源的程序库,可以实现交互式的文本编辑功能。这里简单的笔记下如何编译和使用它。

官网:http://cnswww.cns.cwru.edu/php/chet/readline/rltop.html

CONTENTS

编译:

作者编译使用的linux版本是Ubuntu14.04×64版本。

首先从官网上下载下来压缩文件。这里作者下载的版本是readline-6.3.tar.gz。

根据INSTALL文件的介绍,输入命令如下:

cstriker1407@ubuntu1404x64:~$ tar -zxf readline-6.3.tar.gz  #解压缩tar包
cstriker1407@ubuntu1404x64:~$ cd readline-6.3/
cstriker1407@ubuntu1404x64:~/readline-6.3$ ./configure #生成头文件
cstriker1407@ubuntu1404x64:~/readline-6.3$ make everything  #编译所有文件
cstriker1407@ubuntu1404x64:~/readline-6.3$ sudo make install #使用root权限,将编译好的库拷贝到系统中。

如果过程中出现了错误

/usr/bin/ld: cannot find -ltermcap

可以安装一个软件,命令如下:

cstriker1407@ubuntu1404x64:~/readline-6.3$ sudo apt-get install libncurses5-dev

使用:

压缩包中附带了很多例子,这里看一个最简单的:

cstriker1407@ubuntu1404x64:~/readline-6.3$ cd examples/
cstriker1407@ubuntu1404x64:~/readline-6.3/examples$ ls
autoconf      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$ ./rlversion 
6.3

这里分析下rlversion的源码,有删改:

#if defined (HAVE_CONFIG_H)
#include <config.h>
#endif

#include <stdio.h>
#include <sys/types.h>
#include "posixstat.h"

#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#else
extern void exit();
#endif

#ifdef READLINE_LIBRARY
#include "readline.h"
#else
#include <readline/readline.h>
#endif

main()
{
	printf ("%s\n", 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

 

发表评论

15 + 8 =

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据