linux编程libevent安装

短命女 2022-12-02 04:12 467阅读 0赞

linux编程libevent安装

下载libevent-2.1.8-stable.tar.gz,解压

  1. tar -zxf libevent-2.1.8-stable.tar

或者

  1. mkdir 321
  2. tar -zxf libevent-2.1.8-stable.tar -C ./321

进入目录

  1. cd libevent-2.1.8-stable
  2. ls -la

这时,我们可以看到有一些是可以执行的。
执行

  1. ./configure

生成makefile,执行make命令

  1. make

测试

进入sample目录

  1. cd sample

看一下hello-world.c文件,看到端口9995

  1. #include <string.h>
  2. #include <errno.h>
  3. #include <stdio.h>
  4. #include <signal.h>
  5. #ifndef _WIN32
  6. #include <netinet/in.h>
  7. # ifdef _XOPEN_SOURCE_EXTENDED
  8. # include <arpa/inet.h>
  9. # endif
  10. #include <sys/socket.h>
  11. #endif
  12. #include <event2/bufferevent.h>
  13. #include <event2/buffer.h>
  14. #include <event2/listener.h>
  15. #include <event2/util.h>
  16. #include <event2/event.h>
  17. static const char MESSAGE[] = "Hello, World!\n";
  18. static const int PORT = 9995;
  19. static void listener_cb(struct evconnlistener *, evutil_socket_t,
  20. struct sockaddr *, int socklen, void *);
  21. static void conn_writecb(struct bufferevent *, void *);
  22. static void conn_eventcb(struct bufferevent *, short, void *);
  23. static void signal_cb(evutil_socket_t, short, void *);
  24. int
  25. main(int argc, char **argv)
  26. {
  27. struct event_base *base;
  28. struct evconnlistener *listener;
  29. struct event *signal_event;
  30. struct sockaddr_in sin;

执行

启动终端1,执行例子自带的hello-world程序。

  1. $ ./hello-world

启动终端2,执行命令nc 127.1 9995,返回Hello, World!后立即断开,测试成功。

  1. $ nc 127.1 9995
  2. Hello, World!

编译hello-world

这里生成hello-world还是会报错,所以要安装一下。

  1. $ gcc hello-world.c -o hello -levent

安装

  1. $ sudo make install
  2. ...
  3. ...
  4. Libraries have been installed in:
  5. /usr/local/lib
  6. If you ever happen to want to link against installed libraries
  7. in a given directory, LIBDIR, you must either use libtool, and
  8. specify the full pathname of the library, or use the '-LLIBDIR'
  9. flag during linking and do at least one of the following:
  10. - add LIBDIR to the 'LD_LIBRARY_PATH' environment variable
  11. during execution
  12. - add LIBDIR to the 'LD_RUN_PATH' environment variable
  13. during linking
  14. - use the '-Wl,-rpath -Wl,LIBDIR' linker flag
  15. - have your system administrator add LIBDIR to '/etc/ld.so.conf'
  16. See any operating system documentation about shared libraries for
  17. more information, such as the ld(1) and ld.so(8) manual pages.
  18. ...
  19. ...
  20. $ gcc hello-world.c -o hello -levent

可以看到libevent被安装在/usr/local/lib

动态库问题

  1. $ cd /etc/ld.so.conf.d
  2. $ ls
  3. i386-linux-gnu.conf libc.conf x86_64-linux-gnu.conf

解决方法
修改etc/ld.so.conf文件。
a. 动态库路径添加到该文件中 - 绝对
b. sudo ldconfig
查看

  1. $ cat libc.conf
  2. # libc default configuration
  3. /usr/local/lib

发表评论

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

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

相关阅读