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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章