Linux C language library package

We usually put some public functions made into a library for use by other programs.

The library is divided into two kinds of static and dynamic libraries.
Static library program compiled will be connected to the object code, you will no longer need the static library program is running.
Dynamic library at the assembly time and is not connected to the object code, but the program was only running is loaded, so the program will also be required to run a dynamic inventory.

In this paper, by way of example to illustrate how to create static and dynamic libraries in Linux, as well as use them.

Step 1: Editing program obtained -hello.h exemplified, hello.c and main.c;
hello.h (see procedure 1) header file for the library.
hello.c (see procedure 2) a source program library, which contains the common functions Hello, this function will output on the screen "Hello XXX!".
main.c (see program 3) test libraries for the main program file, in the main program calls a public function hello.


Program 1: hello.h

 #ifndef HELLO_H
 #define HELLO_H
 
 void hello(const char *name);
 
 #endif //HELLO_H

Program 2: hello.c

 #include <stdio.h>
 
 void hello(const char *name)
 {
 	printf("Hello %s!\n", name);
 }

Program 3: main.c

 #include "hello.h"
 
 int main()
 {
 	hello("everyone");
 	return 0;
 }

Step 2: Compile hello.c into a .o file;
whether static library, or DLL files are created by .o. Therefore, we must first compile the source code to gcc hello.c by .o file.
Type the following command at the system prompt to get hello.o file.
# gcc -c hello.c

We run the ls command to see if the survival of the hello.o file.
# ls
hello.c hello.h hello.o main.c

In the ls command results, we see hello.o file, this step is complete.


Step 3: Create a static library .o files;
naming static library file name is prefixed with lib, followed with a static library name, extension .a. For example: We will create a static library called myhello, the static library file name is the libmyhello.a. When you create and use static libraries, you need to pay attention to this point. Create a static library with the ar command.
Type the following command at the system prompt to create static library file libmyhello.a.
# ar cr libmyhello.a hello.o

We also run the ls command to view the results:
# ls
hello.c main.c hello.h hello.o libmyhello.a


Step 4: Use a static library in the program;
static libraries make over, how it functions inside of it? Only need to be included in the use of these public functions of the source of these prototype declaration public function, and then specify a static library name when generating an object file with the command gcc, gcc will connect to the target file from a public static function in the library. Note, gcc will be prefixed with the static lib in front of the library name, and then append a static library file name extension .a get to find the static library file.
In the program 3: main.c, we include the header files hello.h static library, then call a public function hello directly in the main program in the main. Mr. below as the target program hello, hello and then run the program and see what happens.
# gcc -o hello main.c -L. -lmyhello
# ./hello
Hello everyone!

We try to delete a static library files whether public function hello really connect to the target file in the hello.
# rm libmyhello.a
# ./hello
Hello everyone!
Program running as usual, static library is already connected to a public function in the target file.


Step 5: Create a DLL file by the .o file;
dynamic library file naming names and static library file name naming convention similar, but also an increase in the dynamic library name prefix lib, but the file extension is .so. For example: We will create a dynamic library named myhello, then the dynamic library file name is libmyhello so.. Gcc used to create a dynamic library.
Type the following command at the system prompt to obtain dynamic library files libmyhello. So.
# gcc -shared -fPCI -o libmyhello.so hello.o

We still use the ls command to see if the dynamic link library file is generated.
# ls
hello.c hello.h hello.o libmyhello. so main.c


Step 6: Using a dynamic library programs;
using dynamic library and the use of a static library exactly the same program, which also contains a function prototype declaration in common use these public functions to the source, and then generates a target command gcc file indicates the dynamic library name compile time. We run the gcc command to generate the target file, and then run it to see the results.
# gcc -o hello main.c -L. -lmyhello
# ./hello
./hello: error while loading shared libraries: libmyhello so:. can not open shared object file: No such file or directory
Oh! error. Quick look at the error message, the original can not find the dynamic library files libmyhello. So. The program runs, it needs to find the dynamic library files in / usr / lib and / lib and other directories. If found, the dynamic load library. Otherwise, the program terminated with an error similar to the above operation. We will copy the files libmyhello.so to the directory / usr / lib, and then try.
# mv libmyhello.so /usr/lib
# ./hello
Hello everyone!
Success. It also further illustrates the dynamic runtime library is needed.
We look back and found that the use of static and dynamic libraries compiled into gcc command target program uses exactly the same, that when the static and dynamic library when the same name, which gcc command library to use it? Holding of the problem will be sued in the end the mood to give it a try.
In addition to first remove all .c and .h files, restored to our state just finished editing the sample program.
# rm -f hello hello.o /usr/lib/libmyhello.so
# ls
hello.c hello.h main.c

Again to create static library files and dynamic library files libmyhello.a libmyhello. So.
# gcc -c hello.c
# ar cr libmyhello.a hello.o
# gcc -shared -fPCI -o libmyhello.so hello.o
# ls
hello.c hello.h hello.o libmyhello.a libmyhello. so main.c

Through the above last ls command, you can find a static library files and dynamic library files libmyhello.a libmyhello.so have been generated and are in the current directory. Then, we run the gcc command to use the library myhello generate an object file hello, and run the program hello.
# gcc -o hello main.c -L. -lmyhello
# ./hello
./hello: error while loading shared libraries: libmyhello so:. can not open shared object file: No such file or directory

It is easy to know from the results of the program run hello, when the static and dynamic library of the same name, gcc command will give priority to the use of dynamic libraries.

Published 21 original articles · won praise 11 · views 20000 +

Guess you like

Origin blog.csdn.net/hexf9632/article/details/95168938