gpt4 book ai didi

c++ - 链接到共享库时,Boost Log 无法正常工作

转载 作者:行者123 更新时间:2023-11-28 07:12:55 26 4
gpt4 key购买 nike

我尝试这样做:在使用 Boost::Log 的 Windows (*.dll) 上创建 1 个共享库(我想将 Boost::Log 静态链接到这个库,因为我希望所有内容都打包在一个 DLL 文件中)但是不成功

我的项目有如下4个文件:

我的 CMakeLists.txt:(还有另一个 CMakeLists.txt 使用 find_package(Boost 1.54.0 REQUIRED thread log log_setup filesystem date_time system) 找到 Boost 库)

cmake_minimum_required(VERSION 2.6)

add_library(mylog SHARED
mylog.cpp
)
target_link_libraries(mylog ${Boost_LIBRARIES})

if(UNIX)
target_link_libraries(mylog rt)
endif(UNIX)

add_executable(testlog
main.cpp
)
target_link_libraries(testlog mylog)

我的mylog.cpp:

#include "mylog.h"

namespace logging = boost::log;
namespace expr = boost::log::expressions;
namespace keywords = boost::log::keywords;

__declspec( dllexport ) void initLog()
{
logging::add_file_log(
keywords::file_name = "testlog.log",
keywords::format = expr::format("%1% [%2%] %3% ")
% expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d, %H:%M:%S.%f")
% expr::attr< logging::trivial::severity_level >("Severity")
% expr::smessage
);

logging::add_common_attributes();
}

mylog.h:

#ifndef _MYLOG_H
#define _MYLOG_H

#include <boost/log/common.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/attributes.hpp>
#include <boost/log/sinks.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/console.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/support/date_time.hpp>
#include <boost/date_time/local_time/local_time.hpp>
#include <boost/log/utility/empty_deleter.hpp>

__declspec( dllexport ) void initLog();

#endif //_MYLOG_H

我的main.cpp:

#include "mylog.h"

int main(int, char*[])
{
using namespace boost::log::trivial;
initLog();
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;
}

当我将库 mylog 编译为一个static 库(没有 SHARED 关键字)并运行程序 teSTLog 时,日志消息是 保存在文件 teSTLog.log 中,没有任何内容打印到屏幕上,如代码所述:

2013-12-20, 15:05:36.741156 [trace] A trace severity message         
2013-12-20, 15:05:36.742156 [debug] A debug severity message
2013-12-20, 15:05:36.743156 [info] An informational severity message
2013-12-20, 15:05:36.743156 [warning] A warning severity message
2013-12-20, 15:05:36.743156 [error] An error severity message
2013-12-20, 15:05:36.743156 [fatal] A fatal severity message

但是如果我把它编译成一个动态链接库共享库(使用SHARED关键字),编译成功但结果却不如预期。 没有创建日志文件并且消息以不同的格式显示在屏幕上。看起来函数 initLog 没有运行:

[2013-12-20 15:06:17.195469] [0x00000e6c] [trace]   A trace severity message          
[2013-12-20 15:06:17.198470] [0x00000e6c] [debug] A debug severity message
[2013-12-20 15:06:17.198470] [0x00000e6c] [info] An informational severity message
[2013-12-20 15:06:17.199470] [0x00000e6c] [warning] A warning severity message
[2013-12-20 15:06:17.199470] [0x00000e6c] [error] An error severity message
[2013-12-20 15:06:17.200470] [0x00000e6c] [fatal] A fatal severity message

请帮我解决这个问题。谢谢。

P/S:我也试过为日志记录创建自定义接收器,而不是使用 Boost::Log::Trivial,但结果是一样的。 Boost 1.54.0 和 1.55.0 都经过测试。

最佳答案

看来您需要适本地配置和构建 boost::log 以支持动态链接。否则,它采用静态链接模型。

此处的文档:http://boost-log.sourceforge.net/libs/log/doc/html/log/installation/config.html

相关引用:

The library has a separately compiled part which should be built as described in the Getting Started guide. One thing should be noted, though. If your application consists of more than one module (e.g. an exe and one or several dll's) that use Boost.Log, the library must be built as a shared object. If you have a single executable or a single module that works with Boost.Log, you may build the library as a static library.

关于c++ - 链接到共享库时,Boost Log 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20698295/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com