前端时间玩了一下memcached,由于工作关系得过一段时间在玩它。哎,工作就是这样,老变!
libeven
memcached的使用需要libeven的支持,我们得先装上libeven。
官方网站:http://www.monkey.org/~provos/libevent/
下载地址:http://www.monkey.org/~provos/libevent-1.4.0-beta.tar.gz
libevent安装方式比较简单:
以下是代码片段: ./configure && make make install |
检查如下:
以下是引用片段: [root@localhost memcached-1.2.2]# ls -l /usr/local/lib/ | grep even lrwxrwxrwx 1 root root 21 Nov 26 11:32 libevent-1.4.so.2 -> libevent-1.4.so.2.0.0 -rwxr-xr-x 1 root root 267334 Nov 26 11:32 libevent-1.4.so.2.0.0 -rw-r--r-- 1 root root 351694 Nov 26 11:32 libevent.a lrwxrwxrwx 1 root root 26 Nov 26 11:32 libevent_core-1.4.so.2 -> libevent_core-1.4.so.2.0.0 -rwxr-xr-x 1 root root 101485 Nov 26 11:32 libevent_core-1.4.so.2.0.0 -rw-r--r-- 1 root root 139080 Nov 26 11:32 libevent_core.a -rwxr-xr-x 1 root root 866 Nov 26 11:32 libevent_core.la lrwxrwxrwx 1 root root 26 Nov 26 11:32 libevent_core.so -> libevent_core-1.4.so.2.0.0 lrwxrwxrwx 1 root root 27 Nov 26 11:32 libevent_extra-1.4.so.2 -> libevent_extra-1.4.so.2.0.0 -rwxr-xr-x 1 root root 214596 Nov 26 11:32 libevent_extra-1.4.so.2.0.0 -rw-r--r-- 1 root root 273270 Nov 26 11:32 libevent_extra.a -rwxr-xr-x 1 root root 873 Nov 26 11:32 libevent_extra.la lrwxrwxrwx 1 root root 27 Nov 26 11:32 libevent_extra.so -> libevent_extra-1.4.so.2.0.0 -rwxr-xr-x 1 root root 831 Nov 26 11:32 libevent.la lrwxrwxrwx 1 root root 21 Nov 26 11:32 libevent.so -> libevent-1.4.so.2.0.0 |
看见有很多libevent,就可以放心了。
memcached
官方网站:http://www.danga.com/memcached/download.bml
下载地址:http://www.danga.com/memcached/dist/memcached-1.2.2.tar.gz
memcached安装需要些参数:
以下是代码片段: ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local make make install |
我们来运行看看:
运行:
以下是代码片段: /usr/local/memcached/bin/memcached -d -m 10 -p 11211 -u root |
结果:
以下是引用片段: [root@localhost memcached-1.2.2]# /usr/local/memcached/bin/memcached -d -m 10 -p 11211 -u root /usr/local/memcached/bin/memcached: error while loading shared libraries: libevent-1.4.so.2: cannot open shared object file: No such file or directory |
查找原因,解决方案如下:
以下是代码片段: LD_DEBUG=libs /usr/local/memcached/bin/memcached -v |
有如下显示:
以下是引用片段: [root@localhost memcached-1.2.2]# LD_DEBUG=libs /usr/local/memcached/bin/memcached -v 18999: find library=libevent-1.4.so.2 [0]; searching 18999: search cache=/etc/ld.so.cache 18999: search path=/lib/tls/i686/sse2:/lib/tls/i686:/lib/tls/sse2:/lib/tls:/lib/i686/sse2:/lib/i686:/lib/sse2:/lib:/usr/lib/tls/i686/sse2:/usr/lib/tls/i686:/usr/lib/tls/sse2:/usr/lib/tls:/usr/lib/i686/sse2:/usr/lib/i686:/usr/lib/sse2:/usr/lib (system search path) 18999: trying file=/lib/tls/i686/sse2/libevent-1.4.so.2 18999: trying file=/lib/tls/i686/libevent-1.4.so.2 18999: trying file=/lib/tls/sse2/libevent-1.4.so.2 18999: trying file=/lib/tls/libevent-1.4.so.2 18999: trying file=/lib/i686/sse2/libevent-1.4.so.2 18999: trying file=/lib/i686/libevent-1.4.so.2 18999: trying file=/lib/sse2/libevent-1.4.so.2 18999: trying file=/lib/libevent-1.4.so.2 18999: trying file=/usr/lib/tls/i686/sse2/libevent-1.4.so.2 18999: trying file=/usr/lib/tls/i686/libevent-1.4.so.2 18999: trying file=/usr/lib/tls/sse2/libevent-1.4.so.2 18999: trying file=/usr/lib/tls/libevent-1.4.so.2 18999: trying file=/usr/lib/i686/sse2/libevent-1.4.so.2 18999: trying file=/usr/lib/i686/libevent-1.4.so.2 18999: trying file=/usr/lib/sse2/libevent-1.4.so.2 18999: trying file=/usr/lib/libevent-1.4.so.2 18999: /usr/local/memcached/bin/memcached: error while loading shared libraries: libevent-1.4.so.2: cannot open shared object file: No such file or directory |
主要是看最后一行提示的错误,那么我们:
以下是代码片段: ln -s /usr/local/lib/libevent-1.4.so.2 /usr/lib/libevent-1.4.so.2 |
OK!再次运行:
以下是代码片段: /usr/local/memcached/bin/memcached -d -m 10 -p 11211 -u root |
貌似用“ldconfig”也可以解决这个问题
libmemcache
libmemcache是memcached C的客户端之一。我准备使用这个玩意。
官网:http://people.freebsd.org/~seanc/libmemcache/
下载:http://people.freebsd.org/~seanc/libmemcache/libmemcache-1.4.0.rc2.tar.bz2
编译:
以下是代码片段: ./configure make make install |
写一个有libmemcache函数的程序
然后
以下是代码片段: gcc a.c -o a.cgi -L/usr/local/include -lmemcache |
运行
以下是代码片段: ./a.cgi |
提示类似上面的“error while loading shared libraries”的错误,查了一下可用"ldconfig"解决这个问题,那上面的可否用这个命令解决呢?
上面的gcc编译的时候,带有参数“-lmemcache”。
-l参数后面跟的库名有规则的,库的命名方式有libxxxx.so或libxxxx.a,编译时要用-lxxxx就可以了。
而“-l”的意思就是代表是个“lib”了。
所以在你自己编写动态库或者静态库时,命名还是得按照 libxxxx.so的形式来。
不得不认为libmemcache有点糟糕,编译它自带的一个程序编译竟然有问题,很无语!
以下是引用片段: [root@login benchmark]# gcc benchmark.c -L/usr/local/include -lmemcache benchmark.c: In function `main': benchmark.c:100: warning: passing arg 2 of `mc_set' discards qualifiers from pointer target type benchmark.c:108: warning: passing arg 2 of `mc_set' discards qualifiers from pointer target type benchmark.c:121: warning: passing arg 2 of `mc_req_add' discards qualifiers from pointer target type benchmark.c:145: warning: passing arg 2 of `mc_delete' discards qualifiers from pointer target type benchmark.c:153: warning: passing arg 2 of `mc_add' discards qualifiers from pointer target type benchmark.c:167: warning: passing arg 2 of `mc_delete' discards qualifiers from pointer target type |
解决方法:
找到passing arg 2这个参数的定义:
以下是引用片段: const char *key; |
把const去了:
以下是引用片段: char *key; |
这样就好了!
然后:
以下是代码片段: [root@login memcache-test]# ./a.out |
执行时可能报以下的错误:
以下是引用片段: ./a.out: error while loading shared libraries: libmemcache.so.0: cannot open shared object file: No such file or directory |
解决方法:
执行ldconfig命令即可!
附:
http://kapoc.blogdriver.com/kapoc/1200549.html
ldconfig命令 - - 动态链接库管理命令
为了让动态链接库为系统所共享,还需运行动态链接库的管理命令--ldconfig.此执行程序存放在/sbin目录下.
ldconfig命令的用途,主要是在默认搜寻目录(/lib和/usr/lib)以及动态库配置文件/etc/ld.so.conf内所列的目录下,搜索出可共享的动态链接库(格式如前介绍,lib*.so*),进而创建出动态装入程序(ld.so)所需的连接和缓存文件.缓存文件默认为/etc/ld.so.cache,此文件保存已排好序的动态链接库名字列表.
ldconfig通常在系统启动时运行,而当用户安装了一个新的动态链接库时,就需要手工运行这个命令.