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 `.'
autoreconf: configure.ac: not using Gettext
autoreconf: running: aclocal --force -I m4
autoreconf: configure.ac: tracing
autoreconf: running: libtoolize --copy --force
autoreconf: running: /usr/bin/autoconf --force
autoreconf: running: /usr/bin/autoheader --force
autoreconf: running: automake --add-missing --copy --force-missing
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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM