我有一个 native C++ 库(我们称它为 CoreLib.dll),它公开了两个类:
- 核心.h
- Atom.h
我有一个 CLI/C++ 包装器(我们称之为 CoreWrapper.dll),它允许 .NET 项目实例化 Core
和 Atom
对象:
- 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/