stdcerr
stdcerr

Reputation: 15608

Troubles getting boost::logger to compile

I'm struggling with getting some boost::logging demo application running. I've got the following: logger.cpp:

#include <iostream>
#include <boost/fusion/iterator/equal_to.hpp>
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/utility/setup/file.hpp>

namespace logging = boost::log;
namespace src = boost::log::sources;
namespace sinks = boost::log::sinks;
namespace keywords = boost::log::keywords;
namespace expr = boost::log::expressions;


void init()
{
    logging::add_file_log("sample.log");

    logging::core::get()->set_filter
    (   
        logging::trivial::severity >= logging::trivial::info
    );
}

int main(void) {
    init();

    std::cout <<"Hello World!";

CMakeLists.txt:

cmake_minimum_required(VERSION 2.6)
project(LOGGER)

set(BOOST_INCLUDEDIR "/path/to/env/include")
set(BOOST_ROOT "/path/to/env/include")

find_package(Boost REQUIRED)

message(STATUS Boost_LIBRARIES:)
message (STATUS ${Boost_LIBRARIES})
message(STATUS BOOST_INCLUDEDIR:)
message(STATUS ${BOOST_INCLUDEDIR})

ADD_EXECUTABLE(logger logger.cpp)
target_include_directories(logger PUBLIC ${BOOST_INCLUDEDIR})

target_link_libraries(logger Boost::boost ${Boost_LIBRARIES})

set (CMAKE_CXX_FLAGS "-g -Wall")

and on make I get this:

$ make
Scanning dependencies of target logger
[ 50%] Building CXX object CMakeFiles/logger.dir/logger.cpp.o
[100%] Linking CXX executable logger
/usr/bin/ld: cannot find -lBoost::boost
collect2: error: ld returned 1 exit status
CMakeFiles/logger.dir/build.make:94: recipe for target 'logger' failed
make[2]: *** [logger] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/logger.dir/all' failed
make[1]: *** [CMakeFiles/logger.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

and I don't understand why! What am I missing or doing wrong?

Upvotes: 0

Views: 152

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409196

The first problem was because you didn't include the correct header file. For add_file_log the <boost/log/utility/setup/file.hpp> header file is needed.

For the second problem (which should really be a second question, no one likes a moving target), if you read the FindBoost reference you will see that

Boost::boost                Target for header-only dependencies
                            (Boost include directory)

That's not the libraries you need to link with. Remove it from the target_link_libraries command.

Upvotes: 2

Related Questions