[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]
In the process of writing software, we need to use others' library functions. As we all know, software is a collaborative project. As a person, you cannot do all the work by yourself. In addition, some excellent open-source libraries on the network have been widely accepted by the industry, and we do not need to waste time on repetitive work.
Since library functions are mentioned, library functions are generally divided into two methods: static library and dynamic library. The difference between the two is actually very small. The static library must be linked to the execution file, while the dynamic library does not need to be linked to the final execution file. How can this problem be solved? That is to say, it doesn't matter whether you delete the static library for the last execution file. However, once you delete the dynamic library, the final execution file won't go around.
Today we are talking about static databases. To show the differences between creating static libraries in Windows and Linux, we first create a static library using Visual C ++ 6.0 on Windows. Source FileCodeVery simple,
# Include "test. H" int add (int A, int B) {return a + B ;}
Header file code is not difficult,
# Ifndef _ test_h # DEFINE _ test_hint add (int A, int B); # endif
If you want to create a static library on Windows, perform the following operations,
(1) Open the Visual C ++ 6.0 tool and click File> New> projects]
(2) select [Win32 static library], write the project name in [project name], and select the Project SAVE address in [location ].
(3) Click OK, continue to finish, and then click OK to create a static library project.
(4) click File> New> files and select C ++ source files ],
(5) Select Add to pProject, add the source file to the project you just created, and enter the file name +. c Suffix in file.
(6) Repeat operations 4 and 5 and add a file name +. h header file.
(7) enter the above Code in the header file and source file respectively, and click the F7 button to generate the *. Lib static library file in the DEBUG directory.
So how should I run it in Linux? In fact, it is very simple to solve the problem by using two commands,
(1) first generate the *. o file and enter gcc-C test. C-O test. O.
(2) Use the AR command to generate a static library and enter ar RC libtest. A test. O.
If there is another hello. c file that uses this static library, for example,
# Include <stdio. h> # include "test. H" int main () {printf ("% d \ n", add (2, 3); return 1 ;}
In fact, it is also very simple. Input a simple command to generate an execution file,
(1) first enter GCC hello. C-O hello./libtest.
(2) Input./hello to verify that the generated execution file is correct.
(3) You can delete the libtest. A file and enter./Hello again to verify that the execution file runs properly.