Linker errors when linking a built libevent as a static library, but works when linking as a shared library

  c++, cmake, libevent, linker, static-libraries

I’ve built the libevent library from source as static library using GCC compiler(on linux).
Cmake options:

cmake -B build -S . -DEVENT__DISABLE_OPENSSL=ON -DEVENT__LIBRARY_TYPE=STATIC -DEVENT__DISABLE_DEBUG_MODE=ON
cmake –build build –config Release

Linking library in CMakeLists.txt:

target_link_libraries(${PROJECT_NAME}

        ${CMAKE_SOURCE_DIR}/libevent_core.a

        )

But I am still getting errors like this:

/usr/bin/ld: /home/user/libevent_test/main.cpp:65: undefined reference to `event_base_new'
/usr/bin/ld: /home/user/libevent_test/main.cpp:85: undefined reference to `event_new'
/usr/bin/ld: /home/user/libevent_test/main.cpp:89: undefined reference to `event_add'

Also I tried to build libevent as shared library (.so output file) and it linked well.
In CMakeLists.txt I just changed ${CMAKE_SOURCE_DIR}/libevent_core.a to ${CMAKE_SOURCE_DIR}/libevent_core.so.
And my program compiled ok. Why static library doesn’t links?
What I’m doing wrong?
Thanks in advance!

Source: Windows Questions C++

LEAVE A COMMENT