本教程使用的是centos7,目前目錄下有5個C檔案,任務是将他們生成makefile檔案

檔案内容如下:
// add.c
int myadd(int a, int b)
{
return a+b;
}
// mul.c
int mymul(int a, int b)
{
return a*b;
}
// div.c
int mydiv(int a, int b)
{
return a/b;
}
// x.h
int myadd(int a, int b);
int mymul(int a, int b);
int mydiv(int a, int b);
// x.c
#include <stdio.h>
#include "x.h"
int main()
{
int a = 100;
int b = 12;
int add, mul, div;
add = myadd(a, b);
mul = mymul(a, b);
div = mydiv(a, b);
printf("%d + %d = %d\n", a, b, add);
printf("%d * %d = %d\n", a, b, mul);
printf("%d / %d = %d\n", a, b, div);
return 0;
}
1. 安裝相關軟體
CentOS的安裝指令
sudo yum install autoconf automake
Ubuntu的安裝指令
sudo apt-get install autoconf
2. 使用autoscan指令
autoscan
這時目錄産生了兩個新檔案:
3. 把configuire.scan重命名為configure.ac
mv configure.scan configure.ac
4. 修改configure.ac的檔案内容
vim configure.ac
5. 使用aclocal指令,産生了幾個新檔案
aclocal
6. 使用autoconf指令
autoconf
7. 使用autoheader指令
autoheader
8. 編寫 Makefile.am檔案,寫入如下資訊,儲存。注意Makefile.am檔案中第2行、第3行中的demo與上面第4步configure.ac檔案中的demo名字要一緻。
vim Makefile.am
Makefile.am檔案内容:
AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS=demo
demo_SOURCES= x.c add.c mul.c div.c
9. 使用 automake --add-missing
automake --add-missing
10. 執行configure檔案,此時生成了Makefile檔案
./configure
11. 執行make,此時生成了之前設定的可執行檔案demo
make
12. 運作生成的可執行檔案demo
./demo
完成!