c++ - 如何使用 C++/CLI 包装器库为 native C++ 库导出多个类

标签 c++ export wrapper command-line-interface

我有一个 native C++ 库(我们称它为 CoreLib.dll),它公开了两个类:

  • 核心.h
  • Atom.h

我有一个 CLI/C++ 包装器(我们称之为 CoreWrapper.dll),它允许 .NET 项目实例化 CoreAtom 对象:

  • CoreDotNet.h
  • AtomDotNet.h(包括 Atom.h 和 CoreDotNet.h)

当我编译 CoreWrapper 时,只有 CoreDotNet.h 被编译而 AtomDotNet.h 被忽略。如果我想编译 AtomDotNet.h,那么我必须将它包含在 CoreDotNet.h 中,但这会导致 CoreDotNet.h 中出现编译器错误:

error C2011: 'CoreWrapperNS::CoreDotNet' : 'class' type redefinition

这是代表我正在做的事情的一些基本代码:

#pragma once // <-- should protect from class type redefinition
#include "Core.h"
//#include "AtomDotNet.h" // uncommenting causes compiler error C2011

using namespace CoreNS;

namespace CoreWrapperNS
{
    public ref class CoreDotNet
    {
    public:
        // Allows users to instantiate a core object
        CoreDotNet();
        CoreDotnet(Core* core);
        //... destructor follows
    };
}

这是 AtomDotNet.h 文件:

#pragma once // <-- should protect from class type redefinition
#include "Atom.h"
#include "CoreDotNet.h"

namespace CoreWrapperNS
{
    public ref class AtomDotNet
    {
    private:
        Atom*       _atom;
        CoreDotNet^ _core;
    public:
        AtomDotNet()
        {
            // The atom allows users to instantiate an atom with a core
            // and to get a reference to the core of the given atom.
            Core* core = new Core();
            _atom = new Atom(core);
            _core = gcnew CoreDotNet(core);
        }

        inline CoreDotNet^ GetCore(){return _core;}
        //... destructor follows
    };
}

CoreWrapper 项目引用了 CoreLib 项目。我在“互联网”上看到了一些关于 CLI/C++ 包装器出现上述编译器错误的帖子,因为它们引用了 C++ 项目并且包含了头文件,但是直到我添加了第二个类(即 AtomDotNet 类)到包装器库,我尝试编译它。关于这里可能发生的事情有什么想法吗?

最佳答案

仅仅在头文件中编写您的代码并不会使其包含在项目中。

您需要将 .cpp 文件添加到项目中,并#include header 。

关于c++ - 如何使用 C++/CLI 包装器库为 native C++ 库导出多个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6918382/

相关文章:

c++ - 是否可以将 "Prototype pattern"调整为右值引用?

C++逐字符读取二进制文件

php - 在 PHP 中将 MySQL 数据导出到 Excel

javascript - 如何在纯 JavaScript 页面的不同 DOM 节点注入(inject) Angular 2 组件?

c++ - 有没有办法为 fscanf 实现包装函数?

c++ - 具有结构的列表映射(包含列表)——无法通过映射访问结构内的列表

r - 使用 dplyr 进行汇总并使用平均值和 sd (+/-) 导出表

c# - 将数据从 SQL Server 导出到 PostgreSQL

swift 包装类

c++ - 曲面 segmentation 着色器没有输出?