我在ubuntu上运行gcc编译器我正在使用一个例子来学习如何与libev建立套接字连接我已经安装了libev4和libev dev,但是除了添加struct ev_loop*loop=ev_default_loop(0)之外,一切都很好;它抛出了一个未定义的引用错误任何人都可以告诉我这里有什么问题是我的代码

#include <stdio.h>
#include <netinet/in.h>
#include <ev.h>

#define PORT_NO 3033
#define BUFFER_SIZE 1024

int total_clients = 0;  // Total number of connected clients

void accept_cb(struct ev_loop *loop, struct ev_io *watcher, int revents);
void read_cb(struct ev_loop *loop, struct ev_io *watcher, int revents);

int main() {
    struct ev_loop *loop = ev_default_loop(0); <--error-->
    int sd;
    struct sockaddr_in addr;
    int addr_len = sizeof(addr);
    struct ev_io w_accept;

    // Create server socket
    if ((sd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
        perror("socket error");
        return (-1);
    }

    bzero(&addr, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(PORT_NO);
    addr.sin_addr.s_addr = INADDR_ANY;

    // Bind socket to address
    if (bind(sd, (struct sockaddr*) &addr, sizeof(addr)) != 0) {
        perror("bind error");
    }

    // Start listing on the socket
    if (listen(sd, 2) < 0) {
        perror("listen error");
        return (-1);
    }

    // Initialize and start a watcher to accepts client requests
    ev_io_init(&w_accept, accept_cb, sd, EV_READ);
    ev_io_start(loop, &w_accept); <-- error -->

    // Start infinite loop
    while (1) {
        ev_loop(loop, 0);
    }

    return (0);
}

/* Accept client requests */
void accept_cb(struct ev_loop *loop, struct ev_io *watcher, int revents) {
    struct sockaddr_in client_addr;
    socklen_t client_len = sizeof(client_addr);
    int client_sd;
    struct ev_io *w_client = (struct ev_io*) malloc(sizeof(struct ev_io));

    if (EV_ERROR & revents) {
        perror("got invalid event");
        return;
    }

    // Accept client request
    client_sd = accept(watcher->fd, (struct sockaddr *) &client_addr,
                       &client_len);

    if (client_sd < 0) {
        perror("accept error");
        return;
    }

    total_clients++; // Increment total_clients count
    printf("Successfully connected with client.\n");
    printf("%d client(s) connected.\n", total_clients);

    // Initialize and start watcher to read client requests
    ev_io_init(w_client, read_cb, client_sd, EV_READ);
    ev_io_start(loop, w_client); <--error-->
}

/* Read client message */
void read_cb(struct ev_loop *loop, struct ev_io *watcher, int revents) {
    char buffer[BUFFER_SIZE];
    ssize_t read;

    if (EV_ERROR & revents) {
        perror("got invalid event");
        return;
    }

    // Receive message from client socket
    read = recv(watcher->fd, buffer, BUFFER_SIZE, 0);

    if (read < 0) {
        perror("read error");
        return;
    }

    if (read == 0) {
        // Stop and free watchet if client socket is closing
        ev_io_stop(loop, watcher); <--error-->
        free(watcher);
        perror("peer might closing");
        total_clients--; // Decrement total_clients count
        printf("%d client(s) connected.\n", total_clients);
        return;
    } else {
        printf("message:%s\n", buffer);
    }

    // Send message bach to the client
    send(watcher->fd, buffer, read, 0);
    bzero(buffer, read);
}

我有多个错误,我已经标记谁可以告诉我什么是错的,我做了很多搜索,但没有找到任何东西。

最佳答案

代码看起来没什么问题,但我怀疑你不喜欢利伯夫将-lev添加到编译器行:

gcc -o ev_test ev_test.c -lev

关于c - c libev对“ev_default_loop”的 undefined reference ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13844309/

10-11 06:45