利用autoconf,automake生成makefile全攻略
Hello.h 文件内容 #ifndef HelloH #define HelloH
class Hello { public: Hello(); void Display();
};
class CInt { public: CInt(); ~CInt(); private: int m_value; public: void SetValue(int i); int GetValue(); }; #endif
Hello.cpp文件内容
#include <iostream.h> #include "Hello.h"
Hello::Hello() { }
void Hello::Display() { cout<<"Hello world!/n"<<endl; }
CInt::CInt() {
} CInt::~CInt() {
}
void CInt::SetValue(int i) { this->m_value = i; return; } int CInt::GetValue() { return this->m_value;
}
MyFirlst.cpp主文件内容 #include <iostream.h> #include "Hello.cpp" int main(int argc, char* argv[]) { Hello theHello; theHello.Display(); CInt ci; ci.SetValue(10); cout<<"value = "<<ci.GetValue()<<endl; return 0; }
利用工具autoconf, automake生成makefile 将上述3个文件放入同一目录,最好是新生成的目录,如program
[root@localhost program]# ls Hello.cpp Hello.h MyFirst.cpp
1. 利用autoscan工具产生一个configure.in文件的模板文件configure.scan文件: [root@localhost program]# autoscan autom4te: configure.ac: no such file or directory autoscan: /usr/bin/autom4te failed with exit status: 1 [root@localhost program]# ls autoscan.log configure.scan Hello.cpp Hello.h MyFirst.cpp 2. 把configure.scan文件更名为configure.in文件,并编译其内容如下: [root@localhost program]# mv configure.scan configure.in [root@localhost program]# vi configure.in
# -*- 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([Hello.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
改成如下内容: # -*- 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_INIT(MyFirst.cpp) AM_INIT_AUTOMAKE(MyFirst_EXE,1.0) AC_CONFIG_SRCDIR([Hello.cpp]) AM_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_CONFIG_FILES([Makefile]) AC_OUTPUT
3. 执行aclocal,会产生aclocal.m4文件 [root@localhost program]# aclocal [root@localhost program]# ls aclocal.m4 autoscan.log Hello.cpp MyFirst.cpp autom4te.cache configure.in Hello.h 4. 执行autoconf,会产生confiure文件 [root@localhost program]# autoconf [root@localhost program]# ls aclocal.m4 autoscan.log configure.in Hello.h autom4te.cache configure Hello.cpp MyFirst.cpp 5. 执行autoheader,会产生config.h.in文件 [root@localhost program]# autoheader [root@localhost program]# ls aclocal.m4 autoscan.log configure Hello.cpp MyFirst.cpp autom4te.cache config.h.in configure.in Hello.h 6. 创建文件Makefile.am并编辑其内容如下: [root@localhost program]# vi Makefile.am 输入以下内容: 注: 其中,hello为编译后产生的可执行文件名称,而第三行等号后面为源文件列表 AUTOMAKE_OPTIONS=foreign bin_PROGRAMS=MyFirst_EXE 注: MyFirst_EXE 2处一定要一样 MyFirst_EXE_SOURCES=MyFirst.cpp Hello.cpp 注:有多少cpp文件,就写上多少
文件内容如下: AUTOMAKE_OPTIONS=foreign bin_PROGRAMS=MyFirst_EXE MyFirst_EXE_SOURCES=MyFirst.cpp Hello.cpp
[root@localhost program]# ls aclocal.m4 autoscan.log configure Hello.cpp Makefile.am autom4te.cache config.h.in configure.in Hello.h MyFirst.cpp 7. 执行automake程序,automake程序会根据Makefile.am产生一些文件,其中最重要的是Makefile.in文件: [root@localhost program]# automake --add-missing 显示如下信息 configure.in: installing `./install-sh' configure.in: installing `./missing' Makefile.am: installing `./depcomp' 8. 执行configure脚本,生成我们需要的Makefile文件。 [root@localhost program]# ./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 checking for gcc... gcc checking whether we are using the GNU C compiler... yes checking whether gcc accepts -g... yes checking for gcc option to accept ANSI C... none needed checking dependency style of gcc... gcc3 configure: creating ./config.status config.status: creating Makefile config.status: creating config.h config.status: executing depfiles commands 9. 最后只执行make就大功告成了: [root@localhost program]# make make all-am make[1]: Entering directory `/root/program' if g++ -DHAVE_CONFIG_H -I. -I. -I. -g -O2 -MT MyFirst.o -MD -MP -MF ".deps/MyFirst.Tpo" -c -o MyFirst.o MyFirst.cpp; / then mv -f ".deps/MyFirst.Tpo" ".deps/MyFirst.Po"; else rm -f ".deps/MyFirst.Tpo"; exit 1; fi 在包含自 /usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/backward/iostream.h:31 的文件中, 从 MyFirst.cpp:1: /usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/backward/backward_warning.h:32:2: 警告:#warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated. if g++ -DHAVE_CONFIG_H -I. -I. -I. -g -O2 -MT Hello.o -MD -MP -MF ".deps/Hello.Tpo" -c -o Hello.o Hello.cpp; / then mv -f ".deps/Hello.Tpo" ".deps/Hello.Po"; else rm -f ".deps/Hello.Tpo"; exit 1; fi 在包含自 /usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/backward/iostream.h:31 的文件中, 从 Hello.cpp:1: /usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/backward/backward_warning.h:32:2: 警告:#warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated. g++ -g -O2 -o MyFirst_EXE MyFirst.o Hello.o make[1]: Leaving directory `/root/program' [root@localhost program]# ls aclocal.m4 config.log Hello.cpp Makefile.am MyFirst.o autom4te.cache config.status Hello.h Makefile.in stamp-h1 autoscan.log configure Hello.o missing config.h configure.in install-sh MyFirst.cpp config.h.in depcomp Makefile MyFirst_EXE 10. 运行生成的MyFirst_EXE,看一下效果 [root@localhost program]# ./MyFirst_EXE Hello world!
value = 20
到此,已经全部OK.
本文介绍如何通过autoconf和automake工具自动生成Makefile,包括配置文件的编辑、工具的使用步骤及最终编译运行的过程。
170

被折叠的 条评论
为什么被折叠?



