下面来实现不同子目录下,带静态/动态链接库的自动生成Makefile:
首先建立工程目录test以及工程代码,这里从往上复制一套简单的工程代码过来,先看看代码框架:
test
|
|---- src
| |---- main
| | |---- main.c
| |
| |---- myadd
| | |---- myadd.c
| |
| |---- swap
| |---- swap.c
|
|---- include
|---- swap.h
|---- myadd.h
代码:
-
/* ### src/main/main.c ### */
-
#include <stdio.h>
-
#include "swap.h"
-
#include "myadd.h"
-
int main()
-
{
-
int a = 10;
-
int b = 20;
-
int sum = 0;
-
printf("a is: %d, b is: %d\n",a,b);
-
swap(&a,&b);
-
printf("after swap,a is: %d, b is: %d\n",a,b);
-
printf("start to run myadd(a,b)\n");
-
sum = myadd(a,b);
-
printf("a+b=%d\n",sum);
-
}
-
-
/* ### src/myadd/myadd.c ### */
-
#include <stdio.h>
-
int myadd(int a, int b)
-
{
-
return (a+b);
-
}
-
-
/* ### src/swap/swap.c ### */
-
#include <stdio.h>
-
void swap(int *a, int *b)
-
{
-
int temp = 0;
-
temp = *a;
-
*a = *b;
-
*b = temp;
-
}
-
-
/* ### include/myadd.h ### */
-
#include <stdio.h>
-
int myadd( int,int ) ;
-
-
/* ### include/swap.h ### */
-
#include <stdio.h>
-
void swap( int*,int* ) ;
这里要求:将swap链接为静态库,将myadd链接为动态库。
OK,开始automake:
1:执行autoscan,把configure.scan改成configure.in然后打开这个.in并修改,修改后如下:
-
# -*- Autoconf -*-
-
# Process this file with autoconf to produce a configure script.
-
-
AC_PREREQ([2.69])
-
AC_INIT(test, 1.0.0, sky_pengliang@163.com)
-
AC_CONFIG_SRCDIR([src/main/main.c])
-
AC_CONFIG_HEADERS([config.h])
-
AM_INIT_AUTOMAKE(test, 1.0.0)
-
AC_CONFIG_MACRO_DIR([m4])
-
AC_PROG_LIBTOOL
-
-
AC_PROG_CC
-
-
AC_OUTPUT(Makefile
-
include/Makefile
-
src/main/Makefile
-
src/swap/Makefile
-
src/myadd/Makefile
-
)
2:接着执行下面的命令
>aclocal; libtoolize; autoheader; autoconf
不出问题的话,就要开始在每个有.h和.c的目录下面添加Makefile.am文件了;
3:添加Makefile.am文件:
-
/* ### 根目录Makefile.am ### */
-
SUBDIRS = include src/swap src/myadd src/main
-
这个有先后顺序
-
-
/* ### include/Makefile.am ### */
-
helloincludedir=$(includedir)
-
helloinclude_HEADERS =swap.h myadd.h
-
-
/* ### src/main/Makefile.am ### */
-
bin_PROGRAMS=test
-
test_SOURCES=main.c
-
test_LDADD=$(top_srcdir)/src/swap/libswap.a
-
LIBS=-lmyadd
-
INCLUDES=-I$(top_srcdir)/include
-
test_LDFLAGS=-L$(top_srcdir)/src/myadd
-
-
/* ### src/swap/Makefile.am ### */
-
noinst_LIBRARIES=libswap.a
-
libswap_a_SOURCES=swap.c
-
INCLUDES=-I$(top_srcdir)/include
-
-
/* ### src/myadd/Makefile.am ### */
-
lib_LTLIBRARIES=libmyadd.la
-
libmyadd_la_SOURCES=myadd.c
-
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
都可以试试!
阅读(852) | 评论(0) | 转发(2) |