【发布时间】:2023-03-04 02:32:01
【问题描述】:
我想在 cmake 中从 header.h 生成一个仅标头库,这取决于 libboost_system。
我可以毫无问题地编译库:
find_package(Boost COMPONENTS
system filesystem
REQUIRED)
include_directories(SYSTEM ${Boost_INCLUDE_DIRS})
add_library(mylib header.h)
target_link_libraries(mylib PUBLIC ${Boost_LIBRARIES})
set_target_properties(mylib PROPERTIES LINKER_LANGUAGE CXX)
但是当我链接到其他地方的 mylib 时,找不到带有 ld 错误的 boost 库。
失败是有道理的,但我不知道如何在 CMake 中解决它。 如何“存储”对 mylib 的 boost 依赖?这样我就不用担心在其他外部项目中找到 boost 库了?
编辑:我正在使用 cmake 3.2
更新:mylib 是一个共享库(.so),当我在其他项目中使用它时,链接器无法找到 boost 库:
target_link_libraries(newproject.exe ${external_mylib})
undefined reference to symbol '_ZN5boost6system15system_categoryEv'
/PATH/TO/libboost_system-mt-d.so.1.57.0: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
如果我再次明确链接到 Boost_LIBRARIES,问题就解决了。
target_link_libraries(newproject.exe ${external_mylib} ${Boost_LIBRARIES))
这并不能避免再次找到 boost_libraries,也许解决方案是将 boost_libraries 放在环境变量 LD_LIBRARY_PATH 中? 那就太过分了……
【问题讨论】: