file directory tree is as follows, as you can see, very simple. 1. libtest/2. |
-- lt.c 3. |
-- lt.h 4. `-- test.c #lt.c 1. 4. 5. #include 6. 7. void myprint(void) 8. { 9. printf("Linux library test!/n" ); 10. } # lt.h 1. 4. 5. void myprint(void); #test.c 1. 4. 5. #include "lt.h" 6. 7. int main(void) 8. { 9. myprint(); 10. return 0; 11. } First look at the static library first made into the static library liblt.a. 1. $ gcc -o lt.o -c lt.c 2. $ ar cqs liblt.a lt.o Again, link, 1. $ gcc test.o liblt.a -o test This time to see his reference again Library situation. 1. $ ldd test 2. linux-gate.so.1 => (0xffffe000) 3. libc.so.6 => /lib/libc.so.6 (0xb7e29000) 4. /lib/ld-linux. So.2 (0xb7f6e000) The dynamic library is made into the dynamic library liblt.so. 1. $ gcc -o lt.o -c lt.c 2. $ gcc -shared -Wall -fPIC -o liblt.so lt.o -shared This option specifies to generate a dynamic link library (let the connector generate a T type of export) Symbol tables, sometimes also generate weakly connected W type export symbols), can not be connected without the external program. Equivalent to an executable file -fPIC: indicates that the code is compiled as a position-independent code. Without this option, the compiled code is position-dependent, so the dynamic loading is done by code copying to meet the needs of different processes, but not The purpose of real code segment sharing. -L.: indicates that the library to be connected is in the current directory -ltest: The compiler has an implicit naming convention when looking up the dynamic link library, that is, prefixing the given name with lib, followed by .so to determine the library Name LD_LIBRARY_PATH: This environment variable indicates the path that the dynamic connector can load the dynamic library. Link method I, copy it into the system library and link it, let gcc find it yourself. $ sudo cp liblt.so /usr/lib 2. $ gcc -o test test.o -llt Here we can see the -llt option, - l[lib_name] specifies the library name, and he will actively search for lib[lib_name].so. The path to this search can be found by gcc --print-search-dirs. Link Method II, manually specify the library path 1. $ cc -o test test.o -llt -B /path/to/lib The -B option here adds /path/to/lib to the path of the gcc search. There is no problem with this link, but the manually linked program in Method II still needs to specify the library path (link and execution are separate). Need to add the system variable LD_LIBRARY_PATH: 1. $ export LD_LIBRARY_PATH=/path/to/lib This time to check the library link status of the test program (method I) 1. $ ldd test 2. linux-gate.so.1 => ; (0xffffe000) 3. liblt.so => /usr/lib/liblt.so (0xb7f58000) 4. libc.so.6 => /lib/libc.so.6 (0xb7e28000) 5. /lib/ld -linux.so.2 (0xb7f6f000) Well, is there a more liblt.so than a statically linked program? Well, this is the biggest difference between static and dynamic. In static case, he loads the library directly into the program. When dynamically linking, he just keeps the interface and separates the dynamic library from the program code. This can improve the reusability of the code and reduce the coupling of the program. In addition, at runtime, to ensure that the main program can find the dynamic library, so the dynamic library is generally released to the system directory, or in a relatively fixed path to the main program, so that regardless of when the main program runs in the local machine, Can find a dynamic library. When the static library only works on the link, the static library file does not exist when running the main program.
For ease of use, we will save the Xshell session. How to transfer the session information saved on
redhat-5.4 U disk installation process Preparation: FbinstTool.exe-softwareredhat-5.4-boot.fba-boot
got a long time (about 3 days), and finally got through. At first I wanted to give up but slowly beg
single, enter single-user mode, then change the startup mode to id:3:initdefault: in /etc/inittab (t
Linux kernel wait queue mechanism principle analysis
Linux operating system partitioning tool Fdisk tool introduction
Linux interrupts and exceptions Basics
Redis several misunderstandings
The problem of creating DBCA error problem under Linux operating system
How to install JDK1.5 under Linux
Linux operating system, Advanced Power Management
Data transfer on the link layer of the linux protocol stack (add interface to the bridge (4))
Linux shutdown command shutdown
Arm linux boot process into the kernel
How to run or demonstrate a Linux system without installation
Hard disk installation under Win7 Red Hat Enterprise Linux 6.0 ES method
How to use wifi sharing wizard
Windows XP system network can not visit the seven major solutions
Resource Sharing - Make the Most of HomeGroup in Windows 7
Ubuntu using the command to delete the PPA repository
Jinshan quickly typed and taught you to learn five strokes lightly
Can the PSP connection computer be used as a PS4?
Hide the system partition with the win7 group policy
Win8 system ten ways to shut down and restart
What should I do if IPV6 cannot connect properly under win10? Win10 open IPV6 method