自动生成Makefile

Step:

1:   How create "configure" of file.

 

我们使用autoscan命令来帮助我们根据目录下的源代码生成一个configure.ac的模板文件。

 

命令:

 

$ autoscan

$ ls

configure.scanhelloworld.c

 

 

现在将configure.scan改名为configure.ac,并且编辑它,按下面的内容修改,去掉无关的语句:

 

============================configure.in内容开始=========================================

# -*- Autoconf -*-

# Process this filewith autoconf to produce a configure script.

 

AC_INIT(helloworld.c)

AM_INIT_AUTOMAKE(helloworld,1.0)

 

# Checks forprograms.

AC_PROG_CC

 

# Checks forlibraries.

 

# Checks for headerfiles.

 

# Checks fortypedefs, structures, and compiler characteristics.

 

# Checks for libraryfunctions.

AC_OUTPUT(Makefile)

============================configure.in内容结束=========================================

 

 

 

 

然后执行命令aclocalautoconf,分别会产生aclocal.m4configure两个文件:

 

$ aclocal

$ls

aclocal.m4configure.in helloworld.c

$ autoconf

$ ls

aclocal.m4autom4te.cache configure configure.in helloworld.c

 

 

2: How create"Makefile" of file.

 

新建Makefile.am文件,命令:

 

 

$ vi Makefile.am

 

内容如下:

 

 

AUTOMAKE_OPTIONS=foreign

bin_PROGRAMS=helloworld

helloworld_SOURCES=helloworld.c

 

 

automake会根据你写的Makefile.am来自动生成Makefile.in

 

Makefile.am中定义的宏和目标,会指导automake生成指定的代码。例如,宏bin_PROGRAMS将导致编译和连接的目标被生成。

 

运行automake --add-missing

 

命令:

 

 

$ automake--add-missing

configure.in:installing `./install-sh'

configure.in:installing `./mkinstalldirs'

configure.in:installing `./missing'

Makefile.am:installing `./depcomp'

 

 

automake会根据Makefile.am文件产生一些文件,包含最重要的Makefile.in

 

 

执行configure生成Makefile

 

 

$ ./configure

checking for aBSD-compatible install... /usr/bin/install -c

checking whetherbuild environment is sane... yes

checking for gawk...gawk

checking whethermake sets $(MAKE)... yes

checking for gcc...gcc

checking for Ccompiler default output... a.out

checking whether theC compiler works... yes

checking whether weare cross compiling... no

checking for suffixof executables...

checking for suffixof object files... o

checking whether weare using the GNU C compiler... yes

checking whether gccaccepts -g... yes

checking for gccoption to accept ANSI C... none needed

checking for styleof include used by make... GNU

checking dependencystyle of gcc... gcc3

configure: creating./config.status

config.status:creating Makefile

config.status:executing depfiles commands

$ ls -l Makefile

-rw-rw-r-- 1 yutaoyutao 15035 Oct 15 10:40 Makefile

 

 

 

使用Makefile编译代码

 

 

 

$ make

if gcc-DPACKAGE_NAME="" -DPACKAGE_TARNAME=""-DPACKAGE_VERSION="" -

 

DPACKAGE_STRING=""-DPACKAGE_BUGREPORT="" -DPACKAGE="helloworld"-DVERSION="1.0"

 

-I. -I. -g -O2 -MThelloworld.o -MD -MP -MF ".deps/helloworld.Tpo" \

-c -o helloworld.o`test -f 'helloworld.c' || echo './'`helloworld.c; \

then mv -f".deps/helloworld.Tpo" ".deps/helloworld.Po"; \

else rm -f".deps/helloworld.Tpo"; exit 1; \

fi

gcc -g -O2 -ohelloworld helloworld.o 

 

 

运行helloworld

 

 

 

$ ./helloworld

Hello, Linux World!

 

Makefile 

 

在符合GNUMakefiel惯例的Makefile中,包含了一些基本的预先定义的操作: 

 

make 

 

根据Makefile编译源代码,连接,生成目标文件,可执行文件。 

 

make clean 

 

清除上次的make命令所产生的object文件(后缀为“.o”的文件)及可执行文件。 

 

make install 

 

将编译成功的可执行文件安装到系统目录中,一般为/usr/local/bin目录。 

 

make dist 

 

产生发布软件包文件(即distributionpackage)。这个命令将会将可执行文件及相关文件打包成一个tar.gz压缩的文件用来作为发布软件的软件包。 

 

它会在当前目录下生成一个名字类似“PACKAGE-VERSION.tar.gz”的文件。PACKAGEVERSION,是我们在configure.in中定义的AM_INIT_AUTOMAKE(PACKAGE, VERSION) 

 

make distcheck 

 

生成发布软件包并对其进行测试检查,以确定发布包的正确性。这个操作将自动把压缩包文件解开,然后执行configure命令,并且执行make,来确认编译不出现错误,最后提示你软件包已经准备好,可以发布了。 

 

=============================================== 

helloworld-1.0.tar.gz is ready for distribution 

=============================================== 

make distclean 

 

类似makeclean,但同时也将configure生成的文件全部删除掉,包括Makefile 

 

五、结束语 

 

通过上面的介绍,你应该可以很容易地生成一个你自己的符合GNU惯例的Makefile文件及对应的项目文件。 

 

如果你想写出更复杂的且符合惯例的Makefile,你可以参考一些开放代码的项目中的configure.inMakefile.am文件,比如:嵌入式数据库sqlite,单元测试cppunit

猜你喜欢

转载自blog.csdn.net/qq_34765864/article/details/70597576