Libevent source code learning: libevent library installation and simple use

Posted by warrenk on Tue, 25 Jan 2022 14:07:44 +0100

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:

 
  1. make
  2. 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:

 
  1. #include <stdio.h>
  2. //Header files required to use the libevent Library
  3. #include <event.h>
  4. void on_time(int sock,short event,void *arg)
  5. {
  6. printf( "hello world\n");
  7. struct timeval tv;
  8. tv.tv_sec = 1;
  9. tv.tv_usec = 0;
  10. // After the event is executed, it is deleted by default, so it needs to be added again
  11. event_add((struct event*)arg, &tv);
  12. }
  13. int main()
  14. {
  15. // Initialization event
  16. event_init();
  17. // Set timer callback function
  18. struct event ev_time;
  19. evtimer_set(&ev_time, on_time, &ev_time);
  20. //Run func function once in 1s
  21. struct timeval tv;
  22. tv.tv_sec = 1;
  23. tv.tv_usec = 0;
  24. //Add to event loop
  25. event_add(&ev_time, &tv);
  26. //The program waits for the ready event and performs event handling
  27. event_dispatch();
  28. return 0;
  29. }

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:

 
  1. cmake_minimum_required(VERSION 3.12)
  2. project(mytest)
  3. set(CMAKE_CXX_STANDARD 14)
  4. include_directories(/usr/ local/libevent/ include) #Specifies the header file search path
  5. LINK_DIRECTORIES(/usr/ local/libevent/lib) #Specify the library file search path
  6. add_executable(mytest main.cpp)
  7. target_link_libraries(mytest libevent.a) #Link library

 

Topics: Linux