Kiril
Kiril

Reputation: 40345

How to export multiple classes with a C++/CLI wrapper library for a native C++ library

I have a native C++ library (let's call it CoreLib.dll) and it exposes two classes:

I have a CLI/C++ wrapper (let's call it CoreWrapper.dll) that allows .NET projects to instantiate Core and Atom objects:

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?

Upvotes: 2

Views: 2399

Answers (3)

CJ1
CJ1

Reputation: 11

C++/CLI does NOT require headers, in fact one of the advantages that C++/CLI proclames is that it does away with the headers and uses reflection to gain the information it needs that would normally be included in a header. Rename your .h to .cpp and forget about .h except in native code.

To use the managed classes defined in another .CPP file in the same project, use the '#using' (similar to #include) for the obj file in CoreDotNet.cpp as follows:

#using "AtomDotNet.obj"

Also, in you CoreDotNet.cpp properties page or in your project properties, under C/C++ on the Resolve #using reference add "$(IntDir)" path so it can find the file when compiling.

This is the C++/CLI way, no more DLL hell and no more .h hell!

Upvotes: 1

Ben Voigt
Ben Voigt

Reputation: 283624

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 #include the headers.

Upvotes: 3

reece
reece

Reputation: 8145

C++/CLI does not have the concept of header files. The declaration in a header file is its implementation. So including "CoreDotNet.h" in "AtomDotNet.h" is the issue.

If you have CoreDotNet.cpp that just contains:

#include "CoreDotNet.h"

and a similar pattern for "AtomDotNet.h", then remove the "CoreDotNet.h" include from "AtomDotNet.h" you should be able to compile it.

You need to treat C++/CLI like you would a C# project and related classes/*.cs files.

Upvotes: -2

Related Questions