链接Boost-对`boost :: serialization :: singleton_module :: get_lock()的未定义引用

时间:2018-09-06 17:04:35

标签: c++ boost cmake compilation dynamic-linking

我有自己的库mylib,该库使用Boost序列化库并进行动态链接。现在,我想将我的库链接到另一个代码-命令如下:

/usr/bin/g++-8   -Wall -g  -rdynamic CMakeFiles/net_test_ode_1.dir/net_test_ode_1.cpp.o  -o ../bin/examples/net_test_ode_1 -Wl,-rpath,/home/martin/MyLib/build/lib:/home/martin/MyLib/external_dependencies/boost/stage/lib ../lib/libmylib.so ../../external_dependencies/boost/stage/lib/libboost_system.so ../../external_dependencies/boost/stage/lib/libboost_serialization.so ../../external_dependencies/boost/stage/lib/libboost_random.so 
../lib/libmylib.so: undefined reference to `boost::serialization::singleton_module::get_lock()'
collect2: error: ld returned 1 exit status

如您所见,我不仅链接libmylib.so,而且链接libboost_serialization.so,因此不应缺少任何依赖项。

仍然,我得到了这个未定义的参考错误。更加令人困惑,因为我已经将其链接到我的库,因此,如果我理解正确的话,就不需要在这里使用它。

你知道吗,我在做什么错了?


CMakeLists.txt-我的图书馆

if ("${BUILD_LIB}" STREQUAL "yes")
    add_library(mylib SHARED
        source codes...       
    )

    set_target_properties(
        mylib

        PROPERTIES
            INCLUDE_DIRECTORIES "${ROOT_DIR}/include;${SRC_DIR};${Boost_INCLUDE_DIRS}"
            LINK_LIBRARIES "${BOOST_LIBRARIES}"
            ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/lib"
            LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/lib"
            RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin"
    )

CMakeLists.txt-我的可执行文件

add_executable(net_test_1 net_test_1.cpp)

set_target_properties(
    net_test_1

    PROPERTIES
        INCLUDE_DIRECTORIES "${ROOT_DIR}/include"
        LINK_LIBRARIES "mylib;${BOOST_LIBRARIES}"
        ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/lib/"
        LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/lib"
        RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin/examples"
)

编辑:

根据compor的评论,我添加了

target_link_libraries(mylib PUBLIC ${BOOST_LIBRARIES})

带我的库的CMakeLists和

target_link_libraries(net_test_1 PUBLIC mylib)

带有我的可执行文件的CMakeLists。

我还从LINK_LIBRARIES中删除了带有set_target_properties()的行,但不幸的是,它没有任何改变,问题仍然存在。


编辑2:

我已根据ChrisD的评论尝试使用-rpath-link,但结果仍然相同

/usr/bin/g++   -Wall -g  -rdynamic CMakeFiles/net_test_ode_1.dir/net_test_ode_1.cpp.o  -o ../bin/examples/net_test_ode_1 -Wl,-rpath-link=/home/martin/MyLibRepo/external_dependencies/boost/stage/lib ../lib/libmylib.so ../../external_dependencies/boost/stage/lib/libboost_system.so ../../external_dependencies/boost/stage/lib/libboost_serialization.so ../../external_dependencies/boost/stage/lib/libboost_random.so
../lib/libmylib.so: undefined reference to `boost::serialization::singleton_module::get_lock()'

我发现,当我将-lboost_serialization添加到编译命令时,它可以正常工作而不会出现错误。那怎么可能?我认为,在命令中添加../../external_dependencies/boost/stage/lib/libboost_serialization.so就足够了,因为它指向正确的库。

1 个答案:

答案 0 :(得分:0)

因此,我想到了一个使用序列化库的最小Boost项目。

项目结构:

.
├── app
│   ├── app.cpp
│   └── CMakeLists.txt
├── CMakeLists.txt
└── mylib
    ├── CMakeLists.txt
    ├── mylib.cpp
    └── mylib.hpp

app / CMakeLists.txt:

add_executable(app app.cpp)
target_include_directories(app PUBLIC mylib)
target_link_libraries(app PUBLIC mylib)

mylib / CMakeLists.txt:

add_library(mylib mylib.cpp)

target_include_directories(mylib PUBLIC 
  ${CMAKE_CURRENT_SOURCE_DIR}
  ${Boost_INCLUDE_DIRS})

target_link_libraries(mylib PUBLIC ${Boost_LIBRARIES})

主要CMakeLists.txt:

cmake_minimum_required(VERSION 3.11)

project(ser CXX)

find_package(Boost COMPONENTS serialization REQUIRED)

add_subdirectory(app)
add_subdirectory(mylib)

我相信您需要使用find_package设置的变量,因为这些变量是按照与您的系统安装相匹配的方式获得的。您随时可以通过doc并影响您需要的任何方面。