linux脚本: makefile以及链接库

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[5] = {1, 4, 5, 23,17};

void show(int * parry, int size)
{
  int i = 0;
  printf("parry:");
  for(i=0; i<size; i++)
  {
    printf("%d ", parry[i]);
  }
  printf("\n");
}

void sort(int* parry, int size)
{
  int i, j;
  int is_sort = 0 ;
  for(i = 0; i < size - 1 && !is_sort; i++)
  {
    for(j = 0; j < size - 1 - i; j++)
    {
      is_sort = 1;
      if(parry[j] > parry[j+1])
      {
        is_sort = 0;
        swap(&parry[j], &parry[j+1]);
      }
    }
  }
}

void old_sort(int* parry, int size)
{
  int i, j;

  for(i = 0; i< size - 1; i++)
  {
    for(j = 0; j< size- 1 - i ; j++)
    {
         if(parry[j] > parry[j+1])
        {
          swap(&parry[j], &parry[j+1]);
        }

    }
  }
}

void swap(int* a, int* b)
{
  if(a==0 || b==0)
  {
    return;
  }

  *a = *a ^ *b;
  *b = *a ^ *b;
  *a = *a ^ *b;
}

int func()
{
  show(arry, 5 );
  sort(arry, 5 );
  show(arry, 5 );
  return 0;
}

 

//main.c
#include <stdio.h>
int main()
{
  printf("start main\n");
  func();
  return 0;
}

 

#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中查找函数库

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值