undefined reference to `event_new' when compiling with libevent

kaitian521

I am using libevent now:

#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <stdio.h>
#include <event2/event.h>

struct event_base *base;
int PORT = 9999;
int BACKLOG = 5;

int create_bind_listen()
{
    struct sockaddr_in my_addr;
    int yes = 1;
    int sock = socket(AF_INET, SOCK_STREAM, 0);
    setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
    memset(&my_addr, 0, sizeof(struct sockaddr_in));
    my_addr.sin_family = AF_INET;
    my_addr.sin_port = htons(PORT);
    my_addr.sin_addr.s_addr = INADDR_ANY;
    bind(sock, (struct sockaddr*)&my_addr, sizeof(struct sockaddr));
    listen(sock, BACKLOG);
    return sock;
}

void cb_func(evutil_socket_t fd, short what, void *arg)
{
    printf("%d\n", fd);
}

int main()
{
    int sock = create_bind_listen();
    struct event *ev;
    base = event_base_new();
    /*   */
    ev = event_new(base, sock,
                   (short)EV_READ|EV_PERSIST,
                   cb_func, NULL);
    event_add(ev, NULL);
    event_base_dispatch(base);
    return 0;
}

When I compile with that:

gcc -Wall main.c -levent -levent_core

Error happened:

/tmp/cc9x1mMj.o: In function `main':
main.c:(.text+0x11b): undefined reference to `event_new'
collect2: ld returned 1 exit status

How could I fix this?

Thank you !

mtripp100

You need to specify the directory where your .a. files are located with the -L option:

gcc -Wall main.c -L. -levent -levent_core

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

undefined reference to `event_new' when compiling with libevent

From Dev

Undefined Reference when Compiling to Executable

From Dev

'undefined reference to' when compiling with g++

From Dev

Error "undefined reference to `sin'" when compiling (with -lm)

From Dev

Undefined reference to 'main' when compiling a module

From Dev

Undefined reference when compiling C code

From Dev

Undefined reference to main when compiling c++

From Dev

C compiling - "undefined reference to"?

From Dev

In C programming, what is `undefined reference`error, when compiling?

From Dev

GCJ throws error: "Undefined reference to main" when compiling

From Dev

Undefined reference when linking with static library, but successful link when compiling with src

From Dev

Undefined reference to `__cxa_thread_atexit@@CXXABI` when compiling with `libc++` on linux

From Dev

undefined reference to `std::errc::operation_canceled` when compiling websocketpp example code with mingw

From Dev

gui/libgui.a(x.o): undefined reference to symbol 'XSetForeground' when compiling Bosh

From Dev

Undefined reference to pico_dhcp_server_initiate when compiling PicoTCP for ARM mbed

From Dev

Getting ‘undefined reference to’ when compiling simple HDF5 example in Eclipse

From Dev

"undefined reference to `gtk_label_set_xalign'" when compiling a Vala application

From Dev

Undefined reference to process(std::__cxx11::basic_string ... ) when compiling affdex linux sample applications

From Dev

gui/libgui.a(x.o): undefined reference to symbol 'XSetForeground' when compiling Bosh

From Dev

undefined reference to `png_set_longjmp_fn' when compiling PCL source file

From Dev

Clang-3.8 undefined reference to `main' when compiling object file from llvm bytecode with global variables

From Dev

How to solve undefined reference cv:.... when compiling openCV application in Linux/Ubuntu?

From Dev

undefined reference to 'WinMain' with SDL compiling in a native enviroment

From Dev

error: "undefined reference to" while compiling c++

From Dev

gcc compiling C++ code: undefined reference to `operator new[](unsigned long long)'

From Dev

"Undefined reference" error when using assembly to embed data, using mingw-w64 compiling for windows (COFF instead of ELF)

From Dev

No undefined references when compiling shared library

From Dev

AngularJS $scope Undefined When Compiling Controller In Directive

From Dev

Undefined references when compiling project with libvpx

Related Related

  1. 1

    undefined reference to `event_new' when compiling with libevent

  2. 2

    Undefined Reference when Compiling to Executable

  3. 3

    'undefined reference to' when compiling with g++

  4. 4

    Error "undefined reference to `sin'" when compiling (with -lm)

  5. 5

    Undefined reference to 'main' when compiling a module

  6. 6

    Undefined reference when compiling C code

  7. 7

    Undefined reference to main when compiling c++

  8. 8

    C compiling - "undefined reference to"?

  9. 9

    In C programming, what is `undefined reference`error, when compiling?

  10. 10

    GCJ throws error: "Undefined reference to main" when compiling

  11. 11

    Undefined reference when linking with static library, but successful link when compiling with src

  12. 12

    Undefined reference to `__cxa_thread_atexit@@CXXABI` when compiling with `libc++` on linux

  13. 13

    undefined reference to `std::errc::operation_canceled` when compiling websocketpp example code with mingw

  14. 14

    gui/libgui.a(x.o): undefined reference to symbol 'XSetForeground' when compiling Bosh

  15. 15

    Undefined reference to pico_dhcp_server_initiate when compiling PicoTCP for ARM mbed

  16. 16

    Getting ‘undefined reference to’ when compiling simple HDF5 example in Eclipse

  17. 17

    "undefined reference to `gtk_label_set_xalign'" when compiling a Vala application

  18. 18

    Undefined reference to process(std::__cxx11::basic_string ... ) when compiling affdex linux sample applications

  19. 19

    gui/libgui.a(x.o): undefined reference to symbol 'XSetForeground' when compiling Bosh

  20. 20

    undefined reference to `png_set_longjmp_fn' when compiling PCL source file

  21. 21

    Clang-3.8 undefined reference to `main' when compiling object file from llvm bytecode with global variables

  22. 22

    How to solve undefined reference cv:.... when compiling openCV application in Linux/Ubuntu?

  23. 23

    undefined reference to 'WinMain' with SDL compiling in a native enviroment

  24. 24

    error: "undefined reference to" while compiling c++

  25. 25

    gcc compiling C++ code: undefined reference to `operator new[](unsigned long long)'

  26. 26

    "Undefined reference" error when using assembly to embed data, using mingw-w64 compiling for windows (COFF instead of ELF)

  27. 27

    No undefined references when compiling shared library

  28. 28

    AngularJS $scope Undefined When Compiling Controller In Directive

  29. 29

    Undefined references when compiling project with libvpx

HotTag

Archive