linux下编译C程序

先上张图:


图片来源见最后
  • autoscan:扫描源代码以搜寻普通的可移植性问题,比如检查编译器,库,头文件等,生成文件configure.scan,它是configure.ac的一个雏形
  • aclocal:根据已经安装的宏,用户定义宏和acinclude.m4文件中的宏将configure.ac文件所需要的宏集中定义到文件 aclocal.m4中。aclocal是一个perl 脚本程序,它的定义是:“aclocal - create aclocal.m4 by scanning configure.ac”
  • automake:将Makefile.am中定义的结构建立Makefile.in,然后configure脚本将生成的Makefile.in文件转换 为Makefile。如果在configure.ac中定义了一些特殊的宏,比如AC_PROG_LIBTOOL,它会调用libtoolize,否则它 会自己产生config.guess和config.sub
  • autoconf:将configure.ac中的宏展开,生成configure脚本。这个过程可能要用到aclocal.m4中定义的宏

安装

我是lubuntu环境,运行命令:

sudo apt install automake
sudo apt install autoconf

编译helloworld

[hello.c]
#include 

int main(int argc, char const *argv[])
{
    printf("hello,world");
    return 0;
}

进源码目录运行命令:
autoscan
mv configure.scan configure.ac
vi configure.ac,把AC_INIT一行改为AC_INIT(hello),紧接下一行加上AM_INIT_AUTOMAKE(hello, 1.0)
aclocal
autoheader
autoconf
automake,使用automake --add-missing安装缺少的包
./configure --prefix(安装目录)
make
make install
至此安装目录就已经生成了可运行程序了。

看懂makefile

  • 规则
    target(目标)...:prerequisites(依赖)...
    command(命令,tab键开头)
  • 变量
    $@: 目标文件,$^: 所有依赖文件,$<: 第一个依赖文件
  • 文件
    make默认寻找顺序:GNUmakefile、makefile、Makefile,make -f file

(我这里只是够用为主没有深入,有兴趣可以去automake等相关官网详细了解)

——————————————————————————————

  • 摘抄来源:https://www.cnblogs.com/bugutian/p/5560548.html

你可能感兴趣的:(linux下编译C程序)