【问题标题】:How to build log4cxx in Visual Studio 2015如何在 Visual Studio 2015 中构建 log4cxx
【发布时间】:2015-10-19 04:14:02
【问题描述】:

我一直在使用 log4cxx,它可以满足我的需要。我们正在迁移到 VS 2015,我发现当我尝试重建 log4cxx 0.10.0.1 时,Visual Studio 2015 中的编译器会抛出错误。如果我将项目工具集更改为 Visual Studio 2013 (v120),构建仍然有效,但是当我尝试使用它时,它会在运行时出现问题。
请参阅下面的错误消息,它在多个场合出现。 有没有人找到使用 Visual Studio 2015 正确构建 log4cxx 的方法?

4>..\..\Log4cxx\log4cxx\apache-log4cxx-0.10.0\src\main\include\log4cxx/layout.h(90): error C2248: 'log4cxx::helpers::ObjectImpl::ObjectImpl': cannot access private member declared in class 'log4cxx::helpers::ObjectImpl'
4>..\..\Log4cxx\log4cxx\apache-log4cxx-0.10.0\src\main\include\log4cxx/helpers/objectimpl.h(43): note: see declaration of 'log4cxx::helpers::ObjectImpl::ObjectImpl'
4>..\..\SDKS\Log4cxx\log4cxx\apache-log4cxx-0.10.0\src\main\include\log4cxx/helpers/objectimpl.h(28): note: see declaration of 'log4cxx::helpers::ObjectImpl'
4>..\..\SDKS\Log4cxx\log4cxx\apache-log4cxx-0.10.0\src\main\include\log4cxx/layout.h(90): note: This diagnostic occurred in the compiler generated function 'log4cxx::Layout::Layout(const log4cxx::Layout &)'

【问题讨论】:

  • 显示类定义。

标签: c++ visual-studio visual-studio-2015 log4cxx


【解决方案1】:

我遇到了同样的问题,希望找到解决方法。

问题是基类ObjectImpl的定义:

    class LOG4CXX_EXPORT ObjectImpl : public virtual Object
    {
    public:
        ObjectImpl();
        virtual ~ObjectImpl();
        void addRef() const;
        void releaseRef() const;

    protected:
        mutable unsigned int volatile ref;

    private:
        //
        //   prevent object copy and assignment
        //
        ObjectImpl(const ObjectImpl&);
        ObjectImpl& operator=(const ObjectImpl&);
    };

正如您所见,该类隐藏了复制构造函数和复制赋值运算符,因为使用引用计数器的设计没有合适的实现。使用共享指针会更好。

新的 stl 实现对其集合类使用移动语义,当没有具有移动语义的复制构造函数和复制赋值运算符时,它必须使用不可用的标准运算符。

为了解决这个问题,我们必须在以下类中添加移动操作符:ObjectImpl、Layout、Filter、PatternConverter、TriggeringPolicy 和 RollingPolicyBase。

这是我对 ObjectImpl 的新实现:

    class LOG4CXX_EXPORT ObjectImpl : public virtual Object
    {
    public:
        ObjectImpl();
        virtual ~ObjectImpl();
        void addRef() const;
        void releaseRef() const;

        // added for VS2015
        ObjectImpl(ObjectImpl && o)
        {
            ref = o.ref;
            o.ref = 0;
        }

        ObjectImpl& operator=(ObjectImpl && o)
        {
            ref = o.ref;
            o.ref = 0;

            return *this;
        }
        // -----

    protected:
        mutable unsigned int volatile ref;

    private:
        //
        //   prevent object copy and assignment
        //
        ObjectImpl(const ObjectImpl&);
        ObjectImpl& operator=(const ObjectImpl&);
    };

还有另一个例子。其余类类似:

    class LOG4CXX_EXPORT Filter : public virtual OptionHandler,
        public virtual helpers::ObjectImpl
    {
        /**
        Points to the next filter in the filter chain.
        */
        FilterPtr next;
    public:
        Filter();

        // added for VS2015
        Filter(Filter && o)
            : helpers::ObjectImpl(std::move(o))
            , next( o.next )
        { }

        Filter& operator=(Filter && o)
        {
            helpers::ObjectImpl::operator=(std::move(o));
            next = o.next;
            return *this;
        }
        // end of added for VS2015

        ...

我希望这会有所帮助。

【讨论】:

  • 谢谢德克。很好的解释,现在对我来说一切都很好。
  • 可能不是很明显,但您需要为 (Layout, TriggeringPolicy) 添加默认构造函数。同样在 PatternConverter 中,应该从字段名称和样式中删除 const 说明符,否则赋值运算符将无法编译。
  • @Nik 你能发布你在其他类中所做的更改吗?
【解决方案2】:

一位同事遇到了这个问题,他通过转到File > New > Project > Visual C++ 并选择Install Windows XP Support for C++ 解决了这个问题。奇怪的是,我从来没有这样做过,但我也从来没有遇到过同样的问题。

【讨论】:

    【解决方案3】:

    以下代码是正确答案 apache-log4cxx-0.10.0vs2015

    // objectimpl.h
    namespace log4cxx
    {
       namespace helpers
       {
          /** Implementation class for Object.*/
          class LOG4CXX_EXPORT ObjectImpl : public virtual Object
          {
          public:
             ObjectImpl();
             virtual ~ObjectImpl();
             void addRef() const;
             void releaseRef() const;
    
          protected:
             mutable unsigned int volatile ref;
    
            //private:
                            //
                //   prevent object copy and assignment
                //
            ObjectImpl(const ObjectImpl&);
            ObjectImpl& operator=(const ObjectImpl&);
          };
       }
    }
    
    // objectimpl.cpp
    ObjectImpl::ObjectImpl(const ObjectImpl& o)
    {
        ref = o.ref;
        o.ref = 0;
    }
    ObjectImpl& ObjectImpl::operator=(const ObjectImpl& o)
    {
        ref = o.ref;
        o.ref = 0;
    
        return *this;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多