您的位置:首页 > 其它

生成符合自由软件惯例的Makefile

2014-06-17 15:02 155 查看
生成符合自由软件惯例的Makefile

./configure 的生成过程

configure 是个shell 脚本文件,是自动产生的,不是手工编写的。

------------------------------------------------------------

过程简述:

------------------------------------------------------------

编写3个文件

test.c

configure.in

Makefile.am

然后执行:

aclocal

autoconf

automake --add-missing

而后可以编译,安装,发布

./configure 生成Makefile

make

make clean

make install

make clean

make dist

------------------------------------------------------------

1. 准备目录和文件,autoscan 生成 configure.in 的模板

------------------------------------------------------------

[hjj@hjj ~/ctest]$ ls

test.c

[hjj@hjj ~/ctest]$ cat test.c

#include <stdio.h>

int main(int argc, char *argv[])

{

printf("hello\n");

return 0;

}

[hjj@hjj ~/ctest]$ autoscan

[hjj@hjj ~/ctest]$ ls

autoscan.log configure.scan test.c

------------------------------------------------------------

2. 编写configure.in 文件

有一些宏的定义要慢慢积累

AM_INIT_AUTOMAKE 宏是必须的

------------------------------------------------------------

以configure.scan 为模板,添加修改AC_INIT, AM_INIT_AUTOMAKE,AC_OUTPUT, 删除不用部分

[hjj@hjj ~/ctest]$ diff configure.scan configure.in

5,7c5,6

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

< AC_CONFIG_SRCDIR([test.c])

< AC_CONFIG_HEADERS([config.h])

---

> AC_INIT(test, 1.0)

> AM_INIT_AUTOMAKE(test, 1.0)

20c19

< AC_OUTPUT

---

> AC_OUTPUT(Makefile)

------------------------------------------------------------

3. 运行aclocal autoconf 命令

aclocal 是一个perl脚本,将configure.in 变成 aclocal.m4

autoconf 生成configure. 它的输入是aclocal.m4

configure 是一个shell 脚本,它能检测系统特性,使支持各种平台

------------------------------------------------------------

[hjj@hjj ~/ctest]$ ls

autoscan.log configure.in configure.scan test.c

[hjj@hjj ~/ctest]$ aclocal

[hjj@hjj ~/ctest]$ ls

aclocal.m4 autom4te.cache autoscan.log configure.in configure.scan test.c

[hjj@hjj ~/ctest]$ autoconf

[hjj@hjj ~/ctest]$ ls

aclocal.m4 autom4te.cache autoscan.log configure configure.in configure.scan test.c

------------------------------------------------------------

4. 编写Makefile.am

是makefile.in 的输入文件

AUTOMAKE_OPTIONS:检查目录下是否存在标准GNU软件包中应具备的各种文件,例如AUTHORS、ChangeLog、NEWS等文件。

我们将其设置成foreign时,automake会改用一般软件包的标准来检查。

bin_PROGRAMS   指定我们所要产生的可执行文件的文件名。

如果你要产生多个可执行文件,那么在各个名字间用空格隔开。

test_SOURCES   指定产生“test”时所需要的源代码。如果它用到了多个源文件,那么请使用空格符号将它们隔开。

比如需要test.h,test.c那么请写成 test_SOURCES= test.h test.c。

bin_PROGRAMS定义了多个可执行文件,则对应每个可执行文件都要定义相对的filename_SOURCES

------------------------------------------------------------

[hjj@hjj ~/ctest]$ cat Makefile.am

AUTOMAKE_OPTIONS=foreign

bin_PROGRAMS=test

test_SOURCES=test.c

------------------------------------------------------------

5. 运行 automake --add-missing

--add-missing 选项会让automake自动加入软件包所必须的一些文件。

------------------------------------------------------------

[hjj@hjj ~/ctest]$ automake --add-missing

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

configure.in:6: installing `./missing'

Makefile.am: installing `./depcomp'

[hjj@hjj ~/ctest]$ ls

aclocal.m4 autom4te.cache autoscan.log configure configure.in configure.scan depcomp install-sh Makefile.am Makefile.in missing test.c

生成的Makefile.in 是符合GNU 惯例的。

至此,源码发布文件已经全部生成。

------------------------------------------------------------

6. 运行 ./configure

------------------------------------------------------------

[hjj@hjj ~/ctest]$ ./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... gawk

checking whether make sets $(MAKE)... yes

checking for gcc... gcc

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 gcc accepts -g... yes

checking for gcc option to accept ISO C89... none needed

checking for style of include used by make... GNU

checking dependency style of gcc... gcc3

configure: creating ./config.status

config.status: creating Makefile

config.status: executing depfiles commands

------------------------------------------------------------

7. make & clean & distribute & install

------------------------------------------------------------

[hjj@hjj ~/ctest]$ make

gcc -DPACKAGE_NAME=\"test\" -DPACKAGE_TARNAME=\"test\" -DPACKAGE_VERSION=\"1.0\" -DPACKAGE_STRING=\"test\ 1.0\" -DPACKAGE_BUGREPORT=\"\" -DPACKAGE=\"test\" -DVERSION=\"1.0\" -I. -g -O2 -MT test.o -MD -MP -MF .deps/test.Tpo -c -o test.o test.c

mv -f .deps/test.Tpo .deps/test.Po

gcc -g -O2 -o test test.o

[hjj@hjj ~/ctest]$ make dist

{ test ! -d "test-1.0" || { find "test-1.0" -type d ! -perm -200 -exec chmod u+w {} ';' && rm -fr "test-1.0"; }; }

test -d "test-1.0" || mkdir "test-1.0"

test -n "" \

|| find "test-1.0" -type d ! -perm -755 \

-exec chmod u+rwx,go+rx {} \; -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/hjj/ctest/install-sh -c -m a+r {} {} \; \

|| chmod -R a+r "test-1.0"

tardir=test-1.0 && /bin/sh /home/hjj/ctest/missing --run tar chof - "$tardir" | GZIP=--best gzip -c >test-1.0.tar.gz

{ test ! -d "test-1.0" || { find "test-1.0" -type d ! -perm -200 -exec chmod u+w {} ';' && rm -fr "test-1.0"; }; }

[hjj@hjj ~/ctest]$ make clean

test -z "test" || rm -f test

rm -f *.o

[hjj@hjj ~/ctest]$ make

gcc -DPACKAGE_NAME=\"test\" -DPACKAGE_TARNAME=\"test\" -DPACKAGE_VERSION=\"1.0\" -DPACKAGE_STRING=\"test\ 1.0\" -DPACKAGE_BUGREPORT=\"\" -DPACKAGE=\"test\" -DVERSION=\"1.0\" -I. -g -O2 -MT test.o -MD -MP -MF .deps/test.Tpo -c -o test.o test.c

mv -f .deps/test.Tpo .deps/test.Po

gcc -g -O2 -o test test.o

[hjj@hjj ~/ctest]$ sudo make install

make[1]: Entering directory `/home/hjj/ctest'

test -z "/usr/local/bin" || /bin/mkdir -p "/usr/local/bin"

/usr/bin/install -c test '/usr/local/bin'

make[1]: Nothing to be done for `install-data-am'.

make[1]: Leaving directory `/home/hjj/ctest'

------------------------------------------------------------

调试信息的添加:

------------------------------------------------------------

不要在Makefile中用CFLAGS,CXXFLAGS,xxx_CFLAGS,xxx_CXXFLAGS来设置调试信息。这样每次发布前都要修改Makefile.am

执行configure,设置相关环境变量, 重新生成Makefile, 来决定是否需要调试信息

如果是 C 程序,就执行 ./configure "CFLAGS=..."

如果是 C++ 程序,就执行 ./configure "CXXFLAGS=..."

可执行 ./configure --help 查看帮助。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: