QT 使用boost库

环境: QT5.5.1 和 boost_1_68_0

一、Boost的编译

1. 打开cmd,输入一下的内容 gcc -v 确定gcc是否能用,不能添加环境变量 C:\Qt\Qt5.5.1\Tools\mingw492_32\bin

2. cmd进入到....\boost_1_68_0\tools\build\src\engine文件夹下执行
 build.bat gcc
 在当前目录将会生成bin.ntx86文件夹,里面包含两个exe文件b2.exe,bjam.exe,将这两个文件复制到 ....\boost_1_68_0目录下

3. cmd进入到boost_1_68_0根目录下执行

3.1 编译一个log  库

bjam.exe --toolset=gcc --with-log  variant=debug,release link=shared runtime-link=shared threading=multi debug release install

3.2 完全编译库

bjam.exe --toolset=gcc --build-type=complete  variant=debug,release link=shared runtime-link=shared threading=multi debug release install

 4. 耐心等待编译 

 5.默认在C盘根目录下生成一个Boost文件夹里面由include和lib文件夹

 

二、QT中使用编译好的Boost

新建qt c++项目文件目录结构如下,将上面C盘编译生成的Boost文件夹复制到项目目录同级下

pro文件配置

TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG -= qt
DESTDIR = $$PWD/../bin
SOURCES += main.cpp

INCLUDEPATH += $$PWD/../Boost/include/boost-1_68/

#不定义编译log会出错误
DEFINES += BOOST_LOG_DYN_LINK

LIBS    += -L$$PWD/../Boost/lib/
LIBS    += -llibboost_date_time-mgw49-mt-d-x32-1_68

LIBS    += -llibboost_log-mgw49-mt-d-x32-1_68
LIBS    += -llibboost_log_setup-mgw49-mt-d-x32-1_68

LIBS    += -llibboost_system-mgw49-mt-d-x32-1_68 -llibboost_filesystem-mgw49-mt-d-x32-1_68 \
-llibboost_thread-mgw49-mt-d-x32-1_68

cpp文件 

#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/sinks/text_file_backend.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/sources/record_ostream.hpp>

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

#if 0

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

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

// We need this due to this bug: https://svn.boost.org/trac/boost/ticket/4416
//[ example_tutorial_file_advanced_no_callouts
void init()
{
    logging::add_file_log
    (
        keywords::file_name = "sample_%N.log",
        keywords::rotation_size = 10 * 1024 * 1024,
        keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0),
        keywords::format = "[%TimeStamp%]: %Message%"
    );

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

#else

//[ example_tutorial_file_advanced
void init()
{
    logging::add_file_log
    (
        keywords::file_name = "sample_%N.log",                                        /*< file name pattern >*/
        keywords::rotation_size = 10 * 1024 * 1024,                                   /*< rotate files every 10 MiB... >*/
        keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0), /*< ...or at midnight >*/
        keywords::format = "[%TimeStamp%]: %Message%"                                 /*< log record format >*/
    );

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

#endif

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

 

三、遇到的问题

未定义BOOST_LOG_DYN_LINK会出现

error: undefined reference to `boost::log::v2s_mt_nt5::sinks::file::rotation_at_time_point::rotation_at_time_point(unsigned char, unsigned char, unsigned char)'

发布了33 篇原创文章 · 获赞 3 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/LWLGZY/article/details/103732730
今日推荐