c libevent: undefined reference to `timerclear'

44 Views Asked by At

I used Ubuntu22.04. Recently I am learning libevent

Compile and install with instructions in the installation documentation

mkdir build && cd build
cmake ..
make
sudo make install

Then I tried to write a timer code,

├── CMakeLists.txt
└── main.c
# File: CMakeLists.txt 
# set minimum cmake version
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)

# project name and language
project(libeventstu LANGUAGES C)

set(CMAKE_C_STANDARD 99)
set(CMAKE_C_EXTENSIONS OFF)
set(CMAKE_C_STANDARD_REQUIRED ON)

add_executable(main main.c)
target_link_libraries(main PRIVATE event_core event_extra)
// file: main.c
#include <assert.h>
#include <event2/event.h>
#include <event2/util.h>
#include <stdio.h>


void time_out(evutil_socket_t fd, short event, void *arg) {
    printf("time_out %d", event);
}

int main() {
    struct event_base *base;
    struct event *timeout;
    struct timeval tv;

    base = event_base_new();
    assert(base);

    timeout = event_new(base, -1, EV_PERSIST, time_out, NULL);
    assert(timeout);

    evutil_timerclear(&tv);
    tv.tv_sec = 10;
    event_add(timeout, &tv);

    setbuf(stdout, NULL);
    setbuf(stderr, NULL);

    event_base_dispatch(base);

    return 0;
}

compile

mkdir build && cd build
cmake ..
cmake --build .

Error received:

/usr/bin/ld: CMakeFiles/main.dir/main.c.o: in function `main':
main.c:(.text+0xef): undefined reference to `timerclear'
collect2: error: ld returned 1 exit status

What did I do wrong?

#include <stdio.h>
#include <sys/time.h>

int main() {
    struct timeval t;
    timerclear(&t);
    return 0;
}

There's no problem with using it that way.No need to link the proper libraries. So I don't know how to solve this problem.

new: Under the reminder of a friend, I directly used gcc main.c-levent_core and compiled successfully.

So what's the problem, CMake?

0

There are 0 best solutions below