系统为Linux debian 2.6.32-5-686。这是裸系统,连xwindows都没有。帐户为root,不是的注意一下权限。这里想说明安装过程及出现的问题,故打印的信息较多,以供出现错误的读者对比。不需要这些信息的读者勿喷。
网上流行的安装方法:
sh autogen.sh 或 ./configure
make
make install
注意,sh autogen.sh 或 ./configure 只运行其中一个就可以,功能一样。只是autogen.sh调用autoconf来配置参数,./configure就不多说了。配置libev时,两者打印的信息基本是一样了。如果用./configure,就不用安装autoconf之类的了。
下面以sh autogen.sh的方法来安装。先安装autoconf,因为autogen.sh需要。
root@debian:/home/xzc/cpp/libev-4.15# cat autogen.sh #!/bin/sh autoreconf --install --symlink --force root@debian:/home/xzc/cpp/libev-4.15# apt-get install autoconf Reading package lists... Done Building dependency tree Reading state information... Done The following extra packages will be installed: automake autotools-dev Suggested packages: autoconf2.13 autoconf-archive gnu-standards autoconf-doc libtool gettext The following NEW packages will be installed: autoconf automake autotools-dev 0 upgraded, 3 newly installed, 0 to remove and 0 not upgraded. Need to get 1476 kB of archives. After this operation, 4387 kB of additional disk space will be used. Do you want to continue [Y/n]? y Get:1 http://debian.nctu.edu.tw/debian/ squeeze/main autoconf all 2.67-2 [793 kB] Get:2 http://debian.nctu.edu.tw/debian/ squeeze/main autotools-dev all 20100122.1 [71.9 kB] Get:3 http://debian.nctu.edu.tw/debian/ squeeze/main automake all 1:1.11.1-1+squeeze1 [611 kB] Fetched 1476 kB in 2s (493 kB/s) Selecting previously deselected package autoconf. (Reading database ... 24937 files and directories currently installed.) Unpacking autoconf (from .../autoconf_2.67-2_all.deb) ... Selecting previously deselected package autotools-dev. Unpacking autotools-dev (from .../autotools-dev_20100122.1_all.deb) ... Selecting previously deselected package automake. Unpacking automake (from .../automake_1%3a1.11.1-1+squeeze1_all.deb) ... Processing triggers for man-db ... Processing triggers for install-info ... Setting up autoconf (2.67-2) ... Setting up autotools-dev (20100122.1) ... Setting up automake (1:1.11.1-1+squeeze1) ... update-alternatives: using /usr/bin/automake-1.11 to provide /usr/bin/automake (automake) in auto mode
可以看到,系统还一起安装了automake。现在再运行sh autogen.sh
root@debian:/home/xzc/cpp/libev-4.15# sh autogen.sh configure.ac:21: error: possibly undefined macro: AC_PROG_LIBTOOL If this token and others are legitimate, please use m4_pattern_allow. See the Autoconf documentation. autoreconf: /usr/bin/autoconf failed with exit status: 1
看来还缺了libtool,那就装一个呗
root@debian:/home/xzc/cpp/libev-4.15# apt-get install libtool Reading package lists... Done Building dependency tree Reading state information... Done The following extra packages will be installed: binutils cpp cpp-4.4 gcc gcc-4.4 libc-dev-bin libc6-dev libgmp3c2 libgomp1 libltdl-dev libltdl7 libmpfr4 linux-libc-dev manpages-dev Suggested packages: binutils-doc cpp-doc gcc-4.4-locales gcc-multilib make automake1.9 flex bison gdb gcc-doc gcc-4.4-multilib libmudflap0-4.4-dev gcc-4.4-doc libgcc1-dbg libgomp1-dbg libmudflap0-dbg libcloog-ppl0 libppl-c2 libppl7 glibc-doc libtool-doc gfortran fortran95-compiler gcj The following NEW packages will be installed: binutils cpp cpp-4.4 gcc gcc-4.4 libc-dev-bin libc6-dev libgmp3c2 libgomp1 libltdl-dev libltdl7 libmpfr4 libtool linux-libc-dev manpages-dev 0 upgraded, 15 newly installed, 0 to remove and 26 not upgraded. Need to get 856 kB/19.5 MB of archives. After this operation, 55.8 MB of additional disk space will be used.
现在再来sh autogen.sh
root@debian:/home/xzc/cpp/libev-4.15# sh autogen.sh libtoolize: putting auxiliary files in `.'. libtoolize: linking file `./ltmain.sh' libtoolize: Consider adding `AC_CONFIG_MACRO_DIR([m4])' to configure.ac and libtoolize: rerunning libtoolize, to keep the correct libtool macros in-tree. libtoolize: Consider adding `-I m4' to ACLOCAL_AMFLAGS in Makefile.am. root@debian:/home/xzc/cpp/libev-4.15# ./configure checking for a BSD-compatible install... /usr/bin/install -c checking whether build environment is sane... yes checking for a thread-safe mkdir -p... /bin/mkdir -p checking for gawk... no checking for mawk... mawk checking whether make sets $(MAKE)... no checking whether to enable maintainer-specific portions of Makefiles... no checking for gcc... gcc checking whether the C compiler works... yes checking for C compiler default output file name... a.out checking for suffix of executables... checking whether we are cross compiling... no checking for suffix of object files... o checking whether we are using the GNU C compiler... yes checking whether gcc accepts -g... yes checking for gcc option to accept ISO C89... none needed checking for style of include used by make... none checking dependency style of gcc... none checking build system type... i686-pc-linux-gnu checking host system type... i686-pc-linux-gnu checking for a sed that does not truncate output... /bin/sed checking for grep that handles long lines and -e... /bin/grep checking for egrep... /bin/grep -E checking for fgrep... /bin/grep -F checking for ld used by gcc... /usr/bin/ld checking if the linker (/usr/bin/ld) is GNU ld... yes checking for BSD- or MS-compatible name lister (nm)... /usr/bin/nm -B checking the name lister (/usr/bin/nm -B) interface... BSD nm checking whether ln -s works... yes checking the maximum length of command line arguments... 1572864 checking whether the shell understands some XSI constructs... yes checking whether the shell understands "+="... yes checking for /usr/bin/ld option to reload object files... -r checking for objdump... objdump checking how to recognize dependent libraries... pass_all checking for ar... ar checking for strip... strip checking for ranlib... ranlib checking command to parse /usr/bin/nm -B output from gcc object... ok checking how to run the C preprocessor... gcc -E checking for ANSI C header files... yes checking for sys/types.h... yes checking for sys/stat.h... yes checking for stdlib.h... yes checking for string.h... yes checking for memory.h... yes checking for strings.h... yes checking for inttypes.h... yes checking for stdint.h... yes checking for unistd.h... yes checking for dlfcn.h... yes checking for objdir... .libs checking if gcc supports -fno-rtti -fno-exceptions... no checking for gcc option to produce PIC... -fPIC -DPIC checking if gcc PIC flag -fPIC -DPIC works... yes checking if gcc static flag -static works... yes checking if gcc supports -c -o file.o... yes checking if gcc supports -c -o file.o... (cached) yes checking whether the gcc linker (/usr/bin/ld) supports shared libraries... yes checking whether -lc should be explicitly linked in... no checking dynamic linker characteristics... GNU/Linux ld.so checking how to hardcode library paths into programs... immediate checking whether stripping libraries is possible... yes checking if libtool supports shared libraries... yes checking whether to build shared libraries... yes checking whether to build static libraries... yes checking sys/inotify.h usability... yes checking sys/inotify.h presence... yes checking for sys/inotify.h... yes checking sys/epoll.h usability... yes checking sys/epoll.h presence... yes checking for sys/epoll.h... yes checking sys/event.h usability... no checking sys/event.h presence... no checking for sys/event.h... no checking port.h usability... no checking port.h presence... no checking for port.h... no checking poll.h usability... yes checking poll.h presence... yes checking for poll.h... yes checking sys/select.h usability... yes checking sys/select.h presence... yes checking for sys/select.h... yes checking sys/eventfd.h usability... yes checking sys/eventfd.h presence... yes checking for sys/eventfd.h... yes checking sys/signalfd.h usability... yes checking sys/signalfd.h presence... yes checking for sys/signalfd.h... yes checking for inotify_init... yes checking for epoll_ctl... yes checking for kqueue... no checking for port_create... no checking for poll... yes checking for select... yes checking for eventfd... yes checking for signalfd... yes checking for clock_gettime... no checking for clock_gettime syscall... yes checking for nanosleep... yes checking for library containing floor... -lm configure: creating ./config.status config.status: creating Makefile config.status: creating config.h config.status: executing depfiles commands config.status: executing libtool commands
看来结果OK,然后make,才发现连make都没安装,那就apt-get install make。看一下make的结果
root@debian:/home/xzc/cpp/libev-4.15# make make all-am make[1]: Entering directory `/home/xzc/cpp/libev-4.15' source='ev.c' object='ev.lo' libtool=yes \ DEPDIR=.deps depmode=none /bin/bash ./depcomp \ /bin/bash ./libtool --tag=CC --mode=compile gcc -DHAVE_CONFIG_H -I. -g -O3 -c -o ev.lo ev.c libtool: compile: gcc -DHAVE_CONFIG_H -I. -g -O3 -c ev.c -fPIC -DPIC -o .libs/ev.o ev.c:1531: warning: 'ev_default_loop_ptr' initialized and declared 'extern' libtool: compile: gcc -DHAVE_CONFIG_H -I. -g -O3 -c ev.c -o ev.o >/dev/null 2>&1 source='event.c' object='event.lo' libtool=yes \ DEPDIR=.deps depmode=none /bin/bash ./depcomp \ /bin/bash ./libtool --tag=CC --mode=compile gcc -DHAVE_CONFIG_H -I. -g -O3 -c -o event.lo event.c libtool: compile: gcc -DHAVE_CONFIG_H -I. -g -O3 -c event.c -fPIC -DPIC -o .libs/event.o libtool: compile: gcc -DHAVE_CONFIG_H -I. -g -O3 -c event.c -o event.o >/dev/null 2>&1 /bin/bash ./libtool --tag=CC --mode=link gcc -g -O3 -version-info 4:0:0 -o libev.la -rpath /usr/local/lib ev.lo event.lo -lm libtool: link: gcc -shared .libs/ev.o .libs/event.o -lm -Wl,-soname -Wl,libev.so.4 -o .libs/libev.so.4.0.0 libtool: link: (cd ".libs" && rm -f "libev.so.4" && ln -s "libev.so.4.0.0" "libev.so.4") libtool: link: (cd ".libs" && rm -f "libev.so" && ln -s "libev.so.4.0.0" "libev.so") libtool: link: ar cru .libs/libev.a ev.o event.o libtool: link: ranlib .libs/libev.a libtool: link: ( cd ".libs" && rm -f "libev.la" && ln -s "../libev.la" "libev.la" )
现在看看make install的结果
root@debian:/home/xzc/cpp/libev-4.15# make install make[1]: Entering directory `/home/xzc/cpp/libev-4.15' test -z "/usr/local/lib" || /bin/mkdir -p "/usr/local/lib" /bin/bash ./libtool --mode=install /usr/bin/install -c libev.la '/usr/local/lib' libtool: install: /usr/bin/install -c .libs/libev.so.4.0.0 /usr/local/lib/libev.so.4.0.0 libtool: install: (cd /usr/local/lib && { ln -s -f libev.so.4.0.0 libev.so.4 || { rm -f libev.so.4 && ln -s libev.so.4.0.0 libev.so.4; }; }) libtool: install: (cd /usr/local/lib && { ln -s -f libev.so.4.0.0 libev.so || { rm -f libev.so && ln -s libev.so.4.0.0 libev.so; }; }) libtool: install: /usr/bin/install -c .libs/libev.lai /usr/local/lib/libev.la libtool: install: /usr/bin/install -c .libs/libev.a /usr/local/lib/libev.a libtool: install: chmod 644 /usr/local/lib/libev.a libtool: install: ranlib /usr/local/lib/libev.a libtool: finish: PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/sbin" ldconfig -n /usr/local/lib ---------------------------------------------------------------------- Libraries have been installed in: /usr/local/lib If you ever happen to want to link against installed libraries in a given directory, LIBDIR, you must either use libtool, and specify the full pathname of the library, or use the `-LLIBDIR' flag during linking and do at least one of the following: - add LIBDIR to the `LD_LIBRARY_PATH' environment variable during execution - add LIBDIR to the `LD_RUN_PATH' environment variable during linking - use the `-Wl,-rpath -Wl,LIBDIR' linker flag - have your system administrator add LIBDIR to `/etc/ld.so.conf' See any operating system documentation about shared libraries for more information, such as the ld(1) and ld.so(8) manual pages. ---------------------------------------------------------------------- test -z "/usr/local/include" || /bin/mkdir -p "/usr/local/include" /usr/bin/install -c -m 644 ev.h ev++.h event.h '/usr/local/include' test -z "/usr/local/share/man/man3" || /bin/mkdir -p "/usr/local/share/man/man3" /usr/bin/install -c -m 644 ev.3 '/usr/local/share/man/man3' make[1]: Leaving directory `/home/xzc/cpp/libev-4.15'
注意,里面还是有些有用的信息的,如
Libraries have been installed in: /usr/local/lib
写了一段代码来测试
#include <ev.h> #include <stdio.h> ev_io stdin_watcher; ev_timer timeout_watcher; static void stdin_cb(EV_P_ ev_io *w,int revents) { puts ("stdin ready"); ev_io_stop(EV_A_ w); ev_break (EV_A_ EVBREAK_ALL); } static void timeout_cb (EV_P_ ev_timer *w,int revents) { puts ("timeout"); ev_break( EV_A_ EVBREAK_ONE ); } int main(int argc,char* argv[]) { struct ev_loop *loop = EV_DEFAULT; ev_io_init(&stdin_watcher,stdin_cb,0,EV_READ); ev_io_start (loop,&stdin_watcher); ev_timer_init (&timeout_watcher,timeout_cb,5.5,0); // in document the last is 0. not 0,I don't know why yet ev_timer_start (loop,&timeout_watcher); ev_run (loop,0); return 0; } output: root@debian:/home/xzc/cpp/test# gcc -o libev_hello libev_hello.c /tmp/ccNN2QkE.o: In function `stdin_cb': libev_hello.c:(.text+0x20): undefined reference to `ev_io_stop' libev_hello.c:(.text+0x33): undefined reference to `ev_break' /tmp/ccNN2QkE.o: In function `timeout_cb': libev_hello.c:(.text+0x5a): undefined reference to `ev_break' /tmp/ccNN2QkE.o: In function `main': libev_hello.c:(.text+0x71): undefined reference to `ev_default_loop' libev_hello.c:(.text+0xc9): undefined reference to `ev_io_start' libev_hello.c:(.text+0x11f): undefined reference to `ev_timer_start' libev_hello.c:(.text+0x133): undefined reference to `ev_run' collect2: ld returned 1 exit status
试一下只编译不连接gcc -c -o libev_hello libev_hello.c,没有错误,看来是link时没有找到库
尝试连接时指定需要libev库,gcc -o libev_hello libev_hello.c -lev,没有问题。但:
root@debian:/home/xzc/cpp/test# gcc -o libev_hello libev_hello.c -lev root@debian:/home/xzc/cpp/test# ./libev_hello ./libev_hello: error while loading shared libraries: libev.so.4: cannot open shared object file: No such file or directory
看来编译链接时指定了,可运行时没呢。另外,为什么要加-lev呢,参考gcc的参数-llibrary
-llibrary
制定编译的时候使用的库
例子用法
gcc -lcurses hello.c
使用ncurses库编译程序
所以-lev中-l是参数类型,ev才是libev的库名称,不信看文件命名:ev.h ev++.h
查看/usr/local/lib下的内容:
root@debian:/usr/local/lib# ls -lh total 368K -rw-r--r-- 1 root staff 213K May 10 00:23 libev.a -rwxr-xr-x 1 root staff 926 May 10 00:23 libev.la lrwxrwxrwx 1 root staff 14 May 10 00:23 libev.so -> libev.so.4.0.0 lrwxrwxrwx 1 root staff 14 May 10 00:23 libev.so.4 -> libev.so.4.0.0 -rwxr-xr-x 1 root staff 133K May 10 00:23 libev.so.4.0.0 drwxrwsr-x 4 root staff 4.0K Sep 10 2013 python2.6
解决运行的问题,仔细看make install时产生的信息:
If you ever happen to want to link against installed libraries in a given directory, LIBDIR, you must either use libtool, and specify the full pathname of the library, or use the `-LLIBDIR' flag during linking and do at least one of the following: - add LIBDIR to the `LD_LIBRARY_PATH' environment variable during execution - add LIBDIR to the `LD_RUN_PATH' environment variable during linking - use the `-Wl,-rpath -Wl,LIBDIR' linker flag - have your system administrator add LIBDIR to `/etc/ld.so.conf'
有N多种呢,下面是一种
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib 写到~/.bash_profile里面,就不用每次都要export了。
gcc -o libev_hello libev_hello.c -lev
./libev_hello
运行OK