检测到“DEBUG”的 Log4cpp 命名冲突。

Posted

技术标签:

【中文标题】检测到“DEBUG”的 Log4cpp 命名冲突。【英文标题】:Log4cpp Naming Collision for 'DEBUG' detected. 【发布时间】:2018-04-23 14:52:04 【问题描述】:

问题

编译我的项目时,log4cpp 文件出现错误。错误如下: /usr/include/log4cpp/Priority.hh:49:2: erreur: #error Naming collision for 'DEBUG' detected. Please read the FAQ for a workaround.

文件

此错误引用的文件是随 Log4cpp 安装的标头。这里是。错误在第 49、78 和 109 行

/*
 * Priority.hh
 *
 * Copyright 2000, LifeLine Networks BV (www.lifeline.nl). All rights reserved.
 * Copyright 2000, Bastiaan Bakker. All rights reserved.
 *
 * See the COPYING file for the terms of usage and distribution.
 */

#ifndef _LOG4CPP_PRIORITY_HH
#define _LOG4CPP_PRIORITY_HH

#include <log4cpp/Portability.hh>
#include <string>
#include <stdexcept>

/*
 * Optionally work around rudeness in windows.h on Win32.
 */
#ifdef ERROR
#ifdef LOG4CPP_FIX_ERROR_COLLISION

namespace log4cpp 
    static const int _tmpERRORValue = ERROR;


#undef ERROR
    static const int ERROR = log4cpp::_tmpERRORValue;
#define ERROR ERROR

#else  // LOG4CPP_FIX_ERROR_COLLISION
#error Naming collision for 'ERROR' detected. Please read the FAQ for a \
       workaround. 
#endif // LOG4CPP_FIX_ERROR_COLLISION 

#endif // ERROR

/*
 * Other Win32 rudeness in EDK.h
 */
#ifdef DEBUG

#ifdef LOG4CPP_FIX_ERROR_COLLISION

#undef DEBUG
#define DEBUG DEBUG

#else  // LOG4CPP_FIX_ERROR_COLLISION
#error Naming collision for 'DEBUG' detected. Please read the FAQ for a \
       workaround. 
#endif // LOG4CPP_FIX_ERROR_COLLISION 

#endif // DEBUG

namespace log4cpp 

    /**
     * The Priority class provides importance levels with which one
     * can categorize log messages.
     **/
    class LOG4CPP_EXPORT Priority 
        public:

        static const int MESSAGE_SIZE; // = 8;

        /**
         * Predefined Levels of Priorities. These correspond to the
         * priority levels used by syslog(3).
         **/
        typedef enum EMERG  = 0, 
              FATAL  = 0,
                      ALERT  = 100,
                      CRIT   = 200,
                      ERROR  = 300, 
                      WARN   = 400,
                      NOTICE = 500,
                      INFO   = 600,
                      DEBUG  = 700,
                      NOTSET = 800
         PriorityLevel;

        /**
         * The type of Priority Values
         **/
        typedef int Value;

        /**
         * Returns the name of the given priority value.
         * Currently, if the value is not one of the PriorityLevel values,
         * the method returns the name of the largest priority smaller 
         * the given value.
         * @param priority the numeric value of the priority.
         * @returns a string representing the name of the priority.
         **/
        static const std::string& getPriorityName(int priority) throw();

    /**
     * Returns the value of the given priority name. 
     * This can be either one of EMERG ... NOTSET or a 
     * decimal string representation of the value, e.g. '700' for DEBUG.
     * @param priorityName the string containing the the of the priority
     * @return the value corresponding with the priority name
     * @throw std::invalid_argument if the priorityName does not 
     * correspond with a known Priority name or a number
     **/
        static Value getPriorityValue(const std::string& priorityName)
    throw(std::invalid_argument);
    ;


#endif // _LOG4CPP_PRIORITY_HH

研究

我在log4cpp's Sourceforge 上找到了唯一引用此问题的常见问题解答。这是它所说的:

这是由于某些平台的粗鲁导致 带有一些直言不讳的#defines 的命名空间。更准确地说,Win32 API 包括“错误”和“调试”的#defines。由于预处理器是 不知道 C++ 命名范围,这会导致保留单词 ERROR 到处都是调试。特别是这与 log4cpp::Prioritiy::ERROR 和 log4cpp::Priority::DEBUG。后者 两个名字来自log4j,所以不是我们编的 我们自己。他们 Win32 作者不应该粗暴地声称这些 通过预处理器的通用名称。有很多更好的 替代品:

If they use it as an integer constant, declare it using a language construct. Either 'enum ERROR=1;' or 'static const int ERROR=1;'

会很好。 使用不太通用的名称,如 WIN32API_ERROR,以降低命名冲突的可能性 如果他们将其用作条件编译的标志,请使用“#define DEBUG DEBUG”和“#if defined(DEBUG)”。在这种情况下 预处理器将简单地替换所有出现的“DEBUG” 带有“DEBUG”的源代码,实际上保持一切完好无损。

当然,正确的解决方案是,如果违规方 使用上述方法之一,但我们可能需要等待一段时间 这实际上发生了。作为替代 log4cpp 可以解决 这些#defines。通过执行#define 启用解决方法代码 LOG4CPP_FIX_ERROR_COLLISION 1 before #including any log4cpp header 文件和 #include 之后的所有平台标头。对于 Win32 平台 此#define 已包含在 log4cpp/config-win32.h 中。

一旦 log4cpp 更新到 log4j 1.2 API,我们就可以摆脱 通过采用新的日志级别名称来解决这个问题。

上下文

问题是,我不应该更改源代码。我只能修改编译配置文件(本项目使用 Apache Ant builder、build.xml 文件和 bash 脚本)。

我对这个项目很陌生,我不能向以前的开发人员寻求帮助。

问题

以前有人遇到过这个错误吗?除了更改源代码及其定义之外,还有其他可能的解决方法吗?我的环境变量是原因吗?

我会继续搜索,但任何见解都会很有用。谢谢 !

【问题讨论】:

【参考方案1】:

就像文档中所说的那样,您需要定义LOG4CPP_FIX_ERROR_COLLISION。如果您不允许修改任何源代码,您应该可以在 build.xml 中进行修改:

<define name="LOG4CPP_FIX_ERROR_COLLISION" value="1" />

【讨论】:

您好,感谢您的快速回答。似乎它是在项目的其他部分、Qt 项目文件 (.pro/.pri) 和 Makefiles 中定义的。我将测试在其中添加定义。会及时通知您。

以上是关于检测到“DEBUG”的 Log4cpp 命名冲突。的主要内容,如果未能解决你的问题,请参考以下文章

招摇错误:冲突的schemaIds:为类型A和B检测到重复的schemaIds

log4cpp简单使用及踩到的坑

禁用 Git 重命名检测

tp模式 生命周期 命名空间 路由

log4cpp 日志库

log4cpp 在一段时间后停止正常工作