The so file is similar to the DLL file under Windows. 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 main function is not required in the source file of the-so file, and will not be executed even if there is one. -GCC needs to add
-fpic option when compiling, so that GCC can produce location-independent code.
$ (CC)-shared-o $ (EXEC) $ (OBJS) -At link time GCC uses the
-shared option, which indicates that a shared library file is generated.
$ (CC)-wall-g-fpic-o [email protected]-C $< -the Shared library file name
starts with Lib and has a. so extension. use the so file to also provide a header file in the following format:#ifndef Test_h_#define TEST_H_**********#endif
How to use the so file
-for Linux to find the location of so files, you need to add the export ld_library_path= $LD _library_path:. Bash_profile in. , and let it take effect. -or put so files into the Linux system directory/usr/lib/, but not recommended. -To use the so file in a C file, you first need an # include related header file. adding the-l parameter to the-GCC link indicates the so file storage path, and the-l parameter indicates the so filenamesuch as Gcc-l-ltest-o a A.O-L. is searching for so files under the current path-ltest is the library file to link to libtest.so when calling the C language shared library file in C + + files, you need to modify the function declaration: add extern "C" to the header file, or you will get an error when sharing the library with g++ link. #ifndef Share_h_#define SHARE_H_
extern "C" int max (int a, int b);
extern "C" int Add (int a, int b);#endifto be compatible with C + + and C hybrid programming, you can improve the#ifndef Share_h_#define SHARE_H_
#ifdef __cplusplus
extern "C"
{
#endifint max (int a, int b);int Add (int a, int b);
#ifdef __cplusplus
}
#endif#endif/* Test_h_ * /
Linux Shared Libraries so file