QT框架中使用easylogging日志库报错重定义排查办法:error: multiple definition of `el::base::elStorage

报错内容:

error: multiple definition of `el::base::elStorage' 

error: multiple definition of `el::elCrashHandler' 

error: multiple definition of `el::base::elStorage'

error: multiple definition of `el::elCrashHandler'

一般会在很多cpp里面报错重定义。

原因是我在一个  public_macro.h 文件中,将easylogging再封装了一次,成这样:
 

#ifndef PUBLIC_MACRO_H
#define PUBLIC_MACRO_H
#include "easyloggingpp/easylogging++.h"

#define VERSION_NUMBER  "1.3.1"    //当前版本号

INITIALIZE_EASYLOGGINGPP

const char* GetProcessIdAsString(const el::LogMessage*)
{
    // 获取当前进程的 ID
    qint64 pid = QCoreApplication::applicationPid();
    QString pidStr = QString::number(pid);
    return  pidStr.toStdString().c_str();
}

// 宏封装
#define ELOG_INIT(logName) \
    do { \
        el::Helpers::installCustomFormatSpecifier(el::CustomFormatSpecifier("%process", GetProcessIdAsString)); \
        el::Configurations conf; \
        conf.setGlobally(el::ConfigurationType::Format, "[%datetime] %process %fbase:%line %level - %msg"); \
        QDateTime currentDateTime = QDateTime::currentDateTime(); \
        QString logFileName =  "./log/" + currentDateTime.toString("yyyyMMdd") + "_" + (logName) + ".log"; \
        conf.setGlobally(el::ConfigurationType::Filename, logFileName.toUtf8().constData()); \
        el::Loggers::reconfigureAllLoggers(conf); \
    } while(0)

#define ELOG_INFO(message)  LOG(INFO) << (std::string(message))
#define ELOG_ERROR(message) LOG(ERROR) << (std::string(message))



#endif // PUBLIC_MACRO_H

最开始的报错是:
GetProcessIdAsString 被重定义
然后在报错:

error: multiple definition of `el::base::elStorage' 

error: multiple definition of `el::elCrashHandler' 

error: multiple definition of `el::base::elStorage'

error: multiple definition of `el::elCrashHandler'

显然问题是出在了 public_macro.h中,因为我在多个cpp中 引入了这个头文件

#include "public_macro.h"

最后解决办法是将 
INITIALIZE_EASYLOGGINGPP
和 
const char* GetProcessIdAsString(const el::LogMessage*)
{
    // 获取当前进程的 ID
    qint64 pid = QCoreApplication::applicationPid();
    QString pidStr = QString::number(pid);
    return  pidStr.toStdString().c_str();
}

直接放到了 main.cpp中
最终代码是:
main.cpp

#include "mainwindow.h"
#include 
#include 
#include "public/public_macro.h"
#include "public/pubilc_declare.h"
#include 
#include 
#include 
#include 

INITIALIZE_EASYLOGGINGPP

const char* GetProcessIdAsString(const el::LogMessage*)
{
    // 获取当前进程的 ID
    qint64 pid = QCoreApplication::applicationPid();
    QString pidStr = QString::number(pid);
    return  pidStr.toStdString().c_str();
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
    ELOG_INIT(kTestToolEN);
    ELOG_INFO(QString("当前版本号:v").append(VERSION_NUMBER).toStdString());

    MainWindow w;
    w.show();

    return a.exec();
}

public_macro.h

#ifndef PUBLIC_MACRO_H
#define PUBLIC_MACRO_H
#include "easyloggingpp/easylogging++.h"

#define VERSION_NUMBER  "1.3.1"    //当前版本号

// 宏封装
#define ELOG_INIT(logName) \
    do { \
        el::Helpers::installCustomFormatSpecifier(el::CustomFormatSpecifier("%process", GetProcessIdAsString)); \
        el::Configurations conf; \
        conf.setGlobally(el::ConfigurationType::Format, "[%datetime] %process %fbase:%line %level - %msg"); \
        QDateTime currentDateTime = QDateTime::currentDateTime(); \
        QString logFileName =  "./log/" + currentDateTime.toString("yyyyMMdd") + "_" + (logName) + ".log"; \
        conf.setGlobally(el::ConfigurationType::Filename, logFileName.toUtf8().constData()); \
        el::Loggers::reconfigureAllLoggers(conf); \
    } while(0)

#define ELOG_INFO(message)  LOG(INFO) << (std::string(message))
#define ELOG_ERROR(message) LOG(ERROR) << (std::string(message))



#endif // PUBLIC_MACRO_H

你可能感兴趣的:(C++报错解决,QT,qt,easylogging,日志库,开发语言)