简体   繁体   English

带有 CMake 的 Boost.Log 导致未定义的引用错误

[英]Boost.Log with CMake causing undefined reference error

I am trying to use the new Boost.Log library in a project I am working on.我正在尝试在我正在处理的项目中使用新的 Boost.Log 库。 The project is built with CMake. I am receiving link errors claiming that the linker has come across undefined references to Boost.Log该项目是用 CMake 构建的。我收到链接错误,声称 linker 遇到了对 Boost.Log 的未定义引用

Linking CXX executable main
CMakeFiles/main.dir/main.cpp.o: In function `main':
main.cpp:(.text+0x30): undefined reference to `boost::log::v2s_mt_posix::trivial::logger::get()'

I have a simple hello world test that fails with these errors.我有一个简单的 hello world 测试失败并出现这些错误。 If I am linking against the Boost.Log libraries what would cause it to generate an undefined reference error?如果我链接到 Boost.Log 库,什么会导致它生成未定义的引用错误?

main.cpp:主.cpp:

#include <boost/log/trivial.hpp>
int main(int argc, char* const argv[]) {
  BOOST_LOG_TRIVIAL(info) << "Hello World";
}

CMakeLists.txt: CMakeLists.txt:

CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
FIND_PACKAGE(Boost 1.54 COMPONENTS log REQUIRED)
FIND_PACKAGE(Threads)
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIR})
ADD_EXECUTABLE(main main.cpp)
TARGET_LINK_LIBRARIES(main ${Boost_LOG_LIBRARY} ${CMAKE_THREAD_LIBS_INIT})

Edit: verbose output from cmake and make编辑:详细 output 来自 cmake 并制作

cmake: cmake:

-- [ /usr/share/cmake-2.8/Modules/FindBoost.cmake:476 ] _boost_TEST_VERSIONS = 1.56.0;1.56;1.55.0;1.55;1.54.0;1.54
-- [ /usr/share/cmake-2.8/Modules/FindBoost.cmake:478 ] Boost_USE_MULTITHREADED = TRUE
-- [ /usr/share/cmake-2.8/Modules/FindBoost.cmake:480 ] Boost_USE_STATIC_LIBS = 
-- [ /usr/share/cmake-2.8/Modules/FindBoost.cmake:482 ] Boost_USE_STATIC_RUNTIME = 
-- [ /usr/share/cmake-2.8/Modules/FindBoost.cmake:484 ] Boost_ADDITIONAL_VERSIONS = 
-- [ /usr/share/cmake-2.8/Modules/FindBoost.cmake:486 ] Boost_NO_SYSTEM_PATHS = 
-- [ /usr/share/cmake-2.8/Modules/FindBoost.cmake:538 ] Declared as CMake or Environmental Variables:
-- [ /usr/share/cmake-2.8/Modules/FindBoost.cmake:540 ]   BOOST_ROOT = 
-- [ /usr/share/cmake-2.8/Modules/FindBoost.cmake:542 ]   BOOST_INCLUDEDIR = 
-- [ /usr/share/cmake-2.8/Modules/FindBoost.cmake:544 ]   BOOST_LIBRARYDIR = 
-- [ /usr/share/cmake-2.8/Modules/FindBoost.cmake:546 ] _boost_TEST_VERSIONS = 1.56.0;1.56;1.55.0;1.55;1.54.0;1.54
-- [ /usr/share/cmake-2.8/Modules/FindBoost.cmake:639 ] location of version.hpp: /usr/include/boost/version.hpp
-- [ /usr/share/cmake-2.8/Modules/FindBoost.cmake:663 ] version.hpp reveals boost 1.54.0
-- [ /usr/share/cmake-2.8/Modules/FindBoost.cmake:739 ] guessed _boost_COMPILER = -gcc48
-- [ /usr/share/cmake-2.8/Modules/FindBoost.cmake:749 ] _boost_MULTITHREADED = -mt
-- [ /usr/share/cmake-2.8/Modules/FindBoost.cmake:792 ] _boost_RELEASE_ABI_TAG = -
-- [ /usr/share/cmake-2.8/Modules/FindBoost.cmake:794 ] _boost_DEBUG_ABI_TAG = -d
-- [ /usr/share/cmake-2.8/Modules/FindBoost.cmake:842 ] _boost_LIBRARY_SEARCH_DIRS = /usr/lib64;NO_DEFAULT_PATH
-- [ /usr/share/cmake-2.8/Modules/FindBoost.cmake:930 ] Searching for LOG_LIBRARY_RELEASE: boost_log-gcc48-mt-1_54;boost_log-gcc48-mt;boost_log-mt-1_54;boost_log-mt;boost_log
-- [ /usr/share/cmake-2.8/Modules/FindBoost.cmake:966 ] Searching for LOG_LIBRARY_DEBUG: boost_log-gcc48-mt-d-1_54;boost_log-gcc48-mt-d;boost_log-mt-d-1_54;boost_log-mt-d;boost_log-mt;boost_log
-- [ /usr/share/cmake-2.8/Modules/FindBoost.cmake:1017 ] Boost_FOUND = 1
-- Boost version: 1.54.0
-- Found the following Boost libraries:
--   log
-- Configuring done
-- Generating done
-- Build files have been written to: /home/durrw/boost-log-test/build

make:制作:

/usr/bin/cmake -H/home/durrw/boost-log-test -B/home/durrw/boost-log-test/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /home/durrw/boost-log-test/build/CMakeFiles /home/durrw/boost-log-test/build/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory `/home/durrw/boost-log-test/build'
make -f CMakeFiles/main.dir/build.make CMakeFiles/main.dir/depend
make[2]: Entering directory `/home/durrw/boost-log-test/build'
cd /home/durrw/boost-log-test/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/durrw/boost-log-test /home/durrw/boost-log-test /home/durrw/boost-log-test/build /home/durrw/boost-log-test/build /home/durrw/boost-log-test/build/CMakeFiles/main.dir/DependInfo.cmake --color=
make[2]: Leaving directory `/home/durrw/boost-log-test/build'
make -f CMakeFiles/main.dir/build.make CMakeFiles/main.dir/build
make[2]: Entering directory `/home/durrw/boost-log-test/build'
Linking CXX executable main
/usr/bin/cmake -E cmake_link_script CMakeFiles/main.dir/link.txt --verbose=1
/usr/bin/c++       CMakeFiles/main.dir/main.cpp.o  -o main -rdynamic -lboost_log -lpthread 
CMakeFiles/main.dir/main.cpp.o: In function `main':
main.cpp:(.text+0x39): undefined reference to `boost::log::v2s_mt_posix::trivial::logger::get()'

It looks like it boils down to linking to the shared version of Boost.Log. 看起来它归结为链接到Boost.Log的共享版本。

There is a bit of detail on the issue in the docs for Boost.Log Your error message mentions the namespace boost::log::v2s_mt_posix and from the docs, this implies the linker is expecting to link to a static version of Boost.Log. 关于Boost.Log文档中的问题有一些细节您的错误消息提到名称空间boost::log::v2s_mt_posix并且从文档中,这意味着链接器期望链接到Boost.Log的静态版本。 。

If you want to link to the shared version, it seems you need to define BOOST_LOG_DYN_LINK or BOOST_ALL_DYN_LINK , ie in your CMakeLists.txt add: 如果您想要链接到共享版本,您似乎需要定义BOOST_LOG_DYN_LINKBOOST_ALL_DYN_LINK ,即在您的CMakeLists.txt中添加:

ADD_DEFINITIONS(-DBOOST_LOG_DYN_LINK)

If you want to link to the static version of Boost.Log, instead you need to add a CMake variable before calling FIND_PACKAGE(Boost ...) : 如果要链接到Boost.Log的静态版本,则需要调用FIND_PACKAGE(Boost ...) 之前添加CMake变量:

SET(Boost_USE_STATIC_LIBS ON)
FIND_PACKAGE(Boost 1.54 COMPONENTS log REQUIRED)

For further variables which affect how CMake finds Boost, see the docs for FindBoost . 有关影响CMake如何找到Boost的其他变量,请参阅FindBoost的文档。

For CMake 3.15 (and some earlier versions) the following appears to be sufficient to build Boost.Log with CMake without the original linker error:对于 CMake 3.15(和一些更早的版本),以下似乎足以构建带有 CMake 的 Boost.Log 而没有原始的 linker 错误:

cmake_minimum_required(VERSION 3.15)
project(boost_log_tutorial)

SET(Boost_USE_STATIC_LIBS ON)           # link statically
#ADD_DEFINITIONS(-DBOOST_LOG_DYN_LINK)  # or, link dynamically

find_package(Boost 1.69.0 COMPONENTS log REQUIRED)

add_executable(boost_log_tutorial main.cpp)
target_link_libraries(boost_log_tutorial Boost::log_setup Boost::log)

The key things seem to be linking against Boost:log_setup and Boost::log , and specifying either static or dynamic linking with SET(Boost_USE_STATIC_LIBS ON) or ADD_DEFINITIONS(-DBOOST_LOG_DYN_LINK) .关键的事情似乎是针对Boost:log_setupBoost::log进行链接,并指定 static 或使用SET(Boost_USE_STATIC_LIBS ON)ADD_DEFINITIONS(-DBOOST_LOG_DYN_LINK)进行动态链接。

Also, make sure that the build type ( Debug , or Release ) for Conan matches the build type for CMake. For example:此外,请确保 Conan 的构建类型( DebugRelease )与 CMake 的构建类型匹配。例如:

$ conan install -s build_type=Debug ..
$ cmake -DCMAKE_BUILD_TYPE=Debug ..

If they mismatch then the link will fail with the same error.如果它们不匹配,则链接将失败并出现相同的错误。

It is Boost_INCLUDE_DIRS not Boost_INCLUDE_DIR. 它是Boost_INCLUDE_DIRS而不是Boost_INCLUDE_DIR。

You could try enable Boost_USE_STATIC_LIBS 您可以尝试启用Boost_USE_STATIC_LIBS

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

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