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)'

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Boost是一个开源的C++集合,提供了许多功能强大的组件和工具,用于增强C++语言的功能和性能。Qt是一个跨平台的应用程序开发框架,而MinGW是一个在Windows平台上使用的GNU工具集。 要编译Boost并与Qt和MinGW一起使用,可以按照以下步骤进行操作: 1. 下载Boost:首先,你需要从Boost官方网站(https://www.boost.org/)下载Boost的源代码。选择适合你的操作系统和编译器的版本。 2. 解压Boost:将下载的Boost源代码解压到你选择的目录中。 3. 打开命令提示符:在Windows上,按下Win + R键,输入"cmd"并按下Enter键,打开命令提示符。 4. 进入Boost目录:使用cd命令进入你解压Boost源代码的目录。 5. 运行bootstrap.bat:在命令提示符中运行bootstrap.bat脚本文件,它将为你的系统配置编译环境。 6. 编译Boost:运行b2命令来编译Boost。你可以使用一些选项来指定编译器和其他参数。例如,如果你想使用MinGW编译器,可以运行以下命令: ``` b2 toolset=gcc ``` 7. 安装Boost:编译完成后,运行b2 install命令来安装Boost。这将把编译好的文件复制到指定的目录中。 8. 配置Qt项目:在Qt项目中使用Boost时,你需要在项目文件(.pro文件)中添加相应的配置。例如,如果你想使用Boost的某个模块,可以在.pro文件中添加以下行: ``` LIBS += -lboost_module INCLUDEPATH += /path/to/boost ``` 以上是编译Boost并与Qt和MinGW一起使用的基本步骤。具体的配置和使用方法可能因你的项目需求和环境而有所不同。如果遇到问题,可以参考Boost官方文档或在相关社区寻求帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值