Static Libraries

bill.c:

#include<stdio.h>

void  main(char *arg)

{

      printf("bill:we passed %s\n",arg);

}

 

fred.c:

#include<stdio.h>

void fred(int argc)

{

      printf("fred:we pass %d\n",arg);

}

$ gcc -c bill.c fred.c
$ ls *.o

lib.h:

void bill(char*);

vodi fred(int);

program.c:

#include<stdlib.h>

#include"lib.h"

int main()

{

      bill("Hello world!");

}

gcc -c program.c
$ gcc -o program program.o bill.o
$ ./program
bill: we passed Hello World

creat your own static library file:

Now you’ll create and use a library. Use the ar program to create the archive and add your object files to it. The program is called ar because it creates archives, or collections, of individual files placed together in one large file. Note that you can also use ar to create archives of files of any type. (Like many UNIX utilities, ar is a generic tool.)
$ ar crv libfoo.a bill.o fred.o

$ranlib libfoo.a

$ gcc -o program program.o libfoo.a

$./program program.o libfoo.a

$./program

You could also use the –l option to access the library, but because it is not in any of the standard places,you have to tell the compiler where to find it by using the –L option like this:
$ gcc –o program program.o –L. –lfoo
The –L. option tells the compiler to look in the current directory (.) for libraries. The –lfoo option tells the compiler to use a library called libfoo.a (or a shared library, libfoo.so, if one is present). To see which functions are included in an object file, library, or executable program, you can use the nm com-mand. If you take a look at program and lib.a, you see that the library contains both fred and bill,but that program contains only bill. When the program is created, it includes only functions from the
library that it actually needs. Including the header file, which contains declarations for all of the func-tions in the library, doesn’t cause the entire library to be included in the final program.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值