Some basic knowledge of Linux environment dynamic library and Static library is summarized,
First, summarize the compile steps of the static library.
1 generate the corresponding. o file based on the. cpp or. c Files First
2 add several. o files using the AR-CR command to generate the Libname.a file
The LIBNAME.A is a static library, name is the names of the static libraries,
Can be named according to the module function.
As an example,
In the current directory, there is a mymethod.h file
Implement these two functions in the mymethod.cpp.
There is a main.cpp file under the current directory
Mymethod.cpp encapsulated into static and dynamic libraries for ease of use
First compile the mymethod.cpp into the target file
g++-C Mymethod.cpp-o MYMETHOD.O
Then generate a static library based on the target file
AR-CR libmymethod.a MYMETHOD.O
The AR command can be used to create, modify, or present a single module from the library.
The-c option means creating a library. Regardless of whether the library exists, you will create
The-r option indicates that the module is inserted into the library, and if there is a corresponding module in it, the update
If you insert more than one. o file into the library. A, you just need to write the blanks separately.
Format
AR-CR Static library Libname.a name1.o NAME2.O
The static library is named with a rule, in the Format lib+ library name +.a
After generating the static library libmymethod.a,
Compile main.cpp connect static library build executable file main
g++ main.cpp-o main-i./-L./-lmymethod
-I option indicates header file path
-L represents the path to a static or dynamic library
Link Static library written as-L library name:-lmymethod
Run./main can output results
The following is a compilation of a dynamic library
The name of the dynamic library is lib+ library name +.so
Dynamic libraries require the use of the-shared option and the-fpic option
g++-C -fpic Mymethod.cpp-o MYMETHOD.O
-fpic indicates that the code is address-independent and does not need to be relocated by different modules
The dynamic library. So file is then generated according to the target file
g++-shared-o libmymethodso.so MYMETHOD.O
The-shared option indicates that the generated. So dynamic library file
The next step is to link the dynamic library
g++ main.cpp-o main-i./-L./-LMYMETHODSO
You cannot run directly at this time./main Otherwise, you will be prompted not to find the corresponding dynamic library
The above steps can be written directly to a sentence
g++-shared-fpic Mymethod.cpp-o libmymethodso.so
We now have several ways to configure the path of the dynamic library
First Kind
Export ld_library_path="./: $LD _library_path"
This approach configures the dynamic library path to Ld_library_path, which is only temporarily active
The second Kind
Using the ldconfig mechanism (root permission required):
First, create a. conf file under/etc/ld.so.conf.d/,
As libmymethod.conf, the content is the absolute path to the directory where the shared library resides
Then, run the Ldconfig
The first is relatively simple and can be run directly after configuration./main
The second type of operation enters the/etc/ld.so.conf.d/,
Then create the mymethod.conf
Open mymethod.conf with VI, add the absolute path of the dynamic library
/home/secondtonone/soando
Last run Ldconfig, it will be effective.
This will allow you to run the./main
Now make a summary of the dynamic library path configuration
Resolving Shared Library Path issues
gcc/g++ Compile Period:
Use the L option to specify the directory where the shared library of the desired link is located (if the shared library or its
The file is located in/usr/lib,/usr/local/lib, or specified by Ld_library_path
directory, you do not need to specify this option)
Use the L option to specify the shared library name for the desired link (no prefix, suffix)
Program Run Time:
Use the LD_LIBRARY_PATH environment variable to specify the shared library where you want to reference
Directory (temporary designation or permanent write-related files, universally applicable to UNIX world)
Using the ldconfig mechanism (root permission required):
First, create a. conf file under/etc/ld.so.conf.d/,
As libmymethod.conf, the content is the absolute path to the directory where the shared library resides
Then, run the Ldconfig
My public number, thank you for your attention.
Linux Environment compilation dynamic library and Static library summary