Create static and dynamic libraries in Linux
Source code
----------- A. cpp ---------------
# Include <cstdio>
Int A (int I)
{
STD: printf ("in a (int A) % DN", I );
Return 0;
}
----------- B. cpp ----------------
# Include <iostream>
Int B (char * s)
{
STD: cout <"in int B (char * s):" <S <STD: Endl;
Return 0;
}
---------- Main. cpp --------------
# Include <cstdio>
Using namespace STD;
Int A (int I );
Int B (char * s );
Int main ()
{
A (5 );
B ("OK ");
Printf ("in mainn ");
Return 0;
}
Static Library
1. Compile a. o B. o file
# G ++-c a. cpp B. cpp
2. Generate the archive file libtest. a c-create R-Add the file to libtest.
# Ar RC libtest. a A. o B. O
3. Specify the static library libtest. A for compilation.
# G ++-O main. cpp libtest.
Running result
#./A. Out
In a (int A) 5
In int B (char * s): OK
In Main
For libtest.CodeCopying is equivalent to static compilation.
Dynamic library
1. compile and generate a. o B. o file-FPIC when generating the. o file, use the relative address to implement code location independence.
# G ++-fpic a. cpp B. cpp
2. Generate the dynamic library libtest. So
# G ++-shared-O libtest. So a. o B. o
3. Specify the dynamic library libtest. So for compilation.
# G ++ main. cpp./libtest. So
View the dynamic Connection Library
# ldd. out
. /libtest. so =>. /libtest. so (0x40014000)
libstdc ++-libc6.2-2. so.3 =>/usr/lib/libstdc ++-libc6.2-2. so.3 (0x4002e000)
libm. so.6 =>/lib/i686/libm. so.6 (0x40071000)
libc. so.6 =>/lib/i686/libc. so.6 (0x42000000)
/lib/ld-linux.so.2 =>/lib/ld-linux.so.2 (0x40000000)
/lib/ld-linux.so.2 =>/lib/ld-linux.so.2 (0x40000000)
Others
1. When dynamically connecting to database 3, if
# G ++ main. cpp libtest. So
The libtest. So library cannot be found in the default library path.
#./A. Out
./A. Out: Error while loading shared libraries: libtest. So: cannot open shared object file: no such
File or directory
View connected database
# Ldd a. Out
Libtest. So => not found
Libstdc ++-libc6.2-2. so.3 =>/usr/lib/libstdc ++-libc6.2-2. so.3 (0x4002c000)
Libm. so.6 =>/lib/i686/libm. so.6 (0x4006f000)
Libc. so.6 =>/lib/i686/libc. so.6 (0x42000000)
/Lib/ld-linux.so.2 =>/lib/ld-linux.so.2 (0x40000000)
2. For ease of use, you can copy the static library libtest. A or the dynamic library libtest. So to/lib or/usr/lib in the search path of the default library,
Compile directly
# G ++ main. cpp-ltest
-L automatic library name extension
# Ldd a. Out
Libtest. So =>/usr/lib/libtest. So (0x4002b000)
Libstdc ++-libc6.2-2. so.3 =>/usr/lib/libstdc ++-libc6.2-2. so.3 (0x4002e000)
Libm. so.6 =>/lib/i686/libm. so.6 (0x40071000)
Libc. so.6 =>/lib/i686/libc. so.6 (0x42000000)
/Lib/ld-linux.so.2 =>/lib/ld-linux.so.2 (0x40000000)
Static condition
# G ++ main. cpp-ltest
# Ldd a. Out
Libstdc ++-libc6.2-2. so.3 =>/usr/lib/libstdc ++-libc6.2-2. so.3 (0x4002b000)
Libm. so.6 =>/lib/i686/libm. so.6 (0x4006f000)
Libc. so.6 =>/lib/i686/libc. so.6 (0x42000000)
/Lib/ld-linux.so.2 =>/lib/ld-linux.so.2 (0x40000000)
3. After archive is generated in static library 2, ranlib libtest. A may need to write the content index to libtest. A for LD connection.
4. The-L option should be placed behind the compiled file. An error will occur before it.
# G ++-ltest main. cpp
/Tmp/ccbqk3ky. O: In function 'main ':
/Tmp/ccbqk3ky. O (. Text + 0xc): Undefined reference to 'a (INT )'
/Tmp/ccbqk3ky. O (. Text + 0x1c): Undefined reference to 'B (char *)'
Collect2: LD returned 1 exit status
At the same time, the following URL has more descriptions:
Http://blog.21ic.com/user1/2663/archives/2009/65033.html
Http://blog.chinaunix.net/u1/43190/showart_425624.html
Http://blog.csdn.net/jiangnanyouzi/archive/2008/11/17/3321150.aspx
Http://blog.csdn.net/laomai/archive/2007/02/16/1510957.aspx
Http://home.lupaworld.com/home.php? MoD = Space & uid = 56821 & Do = Blog & id = 128928