In a Linux operating system, compiling static libraries and dynamic library Linux library files are divided into two types: static library (*. file A, which is equivalent *. lib file), the other is the dynamic library (*. so file, which is equivalent *. DLL file ). During compilation, one method is to compile your own. o file (equivalent to Windows. object files) and static libraries are linked to executable files, the other is to compile only. o file. When running, the function in the dynamic library is called, and then loaded into the dynamic library. The compilation method is as follows: 1. If the source file of the static library is main. C and mystatic. C. 1) Compile mystatic. c into the static library libmystatic. A, and call the functions in this library in Main. C. Gcc-c-I inlcude-O mystaic. O mystatic. C // set mystatic. c. compile it into the target file mystatic. O ar-Rs lib/libmystaic. A mystatic. O // set the target mystaic. O adds libmystatic. library A (the Library file must start with LIB), if libmystatic. A does not exist. Create this database. 2. C is compiled into an executable file test. Gcc-I include-l Lib-O test main. c-lmystatic-static is compiled successfully, and libmystatic is generated successfully under the folder Lib. A library file, and the compiled executable program test uses the library libmystatic. A 2. the source file of the dynamic library is: Main. c, mydynamic. c. 1) Compile mydynamic. c into the dynamic library libmydynamic. So, and call the functions in this library in Main. C. The GCC-I include-shared-O lib/libmydynamic. So mydynamic. C // library file must start with LIB 2) Compile main. c into an executable file test. Gcc-I include-l Lib-O test main. C-lmydynamic // without the parameter-static, the default value is dynamic compilation. 3) run the executable program test. When you run the test program, the system searches for mydynamic. So in the default library file directory. Add the compiled dynamic library directory to the system. The specific method is as follows: Modify ~ /. Bash_profile (this file is a hidden file, use LS-~ /Can be displayed.) add LD_LIBRARY_PATH = *** # (*** is libmydynamic. so directory) Export LD_LIBRARY_PATH: log on to the system again and run the executable file test.
3. Use the dlopen dlsynm dlerror dlclose function in the shared library DL during compilation.