ubuntu下调用libev出现fatal error: ev.h: No such file or directory及undefined reference to `ev_io_stop‘解决方案

1.例子代码来源以下链接:

https://blog.csdn.net/weixin_39510813/article/details/90713030?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase

#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <sys/unistd.h>
#include <ev.h>


void io_action(struct ev_loop *main_loop,ev_io *io_w,int e)
{
    int rst;
    char buf[1024];
    memset(buf,0,sizeof(buf));
    puts("In IO action");
    read(STDIN_FILENO,buf,sizeof(buf));
    buf[1023]='\0';
    printf("String: %s\n",buf);
    ev_io_stop(main_loop,io_w);
}

void timer_action(struct ev_loop *main_loop,ev_timer *time_w,int e)
{
    puts("In Time action");
    ev_timer_stop(main_loop,time_w);
}

void signal_action(struct ev_loop *main_loop,ev_signal *signal_w,int e)
{
    puts("In Signal action");
    ev_signal_stop(main_loop,signal_w);
    ev_break(main_loop,EVBREAK_ALL);
}

int main(int argc,char **argv)
{
    ev_io io_w;
    ev_timer timer_w;
    ev_signal signal_w;
    struct ev_loop *main_loop = ev_default_loop(0);

    ev_init(&io_w,io_action);
    ev_io_set(&io_w,STDIN_FILENO,EV_READ);

    ev_init(&timer_w,timer_action);
    ev_timer_set(&timer_w,2,0);

    ev_init(&signal_w,signal_action);
    ev_signal_set(&signal_w,SIGINT);

    ev_io_start(main_loop,&io_w);
    ev_timer_start(main_loop,&timer_w);
    ev_signal_start(main_loop,&signal_w);

    ev_run(main_loop,0);
    return 0;
}

2.编译出现fatal error: ev.h: No such file or directory错误

解决方法:sudo apt-get install libev-dev

3.重新编译出现如下错误

/tmp/ccFsapga.o: In function `io_action':
ev_libdev.c:(.text+0x9c): undefined reference to `ev_io_stop'
/tmp/ccFsapga.o: In function `timer_action':
ev_libdev.c:(.text+0xe3): undefined reference to `ev_timer_stop'
/tmp/ccFsapga.o: In function `signal_action':
ev_libdev.c:(.text+0x116): undefined reference to `ev_signal_stop'
ev_libdev.c:(.text+0x127): undefined reference to `ev_break'
/tmp/ccFsapga.o: In function `main':
ev_libdev.c:(.text+0x15b): undefined reference to `ev_default_loop'
ev_libdev.c:(.text+0x25e): undefined reference to `ev_io_start'
ev_libdev.c:(.text+0x274): undefined reference to `ev_timer_start'
ev_libdev.c:(.text+0x28a): undefined reference to `ev_signal_start'
ev_libdev.c:(.text+0x29e): undefined reference to `ev_run'

解决方案:使用以下指令编译,需要链接libev库

gcc -o test ev_libdev.c -lev

至此,libev库调用完成

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wellnw

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值