一、autotools的安装步骤
环境是Ubuntu 16.04版本,Ubuntu安装工具十分方便,用apt命令即可。
sudo apt-get install autoconf
安装完成之后,使用which命令查看是否安装成功。因为autotools是个系列工具,安装包相互直接存在依赖
root@ubuntu:/# which aclocal
/usr/bin/aclocal
root@ubuntu:/# which autoscan
/usr/bin/autoscan
root@ubuntu:/# which autoconf
/usr/bin/autoconf
root@ubuntu:/# which autoheader
/usr/bin/autoheader
root@ubuntu:/# which automake
/usr/bin/automake
这样就安装完成了,接下来讲讲怎么使用autotools
二、autotools的使用
接下来我们写个测试代码吧!
安装tree命令
sudo apt-get install tree
使用tree命令查看程序目录结构
root@ubuntu:/opt/gurbly/pmu# tree
.
├── aclocal.m4
├── AUTHORS
├── autom4te.cache
│ ├── output.0
│ ├── output.1
│ ├── requests
│ ├── traces.0
│ └── traces.1
├── autoscan.log
├── ChangeLog
├── client.c
├── client.c.bak
├── client.h
├── compile -> /usr/share/automake-1.15/compile
├── config.h
├── config.h.in
├── config.log
├── config.status
├── configure
├── configure.ac
├── configure.scan
├── crc.c
├── crc.h
├── depcomp -> /usr/share/automake-1.15/depcomp
├── history.c
├── history.c.bak
├── history.h
├── init.c
├── init.c.bak
├── init.h
├── install-sh -> /usr/share/automake-1.15/install-sh
├── main.c
├── main.c.bak
├── main-debug
├── Makefile
├── Makefile1
├── Makefile.am
├── Makefile.in
├── missing -> /usr/share/automake-1.15/missing
├── NEWS
├── pmuConfig.h
├── README
├── serial.c
├── serial.c.bak
├── serial.h
├── server.c
├── sharemem.c
├── sharemem.h
├── stamp-h1
├── system.c
├── system.c.bak
└── system.h
三、生成Makefile文件
1、使用autoscan命令生成autoscan.log configure.scan两个文件。
root@ubuntu:/opt/gurbly/pmu# autoscan
root@ubuntu:/opt/gurbly/pmu# ls
autoscan.log configure.scan
2、使用cp命令生成configure.ac文件。
root@ubuntu:/opt/gurbly/pmu# cp configure.scan configure.ac
root@ubuntu:/opt/gurbly/pmu# ls
autoscan.log configure.ac configure.scan
3、修改configure.ac文件。
找到这句 AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
其中,FULL-PACKAGE-NAME为程序名称,VERSION为当前版本,BUG-REPORT-ADDRESS为bug汇报地址。
AC_INIT(Main, 1.0.1, xxx@xx.com)
然后在AC_INIT()后面添加一句AM_INIT_AUTOMAKE
root@ubuntu:/opt/gurbly/pmu# gedit configure.ac
4、执行aclocal命令生成aclocal.m4和autom4te.cache文件。
root@ubuntu:/opt/gurbly/pmu# aclocal
root@ubuntu:/opt/gurbly/pmu# ls
aclocal.m4 autoscan.log configure.scan autom4te.cache configure.ac
5、制作Makefile.am文件,因为automake这个命令需要用到这个配置文件。
root@ubuntu:/opt/gurbly/pmu# gedit Makefile.am
AUTOMAKE_OPTIONS = foreign
bin_PROGRAMS = main
main_SOURCES = config.h client.c crc.c history.c init.c main.c serial.c sharemem.c system.c client.h crc.h history.h init.h main.h serial.h sharemem.h system.h
6、执行autoheader命令生成config.h.in文件。
root@ubuntu:/opt/gurbly/pmu# autoheader
root@ubuntu:/opt/gurbly/pmu# ls -l
-rw-r--r-- 1 root root 2731 1月 3 03:58 config.h.in
7、使用touch命令生成automake必须文件。
automake必须文件:
install-sh、missing、INSTALL、NEWS、README、AUTHORS、ChangeLog、COPYING、depcomp
执行automake -a的时候会自动生成
install-sh、missing、INSTALL、COPYING、depcomp
其它文件就需要自己手动添加了。可以使用touch命
root@ubuntu:/opt/gurbly/pmu# touch NEWS README AUTHORS ChangeLog
8、执行automake -a命令生成其它文件。
root@ubuntu:/opt/gurbly/pmu# automake -a
9、执行autoconf命令生成configure文件。
root@ubuntu:/opt/gurbly/pmu# autoconf
10、执行./configure命令进行测试。
root@ubuntu:/opt/gurbly/pmu# ./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)... yes
checking whether make supports nested variables... yes
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 whether gcc understands -c and -o together... yes
checking for style of include used by make... GNU
checking dependency style of gcc... gcc3
checking for main in -lpthread... yes
checking how to run the C preprocessor... gcc -E
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -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 fcntl.h usability... yes
checking fcntl.h presence... yes
checking for fcntl.h... yes
checking netdb.h usability... yes
checking netdb.h presence... yes
checking for netdb.h... yes
checking netinet/in.h usability... yes
checking netinet/in.h presence... yes
checking for netinet/in.h... yes
checking for stdlib.h... (cached) yes
checking for string.h... (cached) yes
checking sys/ioctl.h usability... yes
checking sys/ioctl.h presence... yes
checking for sys/ioctl.h... yes
checking sys/socket.h usability... yes
checking sys/socket.h presence... yes
checking for sys/socket.h... yes
checking sys/time.h usability... yes
checking sys/time.h presence... yes
checking for sys/time.h... yes
checking termios.h usability... yes
checking termios.h presence... yes
checking for termios.h... yes
checking for unistd.h... (cached) yes
checking for size_t... yes
checking for bzero... yes
checking for gethostbyname... yes
checking for gettimeofday... yes
checking for inet_ntoa... yes
checking for memset... yes
checking for select... yes
checking for socket... yes
checking that generated files are newer than configure... done
configure: creating ./config.status
config.status: creating Makefile
config.status: creating config.h
config.status: config.h is unchanged
config.status: executing depfiles commands
root@ubuntu:/opt/gurbly/pmu#
11、执行make命令生成可执行文件。
root@ubuntu:/opt/gurbly/pmu# make
mv -f .deps/main.Tpo .deps/main.Po
gcc -DHAVE_CONFIG_H -I. -g -O2 -MT serial.o -MD -MP -MF .deps/serial.Tpo -c -o serial.o serial.c
mv -f .deps/serial.Tpo .deps/serial.Po
gcc -DHAVE_CONFIG_H -I. -g -O2 -MT sharemem.o -MD -MP -MF .deps/sharemem.Tpo -c -o sharemem.o sharemem.c
mv -f .deps/sharemem.Tpo .deps/sharemem.Po
gcc -DHAVE_CONFIG_H -I. -g -O2 -MT system.o -MD -MP -MF .deps/system.Tpo -c -o system.o system.c
mv -f .deps/system.Tpo .deps/system.Po
gcc -g -O2 -o main client.o crc.o history.o init.o main.o serial.o sharemem.o system.o -lpthread
make[1]: Leaving directory '/opt/gurbly/pmu'