Chinaunix首页 | 论坛 | 博客
  • 博客访问: 10838
  • 博文数量: 8
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 10
  • 用 户 组: 普通用户
  • 注册时间: 2014-06-25 14:58
文章分类
文章存档

2014年(8)

我的朋友

分类: 嵌入式

2014-10-16 16:57:55

    下面来实现不同子目录下,带静态/动态链接库的自动生成Makefile:
    首先建立工程目录test以及工程代码,这里从往上复制一套简单的工程代码过来,先看看代码框架:
test
|
|---- src
|     |---- main
|     |     |---- main.c
|     |
|     |---- myadd
|     |     |---- myadd.c
|     |
|     |---- swap
|           |---- swap.c
|
|---- include
       |---- swap.h
       |---- myadd.h
代码:

  1. /* ### src/main/main.c ### */
  2. #include <stdio.h>
  3. #include "swap.h"
  4. #include "myadd.h"
  5. int main()
  6. {
  7.     int a = 10;
  8.     int b = 20;
  9.     int sum = 0;
  10.     printf("a is: %d, b is: %d\n",a,b);
  11.     swap(&a,&b);
  12.     printf("after swap,a is: %d, b is: %d\n",a,b);
  13.     printf("start to run myadd(a,b)\n");
  14.     sum = myadd(a,b);
  15.     printf("a+b=%d\n",sum);
  16. }

  17. /* ### src/myadd/myadd.c ### */
  18. #include <stdio.h>
  19. int myadd(int a, int b)
  20. {
  21.     return (a+b);
  22. }

  23. /* ### src/swap/swap.c ### */
  24. #include <stdio.h>
  25. void swap(int *a, int *b)
  26. {
  27.     int temp = 0;
  28.     temp = *a;
  29.     *a = *b;
  30.     *b = temp;
  31. }

  32. /* ### include/myadd.h ### */
  33. #include <stdio.h>
  34. int myadd( int,int ) ;

  35. /* ### include/swap.h ### */
  36. #include <stdio.h>
  37. void swap( int*,int* ) ;
这里要求:将swap链接为静态库,将myadd链接为动态库。
OK,开始automake:
1:执行autoscan,把configure.scan改成configure.in然后打开这个.in并修改,修改后如下:

  1. # -*- Autoconf -*-
  2. # Process this file with autoconf to produce a configure script.

  3. AC_PREREQ([2.69])
  4. AC_INIT(test, 1.0.0, sky_pengliang@163.com)
  5. AC_CONFIG_SRCDIR([src/main/main.c])
  6. AC_CONFIG_HEADERS([config.h])
  7. AM_INIT_AUTOMAKE(test, 1.0.0)
  8. AC_CONFIG_MACRO_DIR([m4])
  9. AC_PROG_LIBTOOL

  10. AC_PROG_CC

  11. AC_OUTPUT(Makefile
  12.            include/Makefile
  13.            src/main/Makefile
  14.            src/swap/Makefile
  15.            src/myadd/Makefile
  16.           )
2:接着执行下面的命令
>aclocal; libtoolize; autoheader; autoconf 
不出问题的话,就要开始在每个有.h和.c的目录下面添加Makefile.am文件了;

3:添加Makefile.am文件:
  1. /* ### 根目录Makefile.am ### */
  2. SUBDIRS = include src/swap src/myadd src/main
  3. 这个有先后顺序

  4. /* ### include/Makefile.am ### */
  5. helloincludedir=$(includedir)
  6. helloinclude_HEADERS =swap.h myadd.h

  7. /* ### src/main/Makefile.am ### */
  8. bin_PROGRAMS=test
  9. test_SOURCES=main.c
  10. test_LDADD=$(top_srcdir)/src/swap/libswap.a
  11. LIBS=-lmyadd
  12. INCLUDES=-I$(top_srcdir)/include
  13. test_LDFLAGS=-L$(top_srcdir)/src/myadd

  14. /* ### src/swap/Makefile.am ### */
  15. noinst_LIBRARIES=libswap.a
  16. libswap_a_SOURCES=swap.c
  17. INCLUDES=-I$(top_srcdir)/include

  18. /* ### src/myadd/Makefile.am ### */
  19. lib_LTLIBRARIES=libmyadd.la
  20. libmyadd_la_SOURCES=myadd.c
  21. INCLUDES =-I$(top_srcdir)/include

4:执行automake --add-missing,无法通过的,还需要下面几个文件,我们暂时 touch 一下即可
>touch README NEWS AUTHORS ChangeLog

5:最后执行:
>./configure

到此Makefile已经建好:
>make 
>make clean
>make install
>make uninstall
>make dist
都可以试试!






阅读(315) | 评论(0) | 转发(0) |
0

上一篇:(一)自动生成Makefile - 入门级

下一篇:没有了

给主人留下些什么吧!~~