Dynamic Link Library is a common software component technology that provides basic services in a variety of operating systems. For example, the Win32 kernel is composed of several DLL files. This technology is also implemented in the Linux operating system, that is, the standard ojbect object of Linux, and the corresponding file extension is. So. The following is a simple example to introduce Linux Standard objects. Our standard object file contains a function that does not need to declare the export symbol. You only need to set the compiler. As follows:
# Include <stdio. h>
Void show ()
{
Printf ("standard object by gashero/N ");
}
Save as the myso. c file and compile it as follows:
$ Gcc-FPIC-shared-O libmyso. So myso. c
Execute the command to generate a libmyso. So file. According to the naming convention of the Linux Standard object, you should add the "lib" prefix before the library name, although not required. The compile switch-FPIC indicates that function symbols can be redirected, and-shared indicates that the compilation result is a standard object.
Different from win32dll, all functions in Linux Standard objects are exported directly and can be accessed by the calling program. Below we write a calling program:
# Include <stdio. h>
Int main ()
{
Printf ("invoke my so/N ");
Show ();
Return 0;
}
Save as invoke. C and compile it according to the GCC switch below:
$ Gcc-O test invoke. C./libmyso. So
Compile and generate the test executable file. The last line of the above compilation condition must be the called standard object file name. Note that the path must be included. If you only use libmyso. So, make sure that the file is under the accessible path. In this example, the file name "./libmyso. So" is in the current path and the relative path is used.
The test output is as follows:
$./Test
Invoke my so
Standard object by gashero