so files are shared libraries in Linux, similar to DLLs under Windows. The functions in the so file can be called by multiple processes, providing the maximum possible reuse of binary code. Shared libraries can make maintenance of your code much easier, and when bugs are fixed or new features are added, users only need to get the upgraded so and install them. Note: Even though different processes call the same so file, the shared library does not enable communication between different processes because the same so is loaded into different memory spaces by different processes. So file compilation method --The source file of so file does not need the main function, even if there is not executed. --GCC needs the add-fpic option when compiling, which enables GCC to generate location-independent code. in the operating system, when executing a process, the code of the process is loaded into memory, the system assigns a portal address to the program (usually the address of the main function), the address of other functions in this program is relative address (relative to the entry address), So in the process, only the entry address of the program is the absolute address, the address of the other functions are relative addresses, for so shared library file, at compile time do not need to give the so file relative address, because so file does not have an entry address, it is used to be called by other processes When linking, GCC usesthe-shared option, which indicates that a shared library file is generated. --The shared library file name starts with Lib and has a. so extension.
so File Usage --in order for Linux to find the location of so files, you need to add export ld_library_ in. bash_profile Path= $LD _library_path:. Because Linux does not look for executable files in the current directory by default, so is also an executable file, so the so file also needs to configure environment variables to increase local lookups. --or put so files into the Linux system directory/user/lib (not recommended) -- using the so file in a C file requires an # include related. h file first. The --GCC link adds the-l parameter to indicate so file storage path,-l (l) parameter executes so file name to libtest.so file under current path example: GCC - L.-ltest-o a a.o where -l. Search for so file under current path - Span style= "color: #000000;" >ltest meaning to link libtest.so This library file -o a means that the compiled executable file is named a
When we include the Test.h file in the CPP file, Connect with the g++ libtest.so This library will be an error in order for us to write so file colleagues can be called C or C+ + , we need to modify the H file in the function declaration section. Add precompiled instructions with __cplusplus (two underscores). __cplusplus (two underscores) isa predefined macro for the C + + compiler, such as when using g++, the macro is defined in advance. extern"C" extern This is a C + + keyword , C language does not have this keyword, indicating that this is a C language function --NOTE: " C "This character is capitalized
Linux Linux shared libraries