c++ boost cmake boost-log

c++ - Boost.Log con CMake ocasionando un error de referencia indefinido



boost-log (2)

Intento usar la nueva biblioteca Boost.Log en un proyecto en el que estoy trabajando. El proyecto está construido con CMake. Recibo errores de enlace que dicen que el vinculador ha encontrado referencias indefinidas a 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()''

Tengo una prueba de hello world simple que falla con estos errores. Si estoy enlazando con las bibliotecas Boost.Log, ¿qué podría generar un error de referencia indefinido?

main.cpp:

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

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})

Edición: salida detallada de cmake y make

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

hacer:

/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()''


Es Boost_INCLUDE_DIRS no Boost_INCLUDE_DIR.

Puede intentar habilitar Boost_USE_STATIC_LIBS


Parece que se reduce a vincular a la versión compartida de Boost.Log.

Hay un poco de detalle sobre el tema en los documentos para Boost.Log Su mensaje de error menciona el espacio de nombres boost::log::v2s_mt_posix y de los documentos, esto implica que el enlazador espera vincular a una versión estática de Boost.Log .

Si desea vincular a la versión compartida, parece que necesita definir BOOST_LOG_DYN_LINK o BOOST_ALL_DYN_LINK , es decir, en su CMakeLists.txt agregue:

ADD_DEFINITIONS(-DBOOST_LOG_DYN_LINK)

Si desea vincular a la versión estática de Boost.Log, en su lugar debe agregar una variable CMake antes de llamar a FIND_PACKAGE(Boost ...) :

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

Para conocer otras variables que afectan la forma en que CMake encuentra Boost, consulte los documentos de FindBoost .