Home > Back-end >  Trouble building Boost Log example using Bazel
Trouble building Boost Log example using Bazel

Time:12-22

I am using Bazel as my build system and have had no problems with Boost up until I started trying to use Boost Log, I was even able to use some of the compiled libraries without issue however when tried to use Boost.Log or tried to build the example for trivial logging I get a host of undefined reference errors.

I have tried building with the following bazel build commands:

bazel build //app:boost-log-test

bazel build --cxxopt="-pthread" //app:boost-log-test

bazel build --cxxopt="-lpthread" //app:boost-log-test

bazel build --cxxopt="-DBOOST_LOG_DYN_LINK" --cxxopt="-lpthread" //app:boost-log-test

bazel build --cxxopt="-lboost_log" --cxxopt="-lpthread" //app:boost-log-test

bazel build --cxxopt="-DBOOST_LOG_DYN_LINK" --cxxopt="-lboost_log" //app:boost-log-test

bazel build --cxxopt="-DBOOST_LOG_DYN_LINK" --cxxopt="-lboost_log" --cxxopt="-lpthread" //app:boost-log-test

bazel build --cxxopt="-DBOOST_LOG_DYN_LINK" --cxxopt="-lboost_log" --cxxopt="-lpthread" --cxxopt="-std=c 17" //app:boost-log-test

and all resulted in the same errors which I've included at the bottom of this post. My Boost root is located at /opt/boost/boost_1_77_0 and below is my project structure.

./WORKSPACE:

new_local_repository(
    name = "boost",
    path = "/opt/boost/boost_1_77_0",
    build_file = "external/boost.BUILD",
)

./external/boost.BUILD:

load("@rules_cc//cc:defs.bzl", "cc_library")

cc_library(
    name = "boost-headers",
    hdrs = glob(["include/boost/**"]),
    visibility = ["//visibility:public"],
    includes = ['include'],
)

cc_library(
    name = "boost-allbuilt",
    srcs = glob(["lib/*.a"]),
    visibility = ["//visibility:public"],
)

...

cc_library(
    name = "boost-log",
    srcs = ["lib/libboost_log.a"],
    visibility = ["//visibility:public"],
)

cc_library(
    name = "boost-log_setup",
    srcs = ["lib/libboost_log_setup.a"],
    visibility = ["//visibility:public"],
)

...

cc_library(
    name = "boost-wserialization",
    srcs = ["lib/libboost_wserialization.a"],
    visibility = ["//visibility:public"],
)

./app/BUILD:

load("@rules_cc//cc:defs.bzl", "cc_binary")

cc_binary(
    name = "boost-log-test",
    srcs = ["log_test.cpp"],
    deps = [
        "@boost//:boost-headers",
        "@boost//:boost-log_setup",
        "@boost//:boost-log",
    ],
)

./app/log_test.cpp (exact copy of /opt/boost/boost_1_77_0/libs/log/example/trivial/main.cpp):

/*
 *          Copyright Andrey Semashev 2007 - 2015.
 * Distributed under the Boost Software License, Version 1.0.
 *    (See accompanying file LICENSE_1_0.txt or copy at
 *          http://www.boost.org/LICENSE_1_0.txt)
 */
/*!
 * \file   main.cpp
 * \author Andrey Semashev
 * \date   07.11.2009
 *
 * \brief  An example of trivial logging.
 */

// #define BOOST_ALL_DYN_LINK 1
// #define BOOST_LOG_DYN_LINK 1

#include <boost/log/trivial.hpp>
#include <boost/log/core.hpp>
#include <boost/log/expressions.hpp>

int main(int argc, char* argv[])
{
    // Trivial logging: all log records are written into a file
    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";

    // Filtering can also be applied
    using namespace boost::log;

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

    // Now the first two lines will not pass the filter
    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;
}

Build output:

[user@computer cpp]$ bazel build --cxxopt="-DBOOST_LOG_DYN_LINK" --cxxopt="-lboost_log" --cxxopt="-lpthread" --cxxopt="-std=c  17" //app:boost-log-test
INFO: Analyzed target //app:boost-log-test (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
ERROR: /home/blueder/Projects/MarketProphet/cpp/app/BUILD:11:10: Linking app/boost-log-test failed: (Exit 1): gcc failed: error executing command /usr/bin/gcc @bazel-out/k8-fastbuild/bin/app/boost-log-test-2.params

Use --sandbox_debug to see verbose messages from the sandbox
bazel-out/k8-fastbuild/bin/app/_objs/boost-log-test/log_test.pic.o:log_test.cpp:function main: error: undefined reference to 'boost::log::v2_mt_posix::trivial::logger::get()'
bazel-out/k8-fastbuild/bin/app/_objs/boost-log-test/log_test.pic.o:log_test.cpp:function main: error: undefined reference to 'boost::log::v2_mt_posix::trivial::logger::get()'
bazel-out/k8-fastbuild/bin/app/_objs/boost-log-test/log_test.pic.o:log_test.cpp:function main: error: undefined reference to 'boost::log::v2_mt_posix::trivial::logger::get()'
bazel-out/k8-fastbuild/bin/app/_objs/boost-log-test/log_test.pic.o:log_test.cpp:function main: error: undefined reference to 'boost::log::v2_mt_posix::trivial::logger::get()'
bazel-out/k8-fastbuild/bin/app/_objs/boost-log-test/log_test.pic.o:log_test.cpp:function main: error: undefined reference to 'boost::log::v2_mt_posix::core::get()'
bazel-out/k8-fastbuild/bin/app/_objs/boost-log-test/log_test.pic.o:log_test.cpp:function main: error: undefined reference to 'boost::log::v2_mt_posix::core::set_filter(boost::log::v2_mt_posix::filter const&)'
bazel-out/k8-fastbuild/bin/app/_objs/boost-log-test/log_test.pic.o:log_test.cpp:function boost::log::v2_mt_posix::aux::light_rw_mutex::lock_shared(): error: undefined reference to 'pthread_rwlock_rdlock'
bazel-out/k8-fastbuild/bin/app/_objs/boost-log-test/log_test.pic.o:log_test.cpp:function boost::log::v2_mt_posix::aux::light_rw_mutex::unlock_shared(): error: undefined reference to 'pthread_rwlock_unlock'
bazel-out/k8-fastbuild/bin/app/_objs/boost-log-test/log_test.pic.o:log_test.cpp:function boost::log::v2_mt_posix::attribute_name::attribute_name(char const*): error: undefined reference to 'boost::log::v2_mt_posix::attribute_name::get_id_from_string(char const*)'
bazel-out/k8-fastbuild/bin/app/_objs/boost-log-test/log_test.pic.o:log_test.cpp:function boost::log::v2_mt_posix::record::reset(): error: undefined reference to 'boost::log::v2_mt_posix::record_view::public_data::destroy(boost::log::v2_mt_posix::record_view::public_data const*)'
bazel-out/k8-fastbuild/bin/app/_objs/boost-log-test/log_test.pic.o:log_test.cpp:function boost::log::v2_mt_posix::record boost::log::v2_mt_posix::sources::basic_composite_logger<char, boost::log::v2_mt_posix::sources::severity_logger_mt<boost::log::v2_mt_posix::trivial::severity_level>, boost::log::v2_mt_posix::sources::multi_thread_model<boost::log::v2_mt_posix::aux::light_rw_mutex>, boost::log::v2_mt_posix::sources::features<boost::log::v2_mt_posix::sources::severity<boost::log::v2_mt_posix::trivial::severity_level> > >::open_record<boost::parameter::aux::tagged_argument_list_of_1<boost::parameter::aux::tagged_argument<boost::log::v2_mt_posix::keywords::tag::severity, boost::log::v2_mt_posix::trivial::severity_level const> > >(boost::parameter::aux::tagged_argument_list_of_1<boost::parameter::aux::tagged_argument<boost::log::v2_mt_posix::keywords::tag::severity, boost::log::v2_mt_posix::trivial::severity_level const> > const&): error: undefined reference to 'boost::log::v2_mt_posix::core::get_logging_enabled() const'
bazel-out/k8-fastbuild/bin/app/_objs/boost-log-test/log_test.pic.o:log_test.cpp:function boost::log::v2_mt_posix::aux::record_pump<boost::log::v2_mt_posix::sources::severity_logger_mt<boost::log::v2_mt_posix::trivial::severity_level> >::record_pump(boost::log::v2_mt_posix::sources::severity_logger_mt<boost::log::v2_mt_posix::trivial::severity_level>&, boost::log::v2_mt_posix::record&): error: undefined reference to 'boost::log::v2_mt_posix::aux::stream_provider<char>::allocate_compound(boost::log::v2_mt_posix::record&)'
bazel-out/k8-fastbuild/bin/app/_objs/boost-log-test/log_test.pic.o:log_test.cpp:function boost::log::v2_mt_posix::aux::record_pump<boost::log::v2_mt_posix::sources::severity_logger_mt<boost::log::v2_mt_posix::trivial::severity_level> >::auto_release::~auto_release(): error: undefined reference to 'boost::log::v2_mt_posix::aux::stream_provider<char>::release_compound(boost::log::v2_mt_posix::aux::stream_provider<char>::stream_compound*)'
bazel-out/k8-fastbuild/bin/app/_objs/boost-log-test/log_test.pic.o:log_test.cpp:function boost::log::v2_mt_posix::sources::aux::severity_level<boost::log::v2_mt_posix::trivial::severity_level>::set_value(boost::log::v2_mt_posix::trivial::severity_level): error: undefined reference to 'boost::log::v2_mt_posix::sources::aux::get_severity_level()'
bazel-out/k8-fastbuild/bin/app/_objs/boost-log-test/log_test.pic.o:log_test.cpp:function boost::log::v2_mt_posix::record boost::log::v2_mt_posix::sources::basic_logger<char, boost::log::v2_mt_posix::sources::severity_logger_mt<boost::log::v2_mt_posix::trivial::severity_level>, boost::log::v2_mt_posix::sources::multi_thread_model<boost::log::v2_mt_posix::aux::light_rw_mutex> >::open_record_unlocked<boost::parameter::aux::tagged_argument_list_of_1<boost::parameter::aux::tagged_argument<boost::log::v2_mt_posix::keywords::tag::severity, boost::log::v2_mt_posix::trivial::severity_level const> > >(boost::parameter::aux::tagged_argument_list_of_1<boost::parameter::aux::tagged_argument<boost::log::v2_mt_posix::keywords::tag::severity, boost::log::v2_mt_posix::trivial::severity_level const> > const&): error: undefined reference to 'boost::log::v2_mt_posix::core::open_record(boost::log::v2_mt_posix::attribute_set const&)'
bazel-out/k8-fastbuild/bin/app/_objs/boost-log-test/log_test.pic.o:log_test.cpp:function boost::log::v2_mt_posix::sources::basic_logger<char, boost::log::v2_mt_posix::sources::severity_logger_mt<boost::log::v2_mt_posix::trivial::severity_level>, boost::log::v2_mt_posix::sources::multi_thread_model<boost::log::v2_mt_posix::aux::light_rw_mutex> >::push_record_unlocked(boost::log::v2_mt_posix::record&&): error: undefined reference to 'boost::log::v2_mt_posix::core::push_record_move(boost::log::v2_mt_posix::record&)'
bazel-out/k8-fastbuild/bin/app/_objs/boost-log-test/log_test.pic.o:log_test.cpp:function boost::log::v2_mt_posix::value_extractor<boost::log::v2_mt_posix::trivial::severity_level, boost::log::v2_mt_posix::fallback_to_none, boost::log::v2_mt_posix::trivial::tag::severity>::operator()(boost::log::v2_mt_posix::attribute_name const&, boost::log::v2_mt_posix::attribute_value_set const&) const: error: undefined reference to 'boost::log::v2_mt_posix::attribute_value_set::find(boost::log::v2_mt_posix::attribute_name) const'
bazel-out/k8-fastbuild/bin/app/_objs/boost-log-test/log_test.pic.o:log_test.cpp:function boost::log::v2_mt_posix::value_extractor<boost::log::v2_mt_posix::trivial::severity_level, boost::log::v2_mt_posix::fallback_to_none, boost::log::v2_mt_posix::trivial::tag::severity>::operator()(boost::log::v2_mt_posix::attribute_name const&, boost::log::v2_mt_posix::attribute_value_set const&) const: error: undefined reference to 'boost::log::v2_mt_posix::attribute_value_set::end() const'
bazel-out/k8-fastbuild/bin/app/_objs/boost-log-test/log_test.pic.o:log_test.cpp:function boost::log::v2_mt_posix::value_extractor<boost::log::v2_mt_posix::trivial::severity_level, boost::log::v2_mt_posix::fallback_to_none, boost::log::v2_mt_posix::trivial::tag::severity>::operator()(boost::log::v2_mt_posix::attribute_name const&, boost::log::v2_mt_posix::attribute_value_set const&) const: error: undefined reference to 'boost::log::v2_mt_posix::aux::attach_attribute_name_info(boost::exception&, boost::log::v2_mt_posix::attribute_name const&)'
collect2: error: ld returned 1 exit status
Target //app:boost-log-test failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 0.175s, Critical Path: 0.04s
INFO: 2 processes: 2 internal.
FAILED: Build did NOT complete successfully

CodePudding user response:

  1. --cxxopt options are passed to the compilation step not the link step, so anything of the form --cxxopt=-lsomelib. Use --linkopts to configure linking flags on the command line, or add, for example, linkopts = ['-pthread'] to cc targets.
  2. You may want to save yourself some effort and use the preëxisting Bazel ruleset for Boost.
  • Related