The concept of the dynamic link library is still vague. My understanding is:
Take some common Code , Such as functions and classes, is compiled into a "package" that is, DLL (in Windows) or so (in Linux) file,
For other Program When used, you can directly call closed functions to reuse the implemented code, saving
Hard Disk Space (this may be secondary). You can use VC ++ in widows to conveniently generate DLL files.
Various compilation commands are a great challenge for cainiao programmers like me.
The following is a simple example to illustrate how to generate a. So file and use it:
1. I have several files:
Configmap. cpp
Configmap. H (read configuration file class) getwinstate. cpp getwinstate. H (ice interface file, generated by slice)
Getwinsysstate. cpp getwinsysstate. h
(Remote interface implementation file)
Socdbinfo. cpp (A encapsulated class used to call the ice interface for related operations)
Note: This program is used to obtain some system information on the remote host (Windows), such as CPU usage, hard disk usage, and data.
Database Connection status, memory usage, etc.
I want to use these files to generate a dynamic link library. So, mainly by calling classes in socdbinfo. cpp, and then using them in any system (Linux.
2. Start Compilation: $ C ++
-I.-I $ ice_home/include-C
*. Cpp
After compilation, generate the connection file (which I have always called, maybe not), that is
3. Generate a dynamic link library: $ C ++-shared
-O libmyapp. So *. O-L $ ice_home/lib-lice
-Liceutil
In this way, the libmyapp. So file is generated, that is,
4. Use the Dynamic Link Library:
Create two files demo. cpp
Demo. H (using some functions provided by libmyapp. So ),
In Demo. H, the functions and struct provided by libmyapp. So are declared,
Demo. h: the code is as follows:
Struct
Memoryinf
{
Int totalmem; // ×üäú'hangzhou' ó ð ¡
Int
Validmem ¡
Int virtualmem ¡
};
Struct
Diskinf
{
Int totalspace ¡
Int
Freespace ¡
};
Struct dbinf
{
Int
Dbstat; // zookeeper contains zookeeper â ×'zookeeper: "0" ± í ~~~~~~~â '~ú~ø ± {×, "1" ±í ~~~~~~~~â ~~~£, "2" ± í~~~~~~~~â '~~~~~~~~× '~~~」
Int
Dbconnnum; // zookeeper **********************
};
Bool inner (char
* Reshostip); // zookeeper» *****************
Bool desicecom (); // yunú» baiice ío ~~~~~~
Memoryinf
Getmeminf ();
Int getcpuinf (); // μ ã µ½ CPU quota ó â ê
Dbinf
Getdbstat (); // μ ã µ ê ¾ â
Diskinf getdiskstat (); // µã µ%ó² %%%%%%%%%%%%%%%%%%
Note: Garbled text is because Chinese characters are not supported in Linux, and annotations are unnecessary.
Demo. cpp: the code is as follows:
# Include
<String. h>
# Include <iostream>
# Include "demo. H"
Int main (INT argc, char * argv [])
{
Memoryinf mymem;
Diskinf mydisk;
Dbinf mydb;
Incircle (
Argv [1]);
Mydisk = getdiskstat ();
Mymem = getmeminf ();
Mydb =
Getdbstat ();
Printf ("disk total
Space: % d \ n ", mydisk. totalspace );
Printf ("disk freespace
Space: % d \ n ", mydisk. freespace );
Printf ("memory
Totalmem: % d \ n ", mymem. totalmem );
Printf ("validmem: % d \ n", mymem. validmem );
Printf ("virtualmem: % d \ n", mymem. virtualmem );
Printf ("dbconnnum: % d \ n", mydb. dbconnnum );
Printf ("dbstat: % d \ n", mydb. dbstat );
Printf ("CPU: % d \ n", getcpuinf ());
Desicecom ();
Return
1;
}
5. Compile the file to generate executable programs:
Run the following command:
$ C ++-lmyapp-O demo. cpp
Note: The-lmyapp parameter indicates that libmyapp. So is used for compilation together with the dynamic link library. It is best to put libmyapp. So in the/usr/lib directory.
An executable file named demo will be generated in case of any exceptions.