libevent源码安装及Linux自动编译功能总结

这个。。那个。。后来发现。。直接用jumbo就可以安装libevent。不过,学习一些automake的知识还是有好处的。

03机器也安装了。

这几天在阅读libevent源码,发现参考资料是基于libevent-2.1的版本,所以就去官网下载了2.1的版本:

http://libevent.org/ (其实是在git下载的:https://github.com/libevent/libevent),版本应该是libevent-2.1.so.5.0.0

下载下来,发现目录里面没有常见的configure, Makefile等文件,搜索之后,了解到需要使用autoconf, automake等工具进行处理。

首先针对automake等工具进行了学习(目录在/home/work/my_name/autoMake/):

先写了些示例代码,分为两个文件:

helloworld.cpp

#include "helloworld.h"

using namespace std;

int main(int argc, char **argv) {
        printf("Hello Linux!\n");
        return 0;
}

以及helloworld.h

#include <iostream>

autoconf需要configure.ac文件,可以使用autoscan命令先生成configure.scan(错误信息可以先忽略)

[autoMake]$ autoscan 
autom4te: configure.ac: no such file or directory
autoscan: /usr/bin/autom4te failed with exit status: 1
[autoMake]$ ll
-rw-rw-r--  1 work work    0 May  3 17:13 autoscan.log
-rw-rw-r--  1 work work  559 May  3 17:13 configure.scan
-rw-rw-r--  1 work work  121 May  3 17:13 helloworld.cpp
-rw-rw-r--  1 work work   20 May  3 17:13 helloworld.h

configure.scan原始内容如下:

-- Autoconf --

# Process this file with autoconf to produce a configure script.

AC_PREREQ(2.59)

AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)

AC_CONFIG_SRCDIR([helloworld.cpp])

AC_CONFIG_HEADER([config.h])

Checks for programs.

AC_PROG_CXX

AC_PROG_CC

Checks for libraries.

Checks for header files.

Checks for typedefs, structures, and compiler characteristics.

Checks for library functions.

AC_OUTPUT

将configure.scan改名为configure.in(configure.ac亦可),将内容修改如下:

#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ(2.59)
#AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)
#modified, ignore 3rd parameter email_address
AC_INIT(helloworld, 1.0)
AC_CONFIG_SRCDIR([helloworld.cpp])
#AC_CONFIG_HEADER([config.h])
#added
AM_INIT_AUTOMAKE(helloworld, 1.0)

# Checks for programs.
AC_PROG_CXX

# Checks for libraries.

# Checks for header files.

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.

#added
AC_CONFIG_FILES([Makefile])

AC_OUTPUT

直接运行autoconf,会有如下报错(但是configure文件仍然生成)

[autoMake]$ autoconf
configure.in:11: error: possibly undefined macro: AM_INIT_AUTOMAKE
      If this token and others are legitimate, please use m4_pattern_allow.
      See the Autoconf documentation.
[utoMake]$ ll
total 104
drwxr-xr-x  2 work work  4096 May  3 17:26 autom4te.cache
-rw-rw-r--  1 work work     0 May  3 17:24 autoscan.log
-rwxrwxr-x  1 work work 89791 May  3 17:26 configure
-rw-rw-r--  1 work work   615 May  3 17:25 configure.in
-rw-rw-r--  1 work work   121 May  3 17:13 helloworld.cpp
-rw-rw-r--  1 work work    20 May  3 17:13 helloworld.h

这个报错是因为没有运行命令aclocal来生成宏(会生成文件aclocal.m4)。删除刚刚生成的文件,重新运行如下:

[autoMake]$ aclocal
[autoMake]$ autoconf
[autoMake]$ ll
total 176
-rw-rw-r--  1 work work  38499 May  3 17:29 aclocal.m4
drwxr-xr-x  2 work work   4096 May  3 17:29 autom4te.cache
-rw-rw-r--  1 work work      0 May  3 17:24 autoscan.log
-rwxrwxr-x  1 work work 119798 May  3 17:29 configure
-rw-rw-r--  1 work work    615 May  3 17:25 configure.in
-rw-rw-r--  1 work work    121 May  3 17:13 helloworld.cpp
-rw-rw-r--  1 work work     20 May  3 17:13 helloworld.h

在下一步automake前,需要新建文件Makefile.am,内容如下:

AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS=helloworld
helloworld_SOURCES=helloworld.cpp helloworld.h

automake提供了三种软件等级:foreign、gnu和gnits,这里选择foreign,就会只检查必须文件。

再之后运行命令:automake --add-missing(选项--add-missing的定义是“add missing standard files to package”,它会让automake加入一个标准的软件包所必须的一些文件。)

[autoMake]$ automake --add-missing
configure.in: installing `./install-sh'
configure.in: installing `./missing'
Makefile.am: installing `./depcomp'
[autoMake]$ ll
total 200
-rw-rw-r--  1 work work  38499 May  3 17:29 aclocal.m4
drwxr-xr-x  2 work work   4096 May  3 18:07 autom4te.cache
-rw-rw-r--  1 work work      0 May  3 17:24 autoscan.log
-rwxrwxr-x  1 work work 119798 May  3 17:29 configure
-rw-rw-r--  1 work work    616 May  3 18:07 configure.in
lrwxrwxrwx  1 work work     31 May  3 18:08 depcomp -> /usr/share/automake-1.9/depcomp
-rw-rw-r--  1 work work    121 May  3 17:13 helloworld.cpp
-rw-rw-r--  1 work work     20 May  3 17:13 helloworld.h
lrwxrwxrwx  1 work work     34 May  3 18:08 install-sh -> /usr/share/automake-1.9/install-sh
-rw-rw-r--  1 work work     96 May  3 17:35 Makefile.am
-rw-rw-r--  1 work work  16964 May  3 18:08 Makefile.in
lrwxrwxrwx  1 work work     31 May  3 18:08 missing -> /usr/share/automake-1.9/missing

可以发现,Makefile.in文件被生成了。再之后,就可以用configure命令了:

[autoMake]$ ./configure 
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking for g++... g++
checking for C++ compiler default output file name... a.out
checking whether the C++ compiler works... yes
checking whether we are cross compiling... no
checking for suffix of executables... 
checking for suffix of object files... o
checking whether we are using the GNU C++ compiler... yes
checking whether g++ accepts -g... yes
checking for style of include used by make... GNU
checking dependency style of g++... gcc3
configure: creating ./config.status
config.status: creating Makefile
config.status: creating config.h
config.status: error: cannot find input file: config.h.in

可以看到Makefile文件被生成:

[autoMake]$ ll
total 268
-rw-rw-r--  1 work work  38499 May  3 17:29 aclocal.m4
drwxr-xr-x  2 work work   4096 May  3 18:07 autom4te.cache
-rw-rw-r--  1 work work      0 May  3 17:24 autoscan.log
-rw-rw-r--  1 work work   8360 May  3 18:09 config.log
-rwxrwxr-x  1 work work  35215 May  3 18:09 config.status
-rwxrwxr-x  1 work work 119798 May  3 17:29 configure
-rw-rw-r--  1 work work    616 May  3 18:07 configure.in
lrwxrwxrwx  1 work work     31 May  3 18:08 depcomp -> /usr/share/automake-1.9/depcomp
-rw-rw-r--  1 work work    121 May  3 17:13 helloworld.cpp
-rw-rw-r--  1 work work     20 May  3 17:13 helloworld.h
lrwxrwxrwx  1 work work     34 May  3 18:08 install-sh -> /usr/share/automake-1.9/install-sh
-rw-rw-r--  1 work work  16660 May  3 18:09 Makefile
-rw-rw-r--  1 work work     96 May  3 17:35 Makefile.am
-rw-rw-r--  1 work work  16964 May  3 18:08 Makefile.in
lrwxrwxrwx  1 work work     31 May  3 18:08 missing -> /usr/share/automake-1.9/missing

再之后,通过make,就能生成可执行文件:

[autoMake]$ make
cd . && /bin/sh /home/work/liu_chao/autoMake/missing --run aclocal-1.9 
 cd . && /bin/sh /home/work/liu_chao/autoMake/missing --run automake-1.9 --foreign 
cd . && /bin/sh /home/work/liu_chao/autoMake/missing --run autoconf
/bin/sh ./config.status --recheck
running /bin/sh ./configure   --no-create --no-recursion
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking for g++... g++
checking for C++ compiler default output file name... a.out
checking whether the C++ compiler works... yes
checking whether we are cross compiling... no
checking for suffix of executables... 
checking for suffix of object files... o
checking whether we are using the GNU C++ compiler... yes
checking whether g++ accepts -g... yes
checking for style of include used by make... GNU
checking dependency style of g++... gcc3
configure: creating ./config.status
 /bin/sh ./config.status
config.status: creating Makefile
config.status: executing depfiles commands
if g++ -DPACKAGE_NAME=\"helloworld\" -DPACKAGE_TARNAME=\"helloworld\" -DPACKAGE_VERSION=\"1.0\" -DPACKAGE_STRING=\"helloworld\ 1.0\" -DPACKAGE_BUGREPORT=\"\" -DPACKAGE=\"helloworld\" -DVERSION=\"1.0\"  -I. -I.     -g -O2 -MT helloworld.o -MD -MP -MF ".deps/helloworld.Tpo" -c -o helloworld.o helloworld.cpp; \
then mv -f ".deps/helloworld.Tpo" ".deps/helloworld.Po"; else rm -f ".deps/helloworld.Tpo"; exit 1; fi
g++  -g -O2   -o helloworld  helloworld.o  
[autoMake]$ ll
total 404
-rw-rw-r--  1 work work  38499 May  3 18:10 aclocal.m4
drwxr-xr-x  2 work work   4096 May  3 18:10 autom4te.cache
-rw-rw-r--  1 work work      0 May  3 17:24 autoscan.log
-rw-rw-r--  1 work work   8513 May  3 18:10 config.log
-rwxrwxr-x  1 work work  28906 May  3 18:10 config.status
-rwxrwxr-x  1 work work 112022 May  3 18:10 configure
-rw-rw-r--  1 work work    616 May  3 18:07 configure.in
lrwxrwxrwx  1 work work     31 May  3 18:08 depcomp -> /usr/share/automake-1.9/depcomp
-rwxrwxr-x  1 work work  57820 May  3 18:10 helloworld
-rw-rw-r--  1 work work    121 May  3 17:13 helloworld.cpp
-rw-rw-r--  1 work work     20 May  3 17:13 helloworld.h
-rw-rw-r--  1 work work  87160 May  3 18:10 helloworld.o
lrwxrwxrwx  1 work work     34 May  3 18:08 install-sh -> /usr/share/automake-1.9/install-sh
-rw-rw-r--  1 work work  16839 May  3 18:10 Makefile
-rw-rw-r--  1 work work     96 May  3 17:35 Makefile.am
-rw-rw-r--  1 work work  16964 May  3 18:10 Makefile.in
lrwxrwxrwx  1 work work     31 May  3 18:08 missing -> /usr/share/automake-1.9/missing

make install是将可执行文件安装到系统目录中。这里不需要这一步。

可以通过make dist来生成安装包:

[autoMake]$ make dist
{ test ! -d helloworld-1.0 || { find helloworld-1.0 -type d ! -perm -200 -exec chmod u+w {} ';' && rm -fr helloworld-1.0; }; }
mkdir helloworld-1.0
find helloworld-1.0 -type d ! -perm -777 -exec chmod a+rwx {} \; -o \
  ! -type d ! -perm -444 -links 1 -exec chmod a+r {} \; -o \
  ! -type d ! -perm -400 -exec chmod a+r {} \; -o \
  ! -type d ! -perm -444 -exec /bin/sh /home/work/liu_chao/autoMake/install-sh -c -m a+r {} {} \; \
|| chmod -R a+r helloworld-1.0
tardir=helloworld-1.0 && /bin/sh /home/work/liu_chao/autoMake/missing --run tar chof - "$tardir" | GZIP=--best gzip -c >helloworld-1.0.tar.gz
{ test ! -d helloworld-1.0 || { find helloworld-1.0 -type d ! -perm -200 -exec chmod u+w {} ';' && rm -fr helloworld-1.0; }; }
[autoMake]$ ll
total 460
-rw-rw-r--  1 work work  38499 May  3 18:10 aclocal.m4
drwxr-xr-x  2 work work   4096 May  3 18:10 autom4te.cache
-rw-rw-r--  1 work work      0 May  3 17:24 autoscan.log
-rw-rw-r--  1 work work   8513 May  3 18:10 config.log
-rwxrwxr-x  1 work work  28906 May  3 18:10 config.status
-rwxrwxr-x  1 work work 112022 May  3 18:10 configure
-rw-rw-r--  1 work work    616 May  3 18:07 configure.in
lrwxrwxrwx  1 work work     31 May  3 18:08 depcomp -> /usr/share/automake-1.9/depcomp
-rwxrwxr-x  1 work work  57820 May  3 18:10 helloworld
-rw-rw-r--  1 work work  55726 May  3 18:12 helloworld-1.0.tar.gz
-rw-rw-r--  1 work work    121 May  3 17:13 helloworld.cpp
-rw-rw-r--  1 work work     20 May  3 17:13 helloworld.h
-rw-rw-r--  1 work work  87160 May  3 18:10 helloworld.o
lrwxrwxrwx  1 work work     34 May  3 18:08 install-sh -> /usr/share/automake-1.9/install-sh
-rw-rw-r--  1 work work  16839 May  3 18:10 Makefile
-rw-rw-r--  1 work work     96 May  3 17:35 Makefile.am
-rw-rw-r--  1 work work  16964 May  3 18:10 Makefile.in
lrwxrwxrwx  1 work work     31 May  3 18:08 missing -> /usr/share/automake-1.9/missing

可以看到,生成了 helloworld-1.0.tar.gz 这个文件。

到此,autoconf, automake, configure,和make等常用软件编译和安装的方式已经总结完成。

————————

下面进行libevent的编译,使用命令:

aclocal

autoconf

autoheader

automake --add-missing

出现

configure.ac:125: required file `./ltmain.sh' not found

搜索之后,使用:

libevent-master]$ libtoolize --version

libtoolize (GNU libtool) 1.5.6

Copyright (C) 2003 Free Software Foundation, Inc.

This is free software; see the source for copying conditions. There is NO

warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

libevent-master]$ libtoolize --automake --copy --debug --force

然后重新输入命令:

automake --add-missing

发现没有ltmain.sh相关的错误,但是返回码仍然是1,并且无Makefile生成。

——————————————

重新阅读目录自带的README.md,发现:

1. BUILDING AND INSTALLATION (In Depth)

Autoconf

To build libevent, type

$ ./configure && make


(If you got libevent from the git repository, you will

first need to run the included "autogen.sh" script in order to

generate the configure script.)

所以运行autogen.sh命令:

libevent-master]$ sh autogen.sh

autoreconf: Entering directory .'<br></br>autoreconf: configure.ac: not using Gettext<br></br>autoreconf: running: aclocal --force -I m4<br></br>autoreconf: configure.ac: tracing<br></br>autoreconf: running: libtoolize --copy --force<br></br>autoreconf: running: /usr/bin/autoconf --force<br></br>autoreconf: running: /usr/bin/autoheader --force<br></br>autoreconf: running: automake --add-missing --copy --force-missing<br></br>autoreconf: Leaving directory.'

最后返回码为0,并且成功生成Makefile.in(最终的Makefile需要通过 ./configure来生成)

再之后就是下面的命令:

./configure (注意,2.1的libevent默认安装在/usr/local/lib里,需要--prefix=/usr让这个库安装到/usr/lib)

make

make install(可能需要root权限)

——————————————————

关于安装目录,查到的资料是,加载动态库的顺序是:

  1. 编译的时候加上-Wl,-rpath=目录的选项,就会在目录程序的.dynamic 段包含一个叫DT_RPATH的项,里面有冒号分隔的目录。

  2. 先根据LD_LIBRARY_PATH

  3. 再看/etc/ld.so.conf

  4. 最后依次在 /lib /usr/lib里查找

开始的时候,我没有注意看编译定义的文件,后来看到的确把/usr/local目录包含了进去,看来使用默认目录/usr/local应该也没有问题。

编译的命令可以参考:

g++ -o lserver lserver.cpp -I/usr/local/include -levent -L/usr/local/lib -Wl,-rpath=/usr/local/lib
原文链接: https://www.cnblogs.com/charlesblc/p/5452749.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/232855

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月13日 下午3:38
下一篇 2023年2月13日 下午3:39

相关推荐