简体   繁体   中英

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

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!

Solved. I used target_link_libraries for executable, but libevent was used in static library which is linking to executable. Now I link libevent directly to static library which is using it. And it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM