本文主要是介绍autotools使用——C源文件在不同目录下,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
autotools使用——C源文件在不同目录下
# ls
include main.c src
# ls include/
common.h get.h sum.h val.h
# ls src/
get.c sum.c val.c
# cat main.c
#include "include/common.h"//入口主函数
int main() {puts("当前线程sleep 2秒");sleep(2);int x = 10;int y = 20;int z = sum(&x, &y);puts("This is Main");printf("Z:%d\n", z);x = 20;z = get(&x, &y);printf("Z:%d\n", z);return 0;
}
# cat src/sum.c
#include "../include/sum.h"
#include "../include/val.h"int sum(int *x, int *y) {val(x);puts("This is SUM Method!=========HDH");return *x + *y;
}
# cat src/val.c
#include "../include/val.h"int val(int *x) {//引入libevent的方法struct event_base *base; //定义一个event_basebase = event_base_new(); //初始化一个event_baseconst char *y = event_base_get_method(base); //查看用了哪个IO多路复用模型,linux一下用epoll printf("METHOD:%s\n", y);event_base_free(base); //销毁libeventputs("This is Value==");printf("X:%d \n", *x);return 0;
}
# cat src/get.c
#include "../include/get.h"int get(int *x, int *y) {puts("This is get");return (*x) * (*y);
}
# cat include/common.h
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <event2/event.h>
#include <event2/bufferevent.h>
#include <pthread.h>
# cat include/sum.h
int sum(int *x, int *y);
# cat include/val.h
#include "common.h" int val(int *x);
# cat include/get.h
int get(int *x, int *y);
1> 不同目录创建 Makefile.am. automake根据configure.in中的参量把Makefile.am转换成Makefile.in,最终通过Makefile.in 生成Makefile 。
# cat Makefile.am
AUTOMAKE_OPTIONS = foreign
SUBDIRS = src
bin_PROGRAMS = main
main_SOURCE = main.c
main_LDADD = src/libpro.a # 静态链接 libpro.a
LIBS = -l pthread -l event # 动态链接
# cat src/Makefile.am
noinst_LIBRARIES = libpro.a # 生成静态库文件libpro.a;noinst表示只编译,不安装到系统
libpro_a_SOURCES = sum.c get.c val.c # 该静态库文件需要的依赖
include_HEADERS = ../include/common.h ../include/sum.h ../include/get.h ../include/val.h # 依赖头文件
# ls
include main.c Makefile.am src
# ls src/
get.c Makefile.am sum.c val.c
2> autoscan: 扫描工作目录,生成configure.scan(configure.ac模板) 。
# autoscan
# ls
autom4te.cache autoscan.log configure.scan include main.c Makefile.am src
# mv configure.scan configure.ac
# ls
autoscan.log configure.ac get.c get.h main.c sum.c sum.h val.c val.h
修改configure.ac 如下,
# cat configure.ac
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.AC_PREREQ([2.69])
AC_INIT(main, 1.0, test@qq.com)
AM_INIT_AUTOMAKE(main, 1.0)
AC_PROG_RANLIB # 生成静态库
AC_CONFIG_SRCDIR([main.c])
AC_CONFIG_HEADERS([config.h])# Checks for programs.
AC_PROG_CC# Checks for libraries.# Checks for header files.
AC_CHECK_HEADERS([stdlib.h unistd.h])# Checks for typedefs, structures, and compiler characteristics.# Checks for library functions.AC_CONFIG_FILES([Makefilesrc/Makefile])
AC_OUTPUT
configure.ac标签说明:
标签 | 说明 |
AC_PREREQ | 声明autoconf要求的版本号 |
AC_INIT | 定义软件名称、版本号、联系方式 |
AM_INIT_AUTOMAKE | 必须要的,参数为软件名称和版本号或不需要参数 |
AC_CONFIG_SCRDIR | 宏用来侦测所指定的源码文件是否存在, 来确定源码目录的有效性.。此处为当前目录下main.c。 |
AC_CONFIG_HEADER | 宏用于生成config.h文件,以便 autoheader 命令使用。 |
AC_PROG_CC | 指定编译器,默认GCC |
AC_CONFIG_FILES | 生成相应的Makefile文件,不同文件夹下的Makefile通过空格分隔。例如:AC_CONFIG_FILES([Makefile, src/Makefile]) |
AC_OUTPUT | 用来设定 configure 所要产生的文件,如果是makefile,configure 会把它检查出来的结果带入makefile.in文件产生合适的makefile。 |
3> aclocal:扫描configure.ac文件生成aclocal.m4,该文件主要处理本地宏的定义,根据已安装的宏,用户定义宏和acinclude.m4中的宏将configure.ac需要的宏集中定义到aclocal.m4中。
# aclocal
# ls
aclocal.m4 autom4te.cache autoscan.log configure.ac include main.c Makefile.am src
4> autoconf:将configure.ac中的宏展开,根据需要结合aclocal.m4中的宏,生成configurs脚本。
# autoconf
# ls
aclocal.m4 autom4te.cache autoscan.log configure configure.ac include main.c Makefile.am src
5> autoheader:生成config.h.in。该命令通常从“acconfig.h”文件中复制用户附加的符号定义,本例无附件的符号定义,故不需创建 acconfig.h。
# autoheader
# ls
aclocal.m4 autom4te.cache autoscan.log config.h.in configure configure.ac include main.c Makefile.am src
6> automake --add-missing 生成Makefile.in 。选项 --add-missing 让automake 自动添加一些必须的脚本文件。
# automake --add-missing
configure.ac:12: installing './compile'
configure.ac:6: installing './install-sh'
configure.ac:6: installing './missing'
Makefile.am: installing './depcomp'
# ls
aclocal.m4 autoscan.log config.h.in configure.ac include main.c Makefile.in src
autom4te.cache compile configure depcomp install-sh Makefile.am missing
7> ./configure ,将 Makefile.in 变成最终的 Makefile 。
# ./configure
# ls
aclocal.m4 compile config.log configure.ac install-sh Makefile.am src
autom4te.cache config.h config.status depcomp main.c Makefile.in stamp-h1
autoscan.log config.h.in configure include Makefile missing
# ls src/
get.c Makefile Makefile.am Makefile.in sum.c val.c
8> make
# make
# ls
aclocal.m4 compile config.log configure.ac include main.c Makefile.am src
autom4te.cache config.h config.status configure.scan install-sh main.o Makefile.in stamp-h1
autoscan.log config.h.in configure depcomp main Makefile missing
# ls src/
get.c get.o libpro.a Makefile Makefile.am Makefile.in sum.c sum.o val.c val.o
9> 执行生成的可执行文件
# ./main
当前线程sleep 2秒
METHOD:epoll
This is Value==
X:10
This is SUM Method!=========HDH
This is Main
Z:30
This is get
Z:400
参考:https://www.cnblogs.com/idyllcheung/p/10641144.html
这篇关于autotools使用——C源文件在不同目录下的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!