linux使用autotools生成可执行文件、静态库、动态库Makefile的流程介绍

该博客详细介绍了如何利用Autotools在Linux环境中生成可执行文件、静态库和动态库。首先,通过编写configure.in和Makefile.am文件,然后依次执行autoscan、aclocal、autoconf、autoheader、automake等命令生成configure脚本和Makefile。通过configure生成Makefile,并进行编译和链接,最终得到可执行程序。对于生成静态库和动态库,博主还特别讲解了如何处理多个目录下的源码,并配置Makefile.am文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Linux下,工程管理器make是可用于自动编译、链接程序的实用工具。我们要做的是写一个Makefile文件,然后用make命令来编译、链接程序。Makefile的作用就是让编译器知道要编译一个文件需要依赖其他的哪些文件。
    GNU autotools作用:收集系统配置信息并自动生成Makefile文件。
    GNU autotools主要包括三个工具:autoconf、automake、libtool。

 

生成可执行文件:

主要步骤:
1.开发者要书写的文件主要是configure.inMakefile.am这两个文件
2.运行autoscan检测源文件生成configure.scan并修改成configure.in
3.编辑configure.in
4.由aclocal命令生成aclocal.m4
5.运行autoconf生成configure脚本
6.运行autoheader生成config.h.in文件
7.创建并编辑Makfile.am
8.运行automake生成makefile.in  有时候可能要加automake --add-missing
9.运行configure脚本生成Makefile
10.make
11.运行可执行程序main
 

同一个目录

1.写好源码main.c hello.c hello.h
[root@localhost 1]# ls
hello.c  hello.h  main.c

[root@localhost 1]# cat main.c
#include <stdio.h>
int main(void)
{
        printf("main start!\n");
        print();

        return 0;
}

[root@localhost 1]# cat hello.c
#include "hello.h"
void print(void)
{
        char buf[32] = "123";
        strcat(buf, "456");
        printf("%s\n", buf);
        printf("hello,world!\n");

        return ;
}

[root@localhost 1]# cat hello.h
#ifndef __HELLO_H__
#define __HELLO_H__
#include <stdio.h>
#include <string.h>
void print(void);
#endif

2.执行命令:autoscan
[root@localhost 1]# autoscan
autom4te: configure.ac: no such file or directory
autoscan: /usr/bin/autom4te failed with exit status: 1
[root@localhost 1]# ls
autoscan.log  configure.scahello.c  hello.h  main.c

3.编辑configure.scan
[root@localhost 1]# vim configure.scan
//只增加这2项:
AM_INIT_AUTOMAKE(main, 1.0)
AC_OUTPUT(Makefile)
[root@localhost 1]# mv configure.scan configure.in

4.aclocal
[root@localhost 1]# aclocal
[root@localhost 1]# ls
aclocal.m4  autoscan.log  hello.c  main.c  autom4te.cache  configure.in  hello

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值