Making And Using Library

picked from the original address : http://www.delorie.com/djgpp/doc/ug/larger/archives.html

A library is a collection of objects, much like your town library is a collection of books. When building your program, you can make one or more libraries available to gcc and it will use the objects in those libraries to help it complete your program. For example, all the standard C functions (like printf and exit) are in the C library, which is lib/libc.a in a DJGPP installation. When you link your program, gccadds whatever objects in C library it needs, based on what your program calls. It doesn't link them all in, though, which would be a waste of time and space.

To make your own library, you must first compile each of your sources into an object.

  • gcc -c hello.c
    gcc -c goodbye.c

Next, you use the ar command to create the library and put the objects in it.

  • ar rvs mystuff.a hello.o goodbye.o

Each of the rvs letters means something. r means to replace objects in the library with new ones on the command line. Since the library doesn't contain any objects, this effectively means add them to the library. The next time you run it, it will replace the old version with the new version. There are also options for extracting and deleting objects in the library. The v option means verbose, which tells ar to keep you informed about what it's doing. The s option tells ar to create a symbol table, which is something extra that gcc needs when using a library. You could use ar to make libraries of anything - text, images, sounds, etc. When you use ar to make libraries of objects, use the s option.

To use the library, simply add it to your gcc link line just like you would any other object.

  • gcc main.c mystuff.a -o main.exe

It's very important to list libraries in the correct order. As gcc scans the command line, it pulls in what it needs so far. Each time it sees an object, it adds it to the program. Each time it sees a library, it pulls in only the objects that it needs at that point. You should always list libraries after the objects.

Another way to use a library is to give it a special name, put it in a special place, and use a special option for it. Usually, this is used only for the system libraries, because then you don't have to worry about where they are - gcc knows where to find them. If you are building a system library (like the C library, libc.a), you need to do three things.

  1. The name of your library must start with lib, like libc.a or libstuff.a. This extra prefix is a convention that designates your file as a system library.
  2. Your library must be moved into djgpp's lib directory. For example, lib/libc.a and lib/libm.a are in there. gcc knows to look in djgpp's lib directory for system libraries.
  3. When you link your program, you use the -l option to specify only the middle part of your library. For example, if your library is libstuff.a you would use gcc -lstuff to link your library (listing -lstuff after the objects, of course)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值