由于不想搞到linux下开发,所以使用VS2022来开发linux下的应用程序。

因为C++的应用使用 boost库,这时候创建简单的工程都可以编译,但是到log这个库,就怎么搞都不成功了。

它会提示下面这个出错:

`boost::log::v2_mt_posix::attribute_name::get_id_from_string(char const*)'

后来怀疑程序是否有问题,就在linux下进行命令编译:

g++ main.cpp -o main -lboost_log -static -pthread -lboost_thread

这样是可以编译通过的,那么说明就是VS2022的配置问题了。

但是找了不少资料,都没有这方面的配置。

仔细地查看了整个程序的配置参数,都没有发现什么问题。

最后才发现一个问题,在linux命令行里编译时,可以把多个参数一起写入去,它们会分配到不同阶段使用,但是 VS2022就不行了了。

因此把添加的库放到下面的配置:

并且要注意输入库的名称为boost_thread, boost_log,不要输入别的名称,它在linux下的名称为:

libboost_log.a,所以要注意这个变换规则,首先要把lib去掉,再要把.a去掉。

这样添加之后,还是编译不会通过,还会提示出错。

这时就需要在命令行里添加下面两个参数了:

通过定义-static是静态编译,-pthread是使用线程库。

通过上面的设置才可以完成编译。

boost log的例子如下:

/*
 *          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)
 */


#include <ostream>
#include <fstream>
#include <boost/smart_ptr/shared_ptr.hpp>
#include <boost/smart_ptr/make_shared_object.hpp>
#include <boost/optional.hpp>
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/sinks/sync_frontend.hpp>
#include <boost/log/sinks/text_ostream_backend.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/utility/formatting_ostream.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/attributes/value_extraction.hpp>

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

//[ example_tutorial_formatters_custom
void my_formatter(logging::record_view const& rec, logging::formatting_ostream& strm)
{
    // Get the LineID attribute value and put it into the stream
    strm << logging::extract< unsigned int >("LineID", rec) << ": ";

    // The same for the severity level.
    // The simplified syntax is possible if attribute keywords are used.
    strm << "<" << rec[logging::trivial::severity] << "> ";

    // Finally, put the record message to the stream
    strm << rec[expr::smessage];
}

void init()
{
    typedef sinks::synchronous_sink< sinks::text_ostream_backend > text_sink;
    boost::shared_ptr< text_sink > sink = boost::make_shared< text_sink >();

    sink->locked_backend()->add_stream(
        boost::make_shared< std::ofstream >("sample.log"));

    sink->set_formatter(&my_formatter);

    logging::core::get()->add_sink(sink);
}
//]

int main(int, char* [])
{
    init();
    logging::add_common_attributes();

    using namespace logging::trivial;
    src::severity_logger< severity_level > lg;

    BOOST_LOG_SEV(lg, trace) << "A trace severity message";
    BOOST_LOG_SEV(lg, debug) << "A debug severity message";
    BOOST_LOG_SEV(lg, info) << "An informational severity message";
    BOOST_LOG_SEV(lg, warning) << "A warning severity message";
    BOOST_LOG_SEV(lg, error) << "An error severity message";
    BOOST_LOG_SEV(lg, fatal) << "A fatal severity message";

    return 0;
}

Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐