Original link: Libevent source code learning (0): libevent library installation and simple use
catalogue
1. Download and unzip the libevent library
2. Install the libevent library
3. Simply use the libevent library
1. Download and unzip the libevent library
The version of libevent 2.0.21 stable is downloaded here. The wget command is as follows: (the download address can be accessed through http://libevent.org/ Corresponding libevent Version (right click the copy link to get)
wget https://github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.gz
Then unzip it, because the downloaded format is tar.gz format, so the tar command is used for decompression. The decompression command is as follows:
tar -zxvf libevent-2.0.21-stable.tar.gz
The command parameter used here means: - z specifies that the decompression method is gzip- x means decompression (- c means compression)- v represents the file information displaying the decompression process- f specifies the object to unzip.
2. Install the libevent library
The next step is to install the libevent library. The installation of source code is generally divided into three steps: configuration, compilation and installation.
The configuration is to prepare for the subsequent compilation. Generally, there is an executable script file configure in the source file, which can be used directly/ Configure can execute the file. When executing the file, you can also add the parameter -- prefix to specify the path of subsequent installation. If the installation path is not specified here, the library will be installed in the default path later (the executable file is placed in / usr /local by default)/ bin , the library file is placed in / usr/local/lib by default, and the configuration file is placed in / usr/local/etc by default. Other resource files are placed in / usr /local/share)
Here, I first create a new libevent folder under / usr/local / as the installation path, and execute the following statements: (the path here needs to be an absolute path)
./configure --prefix=/usr/local/libevent
After configuration, the Makefile file will be generated, and then compiled. make can be used directly for compilation;
After compilation, execute the make install command to install the source code into the file directory specified above. The last two processes are as follows:
make make install
If there are no errors in the process, you can see that three new folders are generated under / usr/local/libevent /
The inlcude folder contains the header files related to libevent, and the lib folder contains the library files of libevent. At this point, the libevent library is successfully installed.
3. Simply use the libevent library
Now use the libevent library through a simple example:
#include <stdio.h> //Header files required to use the libevent Library #include <event.h> void on_time(int sock,short event,void *arg) { printf( "hello world\n"); struct timeval tv; tv.tv_sec = 1; tv.tv_usec = 0; // After the event is executed, it is deleted by default, so it needs to be added again event_add((struct event*)arg, &tv); } int main() { // Initialization event event_init(); // Set timer callback function struct event ev_time; evtimer_set(&ev_time, on_time, &ev_time); //Run func function once in 1s struct timeval tv; tv.tv_sec = 1; tv.tv_usec = 0; //Add to event loop event_add(&ev_time, &tv); //The program waits for the ready event and performs event handling event_dispatch(); return 0; }
The above code calls back on every 1 second_ Time function to print "hello world".
Enter the following statement on the command line:
g++ libe.cpp -o a -I /usr/local/libevent/include/ -L /usr/local/libevent/lib -levent
Where the path after the - I parameter specifies the path of the header file, and the path after the - L parameter specifies the path of the library file. The results are as follows:
If you use Makelist, you also need to specify the header file directory and link library directory, as follows:
cmake_minimum_required(VERSION 3.12) project(mytest) set(CMAKE_CXX_STANDARD 14) include_directories(/usr/ local/libevent/ include) #Specifies the header file search path LINK_DIRECTORIES(/usr/ local/libevent/lib) #Specify the library file search path add_executable(mytest main.cpp) target_link_libraries(mytest libevent.a) #Link library