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

时间:2018-04-23 14:52:05

标签: c++ compilation pre-compilation log4cpp

问题

编译项目时,我收到 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   包括'ERROR'和'DEBUG'的#defines。由于预处理器是   如果不了解C ++命名范围,则会导致保留ERROR字样   和DEBUG到处都是。特别是这与之相冲突   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;'
     

会好起来的。       使用较不通用的名称(如WIN3​​2API_ERROR)可以降低命名冲突的可能性       如果他们将它用作条件编译的标志,请使用'#define DEBUG DEBUG'和'#if defined(DEBUG)'。在那种情况下   预处理器将简单地替换所有出现的'DEBUG'   带有'DEBUG'的源代码,实际上将所有内容保持不变。

     

当然,正确的解决方案是违规方会   使用上述方法之一,但我们可能需要等待一段时间   这真的发生了。作为替代方案,log4cpp可以解决   这些#defines。通过#define启用变通方法代码   在#including任何log4cpp标头之前的LOG4CPP_FIX_ERROR_COLLISION 1   文件和#including所有平台标题之后。对于Win32平台   这个#define已经包含在log4cpp / config-win32.h中。

     

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

上下文

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

我对这个项目很陌生,我不能向前一个开发者寻求帮助。

问题

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

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

1 个答案:

答案 0 :(得分:0)

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

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