⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 蚂蚁公社 - 轻轻松松产生 makefile 利用automake.htm

📁 Makefile编程及相关资料的介绍与入门。很适合初学者的学习与借鉴
💻 HTM
📖 第 1 页 / 共 4 页
字号:
            <P>档案的地方: </P>
            <P>% mkdir devel </P>
            <P>% cd devel </P>
            <P>% mkdir hello </P>
            <P>% cd hello </P>
            <P>用编辑器写个 hello.c 档, </P>
            <P>#include <STDIO.H></P>
            <P>int main(int argc, char** argv) </P>
            <P>{ </P>
            <P>printf(``Hello, GNU!\n''); </P>
            <P>return 0; </P>
            <P>} </P>
            <P>接下来就要用 Autoconf 及 Automake 来帮我们产生 Makefile 档了, </P>
            <OL>
              <LI>用 autoscan 产生一个 configure.in 的雏型,执行 autoscan 後会产 </LI></OL>
            <P>生一个configure.scan 的档案,我们可以用它做为 configure.in </P>
            <P>档的蓝本。 </P>
            <P>% autoscan </P>
            <P>% ls </P>
            <P>configure.scan hello.c </P>
            <OL>
              <LI>编辑 configure.scan 档,如下所示,并且把它的档名改成 </LI></OL>
            <P>configure.in </P>
            <P>dnl Process this file with autoconf to produce a con </P>
            <P>figure script. </P>
            <P>AC_INIT(hello.c) </P>
            <P>AM_INIT_AUTOMAKE(hello, 1.0) </P>
            <P>dnl Checks for programs. </P>
            <P>AC_PROG_CC </P>
            <P>dnl Checks for libraries. </P>
            <P>dnl Checks for header files. </P>
            <P>dnl Checks for typedefs, structures, and compiler ch </P>
            <P>aracteristics. </P>
            <P>dnl Checks for library functions. </P>
            <P>AC_OUTPUT(Makefile) </P>
            <OL>
              <LI>执行 aclocal 和 autoconf ,分别会产生 aclocal.m4 及 configure 两 </LI></OL>
            <P>个档案 </P>
            <P>% aclocal </P>
            <P>% autoconf </P>
            <P>% ls </P>
            <P>aclocal.m4 configure configure.in hello.c </P>
            <OL>
              <LI>编辑 Makefile.am 档,内容如下 </LI></OL>
            <P>AUTOMAKE_OPTIONS= foreign </P>
            <P>bin_PROGRAMS= hello </P>
            <P>hello_SOURCES= hello.c </P>
            <OL>
              <LI>执行 automake --add-missing ,Automake 会根据 Makefile.am 档产生 
            </LI></OL>
            <P>一些档案,包含最重要的 Makefile.in </P>
            <P>% automake --add-missing </P>
            <P>automake: configure.in: installing `./install-sh' </P>
            <P>automake: configure.in: installing `./mkinstalldirs' </P>
            <P>automake: configure.in: installing `./missing' </P>
            <OL>
              <LI>最後执行 ./configure , </LI></OL>
            <P>% ./configure </P>
            <P>creating cache ./config.cache </P>
            <P>checking for a BSD compatible install... /usr/bin/in </P>
            <P>stall -c </P>
            <P>checking whether build environment is sane... yes </P>
            <P>checking whether make sets ${MAKE}... yes </P>
            <P>checking for working aclocal... found </P>
            <P>checking for working autoconf... found </P>
            <P>checking for working automake... found </P>
            <P>checking for working autoheader... found </P>
            <P>checking for working makeinfo... found </P>
            <P>checking for gcc... gcc </P>
            <P>checking whether the C compiler (gcc ) works... yes </P>
            <P>checking whether the C compiler (gcc ) is a cross-co mpiler... no 
            </P>
            <P>checking whether we are using GNU C... yes </P>
            <P>checking whether gcc accepts -g... yes </P>
            <P>updating cache ./config.cache </P>
            <P>creating ./config.status </P>
            <P>creating Makefile </P>
            <P>现在你的目录下已经产生了一个 Makefile 档,下个 ``make'' 指令就可 </P>
            <P>以开始编译 hello.c 成执行档,执行 ./hello 和 GNU 打声招呼吧! </P>
            <P>% make </P>
            <P>gcc -DPACKAGE=\"hello\" -DVERSION=\"1.0\" -I. -I. -g -O2 -c he 
            </P>
            <P>llo.c </P>
            <P>gcc -g -O2 -o hello hello.o </P>
            <P>% ./hello </P>
            <P>Hello! GNU! </P>
            <P>你还可以试试 ``make clean'',''make install'',''make dist'' 看看 </P>
            <P>会有什麽结果。你也可以把产生出来的 Makefile 秀给你的老板,让他从 </P>
            <P>此对你刮目相看 :-) </P>
            <OL>
              <LI>一探究竟 </LI></OL>
            <P>上述产生 Makefile 的过程和以往自行编写的方式非常不一样,舍弃传统 </P>
            <P>自行定义 make 的规则,使用 Automake 只需用到一些已经定义好的巨集 </P>
            <P>即可。我们把巨集及目标 (target) 写在 Makefile.am 档内,Automake </P>
            <P>读入 Makefile.am 档後会把这一串已经定义好的巨集展开并且产生对应的 </P>
            <P>Makefile.in 档, 然後再由 configure 这个 shell script 根据 </P>
            <P>Makefile.in 产生适合的 Makefile。 </P>
            <P>[Figure 1:利用 autoconf 及 automake 产生 Makefile 的流程] </P>
            <P>上图中表示在上一节范例中所要用的档案以及产生出来的档案,有星号 </P>
            <P>(*) 者代表可执行档。在此范例中可藉由 Autoconf 及 Automake 工具所 </P>
            <P>产生的档案有 configure.scan、aclocal.m4、configure、Makefile.in, </P>
            <P>需要我们加入设定者为 configure.in 及 Makefile.am。 </P>
            <OL>
              <LI>1 编辑 configure.in 档 </LI></OL>
            <P>Autoconf 是用来产生 <CODE>configure</CODE> 档的工具。<CODE>configure</CODE> 
            是一个 </P>
            <P>shell script,它可以自动设定原始程式以符合各种不同平台上 Unix 系 </P>
            <P>统的特性,并且根据系统叁数及环境产生合适的 Makefile 档或是C 的标 </P>
            <P>头档 (header file),让原始程式可以很方便地在这些不同的平台上被编 </P>
            <P>译出来。Autoconf 会读取 configure.in 档然後产生 <CODE>configure</CODE> 这个 
</P>
            <P>shell script。 </P>
            <P>configure.in 档的内容是一连串 GNU m4 的巨集,这些巨集经过 </P>
            <P>autoconf 处理後会变成检查系统特徵的 shell script。configure.in 内 </P>
            <P>巨集的顺序并没有特别的规定,但是每一个 configure.in 档必须在所有 </P>
            <P>巨集前加入 AC_INIT 巨集,然後在所有巨集的最後面加上 AC_OUTPUT 巨 </P>
            <P>集。我们可先用 autoscan 扫描原始档以产生一个 configure.scan 档, </P>
            <P>再对 configure.scan 做些修改成 configure.in 档。在范例中所用到的 </P>
            <P>巨集如下: </P>
            <P>dnl </P>
            <P>这个巨集後面的字不会被处理,可视为注解。 </P>
            <P>AC_INIT(FILE) </P>
            <P>这个巨集用来检查原始码所在的路径,autoscan 会自动产生,我们 </P>
            <P>不必修改它。 </P>
            <P>AM_INIT_AUTOMAKE(PACKAGE,VERSION) </P>
            <P>这是使用 Automake 所必备的巨集,PACKAGE 是我们所要产生软体套 </P>
            <P>件的名称,VERSION 是版本编号。 </P>
            <P>AC_PROG_CC </P>
            <P>检查系统可用的 C 编译器,如果原始程式是用 C 写的就需要这个巨 </P>
            <P>集。 </P>
            <P>AC_OUTPUT(FILE) </P>
            <P>设定 configure 所要产生的档案,如果是 Makefile 的话, </P>
            <P>configure 便会把它检查出来的结果带入 Makefile.in 档然後产生 </P>
            <P>合适的 Makefile。 </P>
            <P>实际上,我们使用 Automake 时,还须要一些其它的巨集,这些额外的巨 </P>
            <P>集我们用 aclocal 来帮我们产生。执行 aclocal 会产生 aclocal.m4 </P>
            <P>档,如果没有特别的用途,我们可以不必修改它,用 aclocal 所产生的巨 </P>
            <P>集会告诉 Automake 怎麽做。 </P>
            <P>有了 configure.in 及 aclocal.m4 两个档案後,便可以执行 autoconf </P>
            <P>来产生 configure 档了。 </P>
            <OL>
              <LI>2 编辑 Makefile.am 档 </LI></OL>
            <P>接下来我们要编辑 Makefile.am 档,Automake 会根据 configure.in 中 </P>
            <P>的巨集把Makefile.am 转成 Makefile.in 档。Makefile.am 档定义我们所 </P>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -