Common options for Linux static and shared libraries-C ※preprocessing: only object files are generated and generated. o file-O ※generate the target file. If this option is not included,. out-ANSI ※support For ansi c-include file ※specify the include file name, which is equivalent to # include-Idir ※specifies the path of the header file. If the header file is not in the default path of the system, with this option-I-※cancel-Idir, the compiler will not search for the header file-G ※generate debugging information in the previous option, otherwise, GDB cannot debug-wall. ※generate warning information-O <0-3> ※※the upper-case letter O is not a number 0. The optimization option is-O1 by default, and-O0 is not optimized, the higher the level, the longer the variation time. -Static ※no dynamic connection library is used. Generally, the dynamic library is directly integrated into the program and becomes a part of the program. Therefore, the generated code is relatively large.-Share ※because the dynamic library is used, the code is relatively small, when the dynamic library file does not exist, the program cannot run. -FPIC ※independent code is generated, which is irrelevant to the path. It is generally used with-share. To create a shared library, first review the process of generating and executing executable files. There are four steps. 1 preprocessing: this stage processes all preprocessing commands 2 Compilation: this stage C file generation target file 3 link: at this stage, all the target files and libraries are linked together to generate the final executable program 4. Execute: this stage scans the reference of the shared library, and execute the executable file. The above steps indicate that, when the system starts, the shared library loader creates a reference table (you can use the ldconfig command to update the table ), then, when the file is executed, the system automatically scans the referenced table to obtain the scheduling entry (you can use the LDD command to check the entry of all referenced shared libraries), completes all links of the program, and finally executes the task. The process of creating a shared library and calling: A compilation location is irrelevant to the code. It is easy to imagine that shared libraries are loaded at runtime and dynamic portals must be used. The command is gcc-C-wall-werror-fpic tmp. O 2. Create a shared link library based on the target file. The corresponding command is gcc-shared-o tmp. so TMP. O 3. Use the shared library to create executable files. Gcc-L/pathoflib/tmp-wall-O target dependence-ltmp there are three ways to call the dynamic library: 1. Change LD_LIBRARY_PATH and set the temporary path. Export LD_LIBRARY_PATH =/home/username/FOO: $ LD_LIBRARY_PATH 2 use the GCC option rpath, And the link specifies the path. Gcc-L/pathoflib/-wl,-rpath =/pathoflib/-wall-O target dependence-ltmp 3 shares the shared library with all users. The standard shared library file is stored in/usr/lib or/usr/local/lib. sudo CP-RF/pathoflib/libtmp. so/usr/lib // copy to the standard path sudo chmod 0755/usr/lib/libtmp. so // Add the sudo ldconfig permission to all users // update the loader five for execution. /Target attachment: run the ldconfig-p | grep TMP command to check whether the shared library is successfully loaded. Example: m @ m-T6Series :~ /Workspace/C/sharelib $ ldconfig-p | grep foolibxgdfoo. So (libc6, x86-64) =>/usr/lib/libxgdfoo. So
Generate static library
It is easy to generate a static library. Here is a brief introduction: gcc-c tmp. c-o TMP. O ar CRS libtmp. a tmp. O because the static library is directly copied to the program when it is linked, you can specify the library path and library file when it is directly linked. The executable file is not associated with the corresponding static library during execution. The specific operation is as follows: gcc-O target dependence-L.-ltmp //-L specifies the library path to the current directory, and-L specifies the library file name to run./Target