I have a native C++ library (let’s call it CoreLib.dll) and it exposes two classes:
- Core.h
- Atom.h
I have a CLI/C++ wrapper (let’s call it CoreWrapper.dll) that allows .NET projects to instantiate Core and Atom objects:
- CoreDotNet.h
- AtomDotNet.h (includes Atom.h and CoreDotNet.h)
When I compile the CoreWrapper, only CoreDotNet.h gets compiled and AtomDotNet.h gets ignored. If I want to compile AtomDotNet.h, then I must include it in CoreDotNet.h, but that causes a compiler error in CoreDotNet.h:
error C2011: 'CoreWrapperNS::CoreDotNet' : 'class' type redefinition
Here is some basic code that represents what I’m doing:
#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
};
}
Here is the AtomDotNet.h file:
#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
};
}
The CoreWrapper project has a reference to the CoreLib project. I’ve seen some posts around the “Internets” about CLI/C++ wrappers getting the above mentioned compiler error because they reference the C++ project AND they include the header file, but I didn’t have that problem until I added a second class (i.e. the AtomDotNet class) to the wrapper library and I tried to compile it. Any ideas on what might be happening here?
Merely writing your code in header files doesn’t cause it to be included in the project.
You need to add .cpp files to the project, and
#includethe headers.