歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Makefile的automake生成(HelloWorld)

Makefile的automake生成(HelloWorld)

日期:2017/3/1 10:29:32   编辑:Linux編程

環境:

Ubuntu 11.04

Autoconf 2.67

Automake 1.11.1

1、新建目錄HelloWorld

2、進入該目錄,編寫HelloWorld.c

[cpp]

  1. int main(int argc,char** argv)
  2. {
  3. printf("Hello World!\n");
  4. return 0;
  5. }
3、生成configure

a、使用autoscan來根據目錄下的源代碼生成一個configure.in的模板文件configure.scan:

[plain]

  1. fzuir@ubuntu:~/workspace/automake/Hellworld$ ls
  2. HelloWorld.c
  3. fzuir@ubuntu:~/workspace/automake/Hellworld$ autoscan
  4. fzuir@ubuntu:~/workspace/automake/Hellworld$ ls
  5. autoscan.log configure.scan HelloWorld.c
b、將configure.scan改為configure.in,並修改其內容為:

[plain]

  1. # -*- Autoconf -*-
  2. # Process this file with autoconf to produce a configure script.
  3. AC_INIT(helloworld.c)
  4. AM_INIT_AUTOMAKE(helloworld,1.0)
  5. # Checks for programs.
  6. AC_PROG_CC
  7. # Checks for libraries.
  8. # Checks for header files.
  9. # Checks for typedefs, structures, and compiler characteristics.
  10. # Checks for library functions.
  11. AC_OUTPUT(Makefile)

c、執行aclocal生成aclocal.m4,執行autoconf生成configure
[plain]
  1. fzuir@ubuntu:~/workspace/automake/Hellworld$ ls
  2. autoscan.log configure.in HelloWorld.c
  3. fzuir@ubuntu:~/workspace/automake/Hellworld$ aclocal
  4. fzuir@ubuntu:~/workspace/automake/Hellworld$ ls
  5. aclocal.m4 autom4te.cache autoscan.log configure.in HelloWorld.c
  6. fzuir@ubuntu:~/workspace/automake/Hellworld$ autoconf
  7. fzuir@ubuntu:~/workspace/automake/Hellworld$ ls
  8. aclocal.m4 autom4te.cache autoscan.log configure configure.in HelloWorld.c
4、創建Makefile.am,內容如下:

[plain]

  1. AUTOMAKE_OPTIONs=foreign
  2. bin_PROGRAMS=Helloworld
  3. HelloWorld_SOURCES=HelloWorld.c
5、運行automake

automake會根據Makefile.am來自動生成Makefile.in

[plain]

  1. fzuir@ubuntu:~/workspace/automake/Hellworld$ automake --add-missing
  2. configure.in:5: installing `./install-sh'
  3. configure.in:5: installing `./missing'
  4. Makefile.am: installing `./depcomp'
  5. fzuir@ubuntu:~/workspace/automake/Hellworld$ ls
  6. aclocal.m4 autoscan.log configure.in HelloWorld.c Makefile.am missing
  7. autom4te.cache configure depcomp install-sh Makefile.in
6、運行configure命令生成Makefile

[plain]

  1. fzuir@ubuntu:~/workspace/automake/Hellworld$ ./configure
  2. checking for a BSD-compatible install... /usr/bin/install -c
  3. checking whether build environment is sane... yes
  4. checking for a thread-safe mkdir -p... /bin/mkdir -p
  5. checking for gawk... no
  6. checking for mawk... mawk
  7. checking whether make sets $(MAKE)... yes
  8. checking for gcc... gcc
  9. checking whether the C compiler works... yes
  10. checking for C compiler default output file name... a.out
  11. checking for suffix of executables...
  12. checking whether we are cross compiling... no
  13. checking for suffix of object files... o
  14. checking whether we are using the GNU C compiler... yes
  15. checking whether gcc accepts -g... yes
  16. checking for gcc option to accept ISO C89... none needed
  17. checking for style of include used by make... GNU
  18. checking dependency style of gcc... gcc3
  19. configure: creating ./config.status
  20. config.status: creating Makefile
  21. config.status: executing depfiles commands
7、運行make命令進行編譯

[plain]

  1. fzuir@ubuntu:~/workspace/automake/Hellworld$ make
  2. gcc -DPACKAGE_NAME=\"\" -DPACKAGE_TARNAME=\"\" -DPACKAGE_VERSION=\"\" -DPACKAGE_STRING=\"\" -DPACKAGE_BUGREPORT=\"\" -DPACKAGE_URL=\"\" -DPACKAGE=\"HelloWorld\" -DVERSION=\"1.0\" -I. -g -O2 -MT HelloWorld.o -MD -MP -MF .deps/HelloWorld.Tpo -c -o HelloWorld.o HelloWorld.c
  3. HelloWorld.c: In function ‘main’:
  4. HelloWorld.c:3: warning: incompatible implicit declaration of built-in function ‘printf’
  5. mv -f .deps/HelloWorld.Tpo .deps/HelloWorld.Po
  6. gcc -g -O2 -o HelloWorld HelloWorld.o

8、運行HelloWorld

[plain]

  1. fzuir@ubuntu:~/workspace/automake/Hellworld$ ./HelloWorld
  2. Hello World!
Copyright © Linux教程網 All Rights Reserved