ubuntu下载安装libevent

ubuntu下载安装libevent

  1. libevent 官网上下载最新稳定版本,然后拖到你的linux系统中,解压。或者可是使用wget在线下载。本文下载的是最新版本,其他版本同操作

bash 复制代码
tar xf libevent-2.1.12-stable.tar.gz 
  1. 进入解压后的目录,执行

    bash 复制代码
    ./configure 

    可能会出现报错,内容如下:

    bash 复制代码
    checking for library containing SSL_new... no
    checking for library containing SSL_new... no
    checking openssl/ssl.h usability... no
    checking openssl/ssl.h presence... no
    checking for openssl/ssl.h... no
    configure: error: openssl is a must but can not be found. You should add the directory containing `openssl.pc' to the `PKG_CONFIG_PATH' environment variable, or set `CFLAGS' and `LDFLAGS' directly for openssl, or use `--disable-openssl' to disable support for openssl encryption

    解决办法安装libssl-dev

    bash 复制代码
    sudo apt install libssl-dev
  2. 安装

    bash 复制代码
    make 
    sudo make install
  3. 查看安装成功否

    bash 复制代码
    ls /usr/local/lib/libevent*

    若出现下面的文件,则安装成功。

    bash 复制代码
    ls /usr/local/lib/libevent*
    /usr/local/lib/libevent-2.1.so.7            /usr/local/lib/libevent_extra.la
    /usr/local/lib/libevent-2.1.so.7.0.1        /usr/local/lib/libevent_extra.so
    /usr/local/lib/libevent.a                   /usr/local/lib/libevent_openssl-2.1.so.7
    /usr/local/lib/libevent.la                  /usr/local/lib/libevent_openssl-2.1.so.7.0.1
    /usr/local/lib/libevent.so                  /usr/local/lib/libevent_openssl.a
    /usr/local/lib/libevent_core-2.1.so.7       /usr/local/lib/libevent_openssl.la
    /usr/local/lib/libevent_core-2.1.so.7.0.1   /usr/local/lib/libevent_openssl.so
    /usr/local/lib/libevent_core.a              /usr/local/lib/libevent_pthreads-2.1.so.7
    /usr/local/lib/libevent_core.la             /usr/local/lib/libevent_pthreads-2.1.so.7.0.1
    /usr/local/lib/libevent_core.so             /usr/local/lib/libevent_pthreads.a
    /usr/local/lib/libevent_extra-2.1.so.7      /usr/local/lib/libevent_pthreads.la
    /usr/local/lib/libevent_extra-2.1.so.7.0.1  /usr/local/lib/libevent_pthreads.so
    /usr/local/lib/libevent_extra.a
  4. 编译的时候加-levent选项

  5. 代码示例:

    c 复制代码
    // 信号事件
    void signal_cb(int fd, short event, void *arg)
    {
        printf("SIGINT is comming! if you wang exit process, please enter other signal\n");
    }
    
    // 定时事件
    void time_cb(int fd, short event, void *arg)
    {
        printf("time out\n");
        exit(0);
    }
    
    int main()
    {
        struct event_base *base = event_init(); // base为Libevent的一个实例,对base进行初始化
    
        // 创建信号事件
        struct event *signal_event = evsignal_new(base, SIGINT, signal_cb, NULL);
        event_add(signal_event, NULL); // 将事件注册到Libevent中
    
        // 创建定时事件
        timeval tm{5}; // 定时时间5s
        struct event *time_event = evtimer_new(base, time_cb, NULL);
        event_add(time_event, &tm); // 将事件注册到Libevent中
    
        // 执行事件循环,检测就绪事件
        event_base_dispatch(base);
    
        // 事件循环结束,释放资源
        event_free(signal_event);
        event_free(time_event);
        event_base_free(base);
    
        return 0;
    }

    上述程序,捕捉2号信号,并设置捕捉超时时间5秒。

相关推荐
胖大和尚24 分钟前
完整的 CentOS 6.10 虚拟机安装启动脚本
linux·运维·centos
熙曦Sakura1 小时前
【Linux网络】TCP全连接队列
linux·网络·tcp/ip
脚比路长2 小时前
win11 安装 wsl ubuntu 18.04后换源失败!
linux
菜鸟康2 小时前
Linux——CMake的快速入门上手和保姆级使用介绍、一键执行shell脚本
linux·运维·服务器
卷卷的小趴菜学编程3 小时前
Linux系统之----基础IO
linux·运维·服务器·文件·fopen·文件操作符·位图传递标志位
陈苏同学3 小时前
[已解决] VS Code / Cursor / Trae 的 PowerShell 终端 conda activate 进不去环境的常见问题
linux·windows·conda
simple_whu3 小时前
Ubuntu24.04编译ORB_SLAM的一系列报错解决
ubuntu·slam
我不是秃头sheep3 小时前
Ubuntu 安装 Docker(镜像加速)完整教程
linux·ubuntu·docker
靡樊3 小时前
网络基础概念
linux·服务器·网络·c++·学习
Kusunoki_D4 小时前
速查 Linux 常用指令 II
linux·运维·服务器