Similar to the dynamic link library in the Windows system, there is also a corresponding shared library in Linux to support the code multiplexing. Windows is * .dll, and Linux is * .so, I will tell you how to write dynamic libraries under Linux and how to use it.
Steps to write a dynamic link library under Linux:
1. Writing the header file and source file of the library.
2. Compile all the source files involved as the target file as follows:
G / gcc -g -c-fpic -o library1.o library1.cpp
G / gcc -g -c-fpic -o library2.o library2.cpp ...
......
(Note: -FPIC refers to the code-independent code, which can be connected and loaded at any address, and -c refers to compiles only the original program)
3. Link all target files to dynamic libraries:
G / gcc -g -shared -wl, -soname, lib ***. so -o lib ***. SO.1.0.0 Library1.o library2.o .... -lc
(Note: -lc option, means using C language library, usually use)
4. Create a library list link
Ln -s lib ***. SO.1.0.0 lib ***. so
Now you can quote the library. Below I give a simple example, tell you how dynamic and static use dynamic libraries:
If your application source code called Testlib.cpp
Compile with / as follows:
G -g -o testlib testlib.cpp-ldl
(Note: -ldl option, indicating that the generated object module needs to use shared libraries)
This example tells you how to dynamically call. SO library
Testlib.cpp
#include
#include
#include ...
int main ()
{
Void * Handle = NULL;
// define a Pointer Which Will Point to the function in the lib you want to use.
YourFuntionType (* pfunc) (YourFunctionPerameterList ........);
// Open the lib you want to use.
Handle = DLOPEN ("/../../../ Yourlib.so", RTLD_LAZY);
IF (handle == null)
{
COUT << "failed loading library!" << endl;
Return -1;
}
DLERROR ();
// Try to load the function in lib
PFUNC = (YourFunctionType (*)) DLSYM (Handle, "YourFuNetionName);
IF (DLERROR ()! = null)
{
COUT << "Loading function in lib error!" << endl;
Return -1;
}
// Now you can use the funtion like this
(* pfunc) (YourFunctionPerameterList);
Return 0;
}
(Note: DLOPEN ()
The first parameter: Specifies the name of the shared library, will look for the specified shared library in the following location.
- Environment Variable LD_Library_path lists all directories of the separated interval.
- File /etc/ld.so.cache lists found in LDConfig Maintenance.
- Directory USR / LIB.
- Directory / LIB.
-Current directory. (This is this situation)
Second parameters: Specify how to open the shared library.
-Rtld_now: Load all functions in the shared library to memory
-Rtld_lazy: The load operation of the function in the shared library is pushed until a function is loaded until DLSYM () is called.
DLSYM ()
When you call DLSYM, use DLOPEN () The PHANDLE and the function name of the shared library return to the function name as a parameter, and return to the entry address to load the function.
DLError ()
This function is used to check the errors that appear in the associated function of the shared library.
)
Some questions that take care of it:
1. When you want to write a dynamic library with C , remember that don't forget to add the following in the header file, otherwise the generated library will have problems !!!!!!!
#ifdef __cplusplus extern "c" {
#ENDIF
....
....
#ifdef __cplusplus
}
#ENDIF
2. When your library includes something related to Omniorb3, be sure to add -D__x86__ -d__ osversion = 4 in Makefile
/ This example tells you how to static call. SO library
First you have to make sure your application can find your .so library, this can have several ways to implement.
method one:
1. You can put yourLib.so.1.0.0 and YourLib.so in / usr / lib, then execute the command: ldconfig, so you can call the function in your library directly in your application, of course You have to contain the header file of the library to your application.
2. Compile your app
G / gcc -g -o yourapp yourapp.cpp-lyourn
Method Two:
1. You can also use the way to set the environment variable in the system. In the root directory:
vi .bash_profile
Then add ld_library = / .. / YourDirinCludingyourlib
Then I canceled once, the environment variable came into effect so you can directly call the functions in the library in your application, and you have to have a header.
2. Compile your app
G / gcc -g -o yourapp yourapp.cpp-lyourn
Method 3:
You can use it directly to tell the system when you compile the link, where is your library? G / gcc -g -o yourapp yourapp.cpp -l / YourDirinCludingYOrlib -lyourlib
/
If there is a function in your library: int EAT (.....)
So call it as follows
Yourrapp.cpp
#include "YourLib.h"
int main ()
{
Eat ();
Return 0;
}
Is it very easy? It seems that it is a good habit of writing C header files when there is no "attention 1" in static calls.