CentOS 7 使用异步网络框架Libevent

约定不等于承诺〃 2024-02-17 09:32 107阅读 0赞

CentOS 7 安装Libevent库

libevent github地址:https://github.com/libevent/libevent

步骤1:首先,你需要下载libevent的源代码。你可以从github或者源代码官方网站下载。并上传至/usr/local/source_code/

5cc63cc7978347949fa1205d4f2d9443.png

步骤2:下载完成后,需要将源代码解压,可以使用以下命令:

  1. [root@localhost source_code]# tar -zxvf libevent-2.1.12-stable.tar.gz

步骤3:解压后,切换到源代码目录:

  1. [root@localhost source_code]# cd libevent-2.1.12-stable

步骤4:生成cJSON动态/静态库,执行如下指令:

  1. [root@localhost libevent-2.1.12-stable]# ./configure
  2. checking for a BSD-compatible install... /usr/bin/install -c
  3. checking whether build environment is sane... yes
  4. checking for a thread-safe mkdir -p... /usr/bin/mkdir -p
  5. checking for gawk... gawk
  6. checking whether make sets $(MAKE)... yes
  7. checking whether make supports nested variables... yes
  8. checking whether make supports nested variables... (cached) yes
  9. checking whether make supports the include directive... yes (GNU
  10. ******
  11. [root@localhost libevent-2.1.12-stable]# make
  12. GEN test/rpcgen-attempted
  13. GEN include/event2/event-config.h
  14. make all-am
  15. make[1]: 进入目录“/usr/local/source_code/libevent-2.1.12-stable
  16. CC sample/dns-example.o
  17. CC buffer.lo
  18. CC bufferevent.lo
  19. CC bufferevent_filter.lo
  20. ******
  21. [root@localhost libevent-2.1.12-stable]# sudo make install
  22. make install-am
  23. make[1]: 进入目录“/usr/local/source_code/libevent-2.1.12-stable
  24. make[2]: 进入目录“/usr/local/source_code/libevent-2.1.12-stable
  25. /usr/bin/mkdir -p '/usr/local/bin'
  26. /usr/bin/install -c event_rpcgen.py '/usr/local/bin'
  27. /usr/bin/mkdir -p '/usr/local/lib'
  28. ****

遇到的问题及解决办法

编译执行./test_libevent,提示如下截图错误信息:

0470ae32256a467d9d9f470af89a6309.png

产生原因:在运行时,程序无法找到libevent-2.1.so.7这个动态库,因为该动态库在默认安装时,存放的路径在/usr/local/lib下,不在系统的默认查找路径内。

解决办法:

  1. [root@localhost libevent_demo]# cat /etc/ld.so.conf
  2. include ld.so.conf.d/*.conf
  3. /usr/local/openssl/lib
  4. /usr/local/openssl/lib
  5. [root@localhost libevent_demo]# echo "/usr/local/lib" >> /etc/ld.so.conf
  6. [root@localhost libevent_demo]# sudo ldconfig

Libevent 之Hello World

在/usr/local/source_code 新增 libevent_demo目录并新增 test_libevent.cpp文件,文件内容如下:

  1. #include <event2/event.h>
  2. #include <iostream>
  3. #include <string>
  4. void timer_cb(evutil_socket_t fd, short what, void *arg)
  5. {
  6. auto str = static_cast<std::string *>(arg);
  7. std::cout << *str << std::endl;
  8. }
  9. int main()
  10. {
  11. std::string str = "Hello, World!";
  12. auto *base = event_base_new();
  13. struct timeval five_seconds = {1, 0};
  14. auto *ev = event_new(base, -1, EV_TIMEOUT, timer_cb, (void *)&str);
  15. event_add(ev, &five_seconds);
  16. event_base_dispatch(base);
  17. event_free(ev);
  18. event_base_free(base);
  19. return 0;
  20. }

编译源码并执行:

  1. [root@localhost libevent_demo]# g++ test_libevent.cpp -o test_libevent -levent
  2. [root@localhost libevent_demo]# ./test_libevent
  3. Hello, World!

Libevent 参考资料:

libevent GitHub地址:https://github.com/libevent/libevent

libevent 编程指南: https://senlinzhan.github.io/2017/08/12/libevent/

libevent 深入浅出:https://aceld.gitbooks.io/libevent/content/

发表评论

表情:
评论列表 (有 0 条评论,107人围观)

还没有评论,来说两句吧...

相关阅读

    相关 libevent使用--socket异步编程

    这篇文章介绍下libevent在socket异步编程中的应用。在一些对性能要求较高的网络应用程序中,为了防止程序阻塞在socket I/O操作上造成程序性能的下降,需要使用异步