简体   繁体   English

将构建的 libevent 作为 static 库链接时出现 Linker 错误,但在作为共享库链接时有效

[英]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).我使用 GCC 编译器(在 Linux 上)从源代码构建了 libevent 库作为 static 库。 Cmake options: Cmake 选项:

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: 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.我还尝试将 libevent 构建为共享库(.so output 文件)并且链接良好。 In CMakeLists.txt I just changed ${CMAKE_SOURCE_DIR}/libevent_core.a to ${CMAKE_SOURCE_DIR}/libevent_core.so .在 CMakeLists.txt 中,我刚刚将${CMAKE_SOURCE_DIR}/libevent_core.a更改为${CMAKE_SOURCE_DIR}/libevent_core.so
And my program compiled ok.我的程序编译正常。 Why static library doesn't links?为什么static库没有链接? 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.我将target_link_libraries用于可执行文件,但在链接到可执行文件的 static 库中使用了 libevent。 Now I link libevent directly to static library which is using it.现在我将 libevent 直接链接到正在使用它的 static 库。 And it works.它有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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