创建动态库的流程

   Previous section   Next section


8.5. Installing Shared Libraries

The ldconfig program does all the hard work of installing a shared library. You just need to put the files in place and run ldconfig. Follow these steps:

1.
Copy the shared library to the directory in which you want to keep it.

2.
If you want the linker to be able to find the library without giving it a -L directory flag, install the library in /usr/lib, or make a sym-link in /usr/lib named libname .so that points to the shared library file. You should use a relative symlink (with /usr/lib/libc.so pointing to ../../lib/libc.so.5.3.12), instead of an absolute symlink ( /usr/lib/libc.so would not point to /lib/libc.so.5.3.12).

3.
If you want the linker to be able to find the library without installing it on the system (or before installing it on the system), create a libname .so link in the current directory just like the system-wide one. Then use -L. to tell gcc to look in the current directory for libraries.

4.
If the full pathname of the directory in which you installed the shared library file is not listed in /etc/ld.so.conf, add it, one directory path per line of the file.

5.
Run the ldconfig program, which will make another symlink in the directory in which you installed the shared library file from the soname to the file you installed. It will then make an entry in the dynamic loader cache so that the dynamic loader finds your library when you run programs linked with it, without having to search many directories in an attempt to find it. [4]

[4] If you remove /etc/ld.so.cache, you may be able to detect the slowdown in your system. Run ldconfig to regenerate /etc/ld.so.cache.

You need to create entries in /etc/ld.so.conf and run ldconfig only if you are installing the libraries as system libraries—if you expect that programs linked against the library will automatically work. Other ways to use shared libraries are explained in the next section.

8.5.1. Example

As an extremely simple but still instructive example, we have created a library that contains one short function. Here, in its entirety, is libhello.c:

1: /* libhello.c */
2:
3: #include <stdio.h>
4:
5: void print_hello(void) {
6:     printf("Hello, library./n");
7: }

Of course, we need a program that makes use of libhello:

1: /* usehello.c */
2:
3: #include "libhello.h"
4:
5: int main (void) {
6:     print_hello();
7:     return 0;
8: }

The contents of libhello.h are left as an exercise for the reader.

In order to compile and use this library without installing it in the system, we take the following steps:

1.
Use -fPIC to build an object file for a shared library:

gcc -fPIC -Wall -g -c libhello.c

2.
Link libhello against the C library for best results on all systems:

gcc -g -shared -Wl,-soname,libhello.so.0 -o libhello.so.0.0 /
        libhello.o -lc

3.
Create a pointer from the soname to the library:

ln -sf libhello.so.0.0 libhello.so.0

4.
Create a pointer for the linker to use when linking applications against -lhello:

ln -sf libhello.so.0 libhello.so

5.
Use -L. to cause the linker to look in the current directory for libraries, and use -lhello to tell it what library to link against:

gcc -Wall -g -c usehello.c -o usehello.o
gcc -g -o usehello usehello.o -L. -lhello

(This way, if you install the library on the system instead of leaving it in the current directory, your application will still link with the same command line.)

6.
Now run usehello like this:

LD_LIBRARY_PATH=$(pwd) ./usehello

The LD_LIBRARY_PATH environment variable tells the system where to look for libraries (see the next section for details). Of course, you can install libhello.so.* in the /usr/lib directory and avoid setting the LD_LIBRARY_PATH environment variable, if you like.


   Previous section   Next section
Top
 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值