embedded Linux C Language (11)--Static library functions and dynamic library functions First,
Static link library
Static links a library is a collection of obj files, usually static links Library with ". A" as the suffix, the name format is generally libxxx.a , generated by program AR. the static link library is linked during the program compilation process, and the related functions of the call have been copied inside the program, and the program runtime and the static link library have no relation.
1.
Creating a static link library
A. Writing source database files
The source library file typically contains . C and . h files,
hello.c File:
#include <stdio.h>
void display (void)
{
printf ("Hello world\n");
}
Hello.h File:
#ifndef __hello_h
#define __hello_h
void display (void);
#endif
B, compile the source library file
Gcc-o hello.o-c hello.c
Generate hello.o target file
C. Archive the target file to generate a static link library file
AR-CR LIBHELLO.A hello.o
D. Publish a static link library
In general, static link libraries need to publish libxxx.a and . h files,and. h files allow third-party developers to understand the functions and function declarations of functions in a static-link library ,thelibxxx.a file is a library that third-party developers link to during the compile link phase after calling functions in the static link library.
2, the use of static link library
A, consult the . h file of the static link library
After you get the published static link library, look at the . h file for the function functions and function declarations of the static link library.
B.a function that uses a static link library
You need to declare a static link library when you use the static link library . h file
#include "hello.h"
int main (int argc, CHAR**ARGV)
{
Display ();
return 0;
}
C. Compiling the project files
When compiling a project file, you need to add the relevant options when compiling the link:
-lpath : Represents the Path directory to search for library files, such as -L. is represented in the current directory.
-lxxx: represents the static link library to be linked to libxxx.a
-static: indicates that all linked libraries are statically loaded
Gcc-o main main.c-l.-lhello
Second,
Dynamic Link library
A dynamic-link library is a library that is loaded when the program runs, and when the dynamic-link library is installed correctly, all programs can use a dynamic library to run the program. A dynamic-link library is a collection of target files that are organized in a dynamic-link library in a special way. The address of a function and variable in a library is a relative address, not an absolute address, and its real address is formed when the program that invokes the dynamic library is loaded.
1, the creation of dynamic link library
A. Writing source database files
The source library file typically contains . C and . h files,
hello.c File:
#include <stdio.h>
void display (void)
{
printf ("Hello world\n");
}
Hello.h File:
#ifndef __hello_h
#define __hello_h
void display (void);
#endif
B, compile the source library file
Gcc-fpic-c Hello.c-o hello.o
Generate the target file hello.o
The purpose of the-FPIC option is to make The code generated by the GCC location-independent
C. Generate a dynamic link library
Gcc-shared-o libhello.so hello.o
Generate a dynamic-link library libhello.so file
The-shared option tells the compiler to generate a dynamic-link library
2.
use of dynamic link libraries
A, check the . h file of the dynamic link library
After you get the published dynamic link library, look at the . h file to see the function functions and function declarations of the dynamic link library.
B.a function that uses a dynamic link library
You need to declare a . h file for a dynamic-link library when using a dynamic-link library
#include "hello.h"
int main (int argc, CHAR**ARGV)
{
Display ();
return 0;
}
C. Compiling the project files
When compiling a project file, you need to add the relevant options when compiling the link:
-lpath : Represents the Path directory to search for library files, such as -L. is represented in the current directory.
-lxxx: indicates that the dynamic link library to be linked is libxxx.so
Gcc-o main main.c-l.-lhello
D. Registering the dynamic-link library file with the library load path in the system environment variable
Method One: Copy the dynamic-link library file to a directory in the library load path in the system environment variable
CP Libhello.so/usr/lib
Method Two: Add the current directory as a library load path in the system environment variable
Add the current working directory to the search path profile for the dynamic link library / in etc/ld.so.conf .
If you do not have the above operation, the runtime program will error:
Error while loading shared Libraries:libhello.so:cannot open Shared object file:no such file or directory
When the program runs, it will load the function execution in the dynamic link library to the appropriate directory.
E, library dependencies when the program runs
the LDD command can query the dependent libraries that are required for the program to run
LDD Main
Linux-vdso.so.1 = (0x00007fff265d8000)
libhello.so =/usr/lib/libhello.so (0x00007f15d8af1000)
libc.so.6 =/lib/x86_64-linux-gnu/libc.so.6 (0x00007f15d8733000)
/lib64/ld-linux-x86-64.so.2 (0x00007f15d8d05000)
This article from "Endless life, Struggle not only" blog, please be sure to keep this source http://9291927.blog.51cto.com/9281927/1790676
Embedded Linux C language (10)--Static library functions and dynamic library functions