This is a frequently encountered problem, in the software development process, some functions need to be packaged into a separate module, so maintenance upgrade is also very convenient. Now we're going to encapsulate the open source log library log4cxx into so dynamic load library files.
In the previous article "Log4cxx Log Library Redhat Installation", we have Log4cxx installed under the home/mac/log4cxx/log4cxx, then we continue to proceed under this path.
Let's start with the tree and/home/mac/log4cxx this catalogue.
Log4cxx---Apr---apr-util---log4cxx
Then create a new four catalogue.
mkdir src Lib include bin
Tree again/home/mac/log4cxx this directory
Log4cxx---Apr---apr-util---log4cxx---src---lib---include---bin
Create a new file under/home/mac/log4cxx Log.h Log.cpp, the contents of these two files are written by yourself to call the Log4cxx Library program, embodies your own encapsulation (Log4cxx library to re-encapsulate the other program for you to use, one usage is to define a class log, it provides its own methods, and each method specific implementation is and LOG4CXX About
There may be doubt as to why the/home/mac/log4cxx will be created under this directory Log.h Log.cpp these two files, not in the SRC directory. This is because Log.h Log.cpp will not be provided directly to the user, strictly speaking Log.cpp this file will not be provided to the user, Log.h will be put into the include directory later.
Now that the preparation for creating the dynamic library liblog.so is done, the next step is the execution of the GCC command.
g++-fpic-shared Log.h log.cpp-o liblog.so \-i/home/mac/log4cxx/log4cxx/include \log4cxx The header file path of the related library, which is actually the individual functions, the declaration of the variable-l /home/mac/log4cxx/log4cxx/lib \log4cxx Related Library's dynamic library path, inside is actually each function, the variable definition-llog4cxx concrete dynamic Library
After execution, the liblog.so file is generated under the current directory/home/mac/log4cxx
Move it to the Lib directory, but also to the/home/mac/log4cxx/Apr apr-util Log4cxx inside Lib and so related files are copied to/home/mac/log4cxx/lib (this step is critical)
Then move the Log.h file to the Include directory, and also copy the/home/mac/log4cxx/log4cxx/include/log4cxx directory (including this directory name) to/home/mac/log4cxx/ Include directory (This step is critical)
At this point, enter the SRC directory, create a new file main.cpp, write the content you want to invoke the dynamic library liblog.so, remember to include the Log.h file, both need
#include "Log.h"
This sentence
Okay, let's build a program that loads the dynamic library liblog.so.
g++ Main.cpp-o. /bin/test \-i/home/mac/log4cxx/include-l/home/mac/log4cxx/lib-llog.so
The test executable is generated in the bin directory when the execution is complete
If the./test after the prompt cannot find the dynamic library, then is/home/mac/log4cxx/lib this directory is not located in the System dynamic Library search path, the specific solution is many, in this do not write, search a lot of.
It may be questioned that dynamic library loading is not used to function Dlopen? In fact, I started out by using the Dlopen function in the program, but the leader wanted me to use this method, I also opened the horizon. So the title of this article is Linux under the Log4cxx package into so dynamic library file (a), there is a two, the next I thank you how to call a dynamic library with function Dlopen
Thank you for watching