linux脚本: makefile以及链接库

时间:2022-04-04 20:24:03

Linux makefile 教程 非常详细,且易懂 http://blog.csdn.net/liang13664759/article/details/1771246

//sort.c
#include <stdio.h>
#include <string.h> void swap(int* a, int* b);
int arry[] = {, , , ,}; void show(int * parry, int size)
{
int i = ;
printf("parry:");
for(i=; i<size; i++)
{
printf("%d ", parry[i]);
}
printf("\n");
} void sort(int* parry, int size)
{
int i, j;
int is_sort = ;
for(i = ; i < size - && !is_sort; i++)
{
for(j = ; j < size - - i; j++)
{
is_sort = ;
if(parry[j] > parry[j+])
{
is_sort = ;
swap(&parry[j], &parry[j+]);
}
}
}
} void old_sort(int* parry, int size)
{
int i, j; for(i = ; i< size - ; i++)
{
for(j = ; j< size- - i ; j++)
{
if(parry[j] > parry[j+])
{
swap(&parry[j], &parry[j+]);
} }
}
} void swap(int* a, int* b)
{
if(a== || b==)
{
return;
} *a = *a ^ *b;
*b = *a ^ *b;
*a = *a ^ *b;
} int func()
{
show(arry, );
sort(arry, );
show(arry, );
return ;
}
//main.c
#include <stdio.h>
int main()
{
printf("start main\n");
func();
return ;
}
#Makefile
a.out: main.o sort.o
gcc -o a.out main.o sort.o
sort.o: sort.c
gcc -o $@ -c $^
main.o : main.c
gcc -o $@ -c $^ clean:
rm -rf *.o a.out

#~/oucaijun/c/>make
make: Warning: File `Makefile' has modification time 43 s in the future
gcc -o main.o -c main.c
gcc -o sort.o -c sort.c
gcc -o a.out main.o sort.o
make: warning: Clock skew detected. Your build may be incomplete.
#~/oucaijun/c/>./a.out
start main
parry:1 4 5 23 17
parry:1 4 5 17 23

#~/oucaijun/c/>make clean
rm -rf *.o a.out

改Makefile:

#Makefile
a.out: main.o sort.o
gcc -o a.out main.o sort.o
*.o: *.c
gcc -o $@ -c $^ clean:
rm -rf *.o a.out

make
make: Warning: File `Makefile' has modification time 85 s in the future
cc -c -o main.o main.c
cc -c -o sort.o sort.c
gcc -o a.out main.o sort.o
make: warning: Clock skew detected. Your build may be incomplete.

改Makefile:

#Makefile
a.out: main.o sort.o
gcc -o a.out main.o sort.o
%.o: %.c
gcc -o $@ -c $^ clean:
rm -rf *.o a.out

make

make: Warning: File `Makefile' has modification time 71 s in the future
gcc -o main.o -c main.c
gcc -o sort.o -c sort.c
gcc -o a.out main.o sort.o
make: warning: Clock skew detected. Your build may be incomplete.

改Makefile

#Makefile
objs := main.o sort.o
a.out: $(objs)
gcc -o a.out $^
%.o: %.c
gcc -o $@ -c $^ clean:
rm -rf *.o a.out

  make结果仍同上.

objs := main.o sort.o
target := a.out
$(target): $(objs)
# gcc -o $@ $^ #ok
gcc -o $(target) $(objs) #ok
%.o: %.c
gcc -o $@ -c $^ clean:
rm -rf *.o a.out

  make结果仍同上.

Makefile以及链接库  http://blog.csdn.net/zkf11387/article/details/8039249

Makefile 里 -l和-L的区别  http://blog.csdn.net/dreamxu/article/details/6259206

gcc -o test test.c -L. -lcrexr64

-L.表示在当前目录(.)中查找函数库,-lcrexr64表示使用名为libcrexr64.a的函数库

-L/usr/lib -L/opt/app/oracle/product/10.2.0.1/lib 表示在/usr/lib 以及 /opt/app/oracle/product/10.2.0.1/lib中查找函数库