C Libevent 未定义的引用,我错了吗?

Posted

技术标签:

【中文标题】C Libevent 未定义的引用,我错了吗?【英文标题】:C Libevent undefined reference, am i wrong? 【发布时间】:2016-01-09 15:59:06 【问题描述】:

**我是 C* 的新手

我一直关注Libevent 网站上的Book,并且正在研究Part 01 对异步IO 的简短介绍。这是我的代码。

/*For sockaddr_in*/
#include <netinet/in.h>
/*for sockets functions*/
#include <sys/socket.h>
/*for fcntl*/
#include <fcntl.h>

#include <event2/event.h>

#include <assert.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>

#define MAX_LINE 16384

void do_read(evutil_socket_t fd, short events, void *arg);
void do_write(evutil_socket_t fd, short events, void *arg);

char rot13_char(char c)

    /*dont use isalpha, so locale can tell which are alphabetical*/
    if ((c >= 'a' && c <= 'm') || (c >= 'A' && c<= 'M'))
        return c + 13;
    else if ((c >= 'n' && c<= 'z') || (c >= 'N' && c<= 'Z'))
        return c- 13;
    else
        return c;   


struct fd_state 
    char buffer[MAX_LINE];
    size_t buffer_used;

    size_t n_written;
    size_t write_upto;

    struct event *read_event;
    struct event *write_event;
;

struct fd_state *
int alloc_fd_state(struct event_base *base, evutil_socket_t fd)

    struct fd_state *state = malloc(sizeof(struct fd_state));
    if(!state)
        return NULL;
    state->read_event = event_new(base, fd, EV_READ|EV_PERSIST, do_read, state);
    if (!state->read_event)
        free(state);
        return NULL;
    
    state->write_event = 
        event_new(base, fd, EV_WRITE|EV_PERSIST, do_write, state);

    if(!state->write_event) 
        event_free(state->read_event);
        free(state);
        return NULL;
    

    state->buffer_used = state->n_written = state->write_upto = 0;

    assert(state->write_event);
    return state;



void free_fd_state(struct fd_state *state)

    event_free(state->read_event);
    event_free(state->write_event);
    free(state);


void do_read(evutil_socket_t fd, short events, void *arg)

    struct fd_state *state = arg;
    char buf[1024];
    int i;
    ssize_t result;
    while (1) 
        assert(state->write_event);
        result = recv(fd, buf, sizeof(buf), 0);
        if (result <= 0)
            break;
        for (i=0; i < result; ++i) 
            if (state->buffer_used < sizeof(state->buffer))
                state->buffer[state->buffer_used++] = rot13_char(buf[i]);
            if (buf[i] == '\n') 
                assert(state->write_event);
                event_add(state->write_event, NULL);
                state->write_upto = state->buffer_used;
            
        
    

    if (result == 0) 
        free_fd_state(state);
     else if(result < 0) 
        if (errno = EAGAIN)
            return;
        perror("recv");
        free_fd_state(state);
    



void do_write(evutil_socket_t fd, short events, void *arg) 

    struct fd_state *state = arg;

    while (state->n_written < state->write_upto) 
        ssize_t result = send(fd, state->buffer + state->n_written,
                              state->write_upto - state->n_written, 0);
        if(result < 0) 
            if (errno = EAGAIN)
                return;
            free_fd_state(state);
            return;
        
        assert(result != 0);

        state->n_written += result;
    

    if (state->n_written == state->buffer_used)
        state->n_written = state->write_upto = state->buffer_used = 1;

    event_del(state->write_event);


void do_accept(evutil_socket_t listener, short event, void *arg)

    struct event_base *base = arg;
    struct sockaddr_storage ss;
    socklen_t slen = sizeof(ss);
    int fd = accept(listener, (struct sockaddr*) &ss, &slen);
    if (fd< 0) 
        perror("accept");
     else if (fd > FD_SETSIZE) 
        close(fd);
     else 
        struct fd_state *state;
        evutil_make_socket_nonblocking(fd);
        state = alloc_fd_state(base, fd);
        assert(state);
        assert(state->write_event);
        event_add(state->read_event, NULL);
    


void run(void)

    evutil_socket_t listener;
    struct sockaddr_in sin;
    struct event_base *base;
    struct event *listener_event;

    base = event_base_new();
    if (!base)
        return;

    sin.sin_family = AF_INET;
    sin.sin_addr.s_addr = 0;
    sin.sin_port = htons(40713);

    listener = socket(AF_INET, SOCK_STREAM, 0);
    evutil_make_socket_nonblocking(listener);

#ifndef WIN32
    
        int one = 1;
        setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));      
    
#endif
    if (bind(listener, (struct sockaddr*)&sin, sizeof(sin)) <0) 
        perror("bind");
        return;
    

    if(listen(listener, 16) <0)
        perror("listen");
        return;
    
    listener_event = event_new(base, listener, EV_READ|EV_PERSIST, do_accept, (void*)base);
    event_add(listener_event, NULL);

    event_base_dispatch(base);  


int main(int c, char **v)

    setvbuf(stdout, NULL, _IONBF, 0);

    run();
    return 0;

我在倒数第二个示例中,我已经重写了页面后面的代码,但是已经克隆了它。当我编译时,我收到以下错误:

/tmp/ccB8uLD2.o: In function `alloc_fd_state':
WORLowLevelLibeventRot13.c:(.text+0x9b): undefined reference to `event_new'
WORLowLevelLibeventRot13.c:(.text+0xef): undefined reference to `event_new'
WORLowLevelLibeventRot13.c:(.text+0x120): undefined reference to `event_free'
/tmp/ccB8uLD2.o: In function `free_fd_state':
WORLowLevelLibeventRot13.c:(.text+0x1bc): undefined reference to `event_free'
WORLowLevelLibeventRot13.c:(.text+0x1cf): undefined reference to `event_free'
/tmp/ccB8uLD2.o: In function `do_read':
WORLowLevelLibeventRot13.c:(.text+0x342): undefined reference to `event_add'
/tmp/ccB8uLD2.o: In function `do_write':
WORLowLevelLibeventRot13.c:(.text+0x50b): undefined reference to `event_del'
/tmp/ccB8uLD2.o: In function `do_accept':
WORLowLevelLibeventRot13.c:(.text+0x5bc): undefined reference to `evutil_make_socket_nonblocking'
WORLowLevelLibeventRot13.c:(.text+0x644): undefined reference to `event_add'
/tmp/ccB8uLD2.o: In function `run':
WORLowLevelLibeventRot13.c:(.text+0x677): undefined reference to `event_base_new'
WORLowLevelLibeventRot13.c:(.text+0x6c2): undefined reference to `evutil_make_socket_nonblocking'
WORLowLevelLibeventRot13.c:(.text+0x74f): undefined reference to `event_new'
WORLowLevelLibeventRot13.c:(.text+0x764): undefined reference to `event_add'
WORLowLevelLibeventRot13.c:(.text+0x770): undefined reference to `event_base_dispatch'
collect2: error: ld returned 1 exit status

我在假设,你知道他们对假设的看法......这是来自 alloc_fd_state,由于范围而无法到达 event_new 等?但是,我也看到它没有被赋予诸如 void 之类的类型,或者其他类型。所以,自然好奇,我只是为了测试而玩弄,说我把 int 放在 alloc_fd_state 前面(虽然知道这是不正确的),我收到一个不同的错误,如下:

WORLowLevelLibeventRot13.c:45:1: error: expected identifier or ‘(’ before ‘int’
 int alloc_fd_state(struct event_base *base, evutil_socket_t fd)
 ^
WORLowLevelLibeventRot13.c: In function ‘do_accept’:
WORLowLevelLibeventRot13.c:148:11: warning: implicit declaration of function ‘alloc_fd_state’ [-Wimplicit-function-declaration]
   state = alloc_fd_state(base, fd);
           ^
WORLowLevelLibeventRot13.c:148:9: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
   state = alloc_fd_state(base, fd);

现在,我想我的问题是(是),如下。

在第一个场景错误代码的情况下,这里错过了什么,我收到了未定义的引用。 Event_new 等在我的代码中似乎都很好,为什么我无法联系到它们?我应该在哪里参考,也许这非常明显,因为我对 C 非常熟悉,但我已经盯着这个看了一段时间,我的大脑有点陷入困境。任何关于为什么会发生这种情况以及我如何确保它不会出现在任何未来代码中的解释都将不胜感激。

对于第二种情况,我假设未定义引用的真正问题只是被显示的新的和更严重的错误所掩盖,实际上并没有修复任何东西。

对 C 菜鸟的任何见解表示赞赏。

【问题讨论】:

看这个:***.com/questions/11698478/… 谢谢,没有意识到我必须明确标记库。 【参考方案1】:

在编译器命令行中添加-levent_core

【讨论】:

感谢具体的flag,除了@kfx,现在一切都好了。

以上是关于C Libevent 未定义的引用,我错了吗?的主要内容,如果未能解决你的问题,请参考以下文章

看起来 is_nothrow_constructible_v() 在 MSVC 中被破坏了,我错了吗?

未捕获的引用错误,未定义通知

++x %= 10 在 C++ 中是不是明确定义?

Firefox 4 中的 JQuery 未定义和 $ 未定义错误 [关闭]

在c中声明与定义变量[重复]

C++ 错误:未定义对“main”的引用