what can cause the following problem: I tried to use boost::log in a cmake project.
My CMakeList.txt file looks like this:
cmake_minimum_required(VERSION 3.10)
project(boostLogTest)
set(CMAKE_CXX_STANDARD 11)
add_executable(boostLogTest main.cpp)
ADD_DEFINITIONS(-DBOOST_LOG_DYN_LINK) ##### edit after DiCri's answer #####
find_package(Boost REQUIRED COMPONENTS log system)
if (Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
message("Boost VERSION: ${Boost_VERSION}")
message("Boost INCLUDE_DIRS: ${Boost_INCLUDE_DIRS}")
message("Boost Boost_LIBRARY_DIRS: ${Boost_LIBRARY_DIRS}")
message("Boost LIBRARIES: ${Boost_LIBRARIES}")
TARGET_LINK_LIBRARIES(boostLogTest ${Boost_LIBRARIES})
endif ()
and my main.cpp file looks like this:
#include <iostream>
#include <boost/log/trivial.hpp>
int main(int, char*[])
{
BOOST_LOG_TRIVIAL(trace) << "A trace severity message";
BOOST_LOG_TRIVIAL(debug) << "A debug severity message";
BOOST_LOG_TRIVIAL(info) << "An informational severity message";
BOOST_LOG_TRIVIAL(warning) << "A warning severity message";
BOOST_LOG_TRIVIAL(error) << "An error severity message";
BOOST_LOG_TRIVIAL(fatal) << "A fatal severity message";
return 0;
}
My CMake output is the following
-- Boost version: 1.64.0
-- Found the following Boost libraries:
-- log
-- system
-- date_time
-- log_setup
-- filesystem
-- thread
-- regex
-- chrono
-- atomic Boost VERSION: 106400 Boost INCLUDE_DIRS: /usr/local/include Boost Boost_LIBRARY_DIRS: /usr/local/lib Boost
LIBRARIES: /usr/local/lib/libboost_log.so;/usr/local/lib/libboost_system.so;/usr/local/lib/libboost_date_time.so;/usr/local/lib/libboost_log_setup.so;/usr/local/lib/libboost_filesystem.so;/usr/local/lib/libboost_thread.so;/usr/local/lib/libboost_regex.so;/usr/local/lib/libboost_chrono.so;/usr/local/lib/libboost_atomic.so
-- Configuring done
-- Generating done
-- Build files have been written to: /home/.../CLionProjects/boostLogTest/cmake-build-debug
but while linking I get the following error messages:
[ 50%] Linking CXX executable boostLogTest
CMakeFiles/boostLogTest.dir/main.cpp.o: In function `main':
/home/.../CLionProjects/boostLogTest/main.cpp:7: undefined reference to `boost::log::v2s_mt_posix::trivial::logger::get()'
/home/.../CLionProjects/boostLogTest/main.cpp:7: undefined reference to `boost::log::v2s_mt_posix::trivial::logger::get()'
/home/.../CLionProjects/boostLogTest/main.cpp:8: undefined reference to `boost::log::v2s_mt_posix::trivial::logger::get()'
/home/.../CLionProjects/boostLogTest/main.cpp:8: undefined reference to `boost::log::v2s_mt_posix::trivial::logger::get()'
/home/.../CLionProjects/boostLogTest/main.cpp:9: undefined reference to
boost::log::v2s_mt_posix::trivial::logger::get()' CMakeFiles/boostLogTest.dir/main.cpp.o:/home/.../CLionProjects/boostLogTest/main.cpp:9: more undefined references to
boost::log::v2s_mt_posix::trivial::logger::get()' follow ...
Edit after DiCri's answer: Now I get the following error messages:
[ 50%] Linking CXX executable boostLogTest
/usr/bin/ld: CMakeFiles/boostLogTest.dir/main.cpp.o: undefined reference to symbol 'pthread_rwlock_unlock@@GLIBC_2.2.5'
//lib/x86_64-linux-gnu/libpthread.so.0: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
CMakeFiles/boostLogTest.dir/build.make:103: recipe for target 'boostLogTest' failed
make[3]: *** [boostLogTest] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/boostLogTest.dir/all' failed
make[2]: *** [CMakeFiles/boostLogTest.dir/all] Error 2
CMakeFiles/Makefile2:79: recipe for target 'CMakeFiles/boostLogTest.dir/rule' failed
make[1]: *** [CMakeFiles/boostLogTest.dir/rule] Error 2
Makefile:118: recipe for target 'boostLogTest' failed
make: *** [boostLogTest] Error 2
If I add
set(Boost_USE_STATIC_LIBS ON)
the paths to the libraries change to a *.a file, like:
Boost LIBRARIES: /usr/local/lib/libboost_log.a;/usr/local/lib/libboost_system.a;...
All listed libraries (the *.so and the *.a) exist in this folders.
What am I missing?