剛好拾起一本書來看,裡面有著Makefile 編譯code的小範例,剛好來練習一下!
通常Makefile都用來多個程式編譯成一個應用程式時候用到
準備3個c檔和1個h檔( mymath.c  mymath.h  three.c  two.c ) 外加 Makefile01
mymath.c

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "mymath.h"


int main(void)
{
int a,b;
srand(time(NULL));
printf("輸入一個整數:");
scanf("%d",&a);


printf("整數 %d 的平方為 %d\n",a,fun2(a));


b=fun3();
printf("隨機產生器得到的數值為: %d\n",b);
return 0;
}




mymath.h

int fun2(int);
int fun3();



two.c

int fun2(int a)
{
    int ret;
    ret=a*a;
    return (ret);
}



three.c
int fun3()
{
    int a;
    a=(rand()%6+1);
    return a;
}


Makefile01
[ -o : 指定輸出檔案名稱 ]
[ -c :  只做編譯不作連結 ]
[ -I :   指定include file 目錄 ]
[ -L :  指定library file 目錄 ]
[ -Wall : 發出gcc 可提供的所有警告訊息 ]

mymath: mymath.o two.o three.o
    gcc -o mymath mymath.o two.o three.o
mymath.o: mymath.c mymath.h
    gcc -c mymath.c
two.o: two.c
    gcc -c two.c
three.o: three.c
    gcc -c three.c


最後只要make -f Makefile01
就可以編譯出mymath bin檔了
-f 後面可以指定要用哪一個Makefile 來編譯
------------------------------------------

root@jsl-System-Product-Name:/home/jsl/code/ex2# make -f Makefile01
gcc -c mymath.c
gcc -c two.c
gcc -c three.c
gcc -o mymath mymath.o two.o three.o

------------------------------------------
rand()會傳回一個亂數數值!
0~RAND_MAX
在stdlib.h中定義的

/* The largest number rand will return (same as INT_MAX).  */
#define RAND_MAX        2147483647