使用 Bazel 构建 Boost Log 示例时遇到问题

Trouble building Boost Log example using Bazel

我正在使用 Bazel 作为我的构建系统,在我开始尝试使用 Boost Log 之前,Boost 一直没有问题,我什至能够毫无问题地使用一些编译库,但是当我尝试使用 Boost.Log 或尝试构建用于简单日志记录的示例我收到大量未定义的引用错误。

我尝试使用以下 bazel 构建命令进行构建:

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

并且所有这些都导致了我在本 post 底部包含的相同错误。我的 Boost 根位于 /opt/boost/boost_1_77_0,下面是我的项目结构。

./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/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;
}

构建输出:

[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
  1. --cxxopt 选项被传递到编译步骤而不是 link 步骤,所以任何形式的 --cxxopt=-lsomelib。使用 --linkopts 在命令行上配置 linking 标志,或者将 linkopts = ['-pthread'] 添加到 cc 目标。
  2. 您可能想节省一些精力并使用 preëxisting Bazel ruleset for Boost