error while loading shared libraries: libevent_pthreads-2.0.so.5: cannot open
shared object file: No such file or directory
今天在执行一代码时,出现了该错误代码如下
#include<event.h>
#include<stdio.h>
#include<unistd.h>
#include<event2/thread.h>
#include<pthread.h> //Linux thread
struct event_base *base = NULL;
void pipe_cb(int fd, short events, void *arg)
{
printf("in the cmd_cb\n");
}
void timeout_cb(int fd, short events, void *arg)
{
printf("in the timeout_cb\n");
}
void* thread_fn(void *arg)
{
char ch;
scanf("%c", &ch); //just for wait
struct event *ev = event_new(base, -1, EV_TIMEOUT | EV_PERSIST,
timeout_cb, NULL);
struct timeval tv = {2, 0};
event_add(ev, &tv);
}
int main(int argc, char ** argv)
{
if( argc >= 2 && argv[1][0] == 'y')
evthread_use_pthreads();
base = event_base_new();
evthread_make_base_notifiable(base);
int pipe_fd[2];
pipe(pipe_fd);
printf("%d %d\n",pipe_fd[0],pipe_fd[1]);
struct event *ev = event_new(base, pipe_fd[0],
EV_READ | EV_PERSIST, pipe_cb, NULL);
event_add(ev, NULL);
pthread_t thread;
pthread_create(&thread, NULL, thread_fn, NULL);
event_base_dispatch(base);
return 0;
}
编译的时候需要链接 -lpthread、-levent和-levent_pthreads,执行之后发现报错
解决方法为:共享库文件安装到了/usr/local/lib(很多开源的共享库都会安装到该目录下)或其它"非/lib或/usr/lib"目录下, 那么在执行ldconfig命令前, 还要把新共享库目录加入到共享库配置文件/etc/ld.so.conf中, 如下:
# cat /etc/ld.so.conf
include ld.so.conf.d/*.conf
# echo "/usr/local/lib" >> /etc/ld.so.conf
# ldconfig
重启之后便能够解决