Patrick.SE
Patrick.SE

Reputation: 4564

Inconsistent Linkage in VS2010

I was following a tutorial on how to create a C++/Cli DLL, for some reason I get a warning for each function declaration, here's the whole code:

// KRecognizer.h

#pragma once

namespace KR
{
    class __declspec(dllimport) KinectRecognizer
    {
        public:
            KinectRecognizer();
            ~KinectRecognizer();
            int Display();
    };
}

_

//  KRecognizer.cpp
#include "stdafx.h"
#include "KRecognizer.h"

using namespace System;

KR::KinectRecognizer::KinectRecognizer()
{
}

KR::KinectRecognizer::~KinectRecognizer()
{
}

int 
KR::KinectRecognizer::Display()
{
    Console::WriteLine(L"Writing a line");
    return 100;
}

Here are the error outputs:

http://pastie.org/3678144

I'm compiling with the /clr flag.

Upvotes: 0

Views: 1273

Answers (1)

Mark Tolonen
Mark Tolonen

Reputation: 177600

The header declares DLL import, which means the definition of the class comes from a DLL. Since you are providing the definition, this gives the linkage error. You'll want to use __declspec(dllexport) instead when defining the DLL.

Since you'll want to use the same header file in the app that will use the DLL, the following idiom is often used:

#ifdef MYAPI_EXPORTS
#   define MYAPI __declspec(dllexport)
#else
#   define MYAPI __declspec(dllimport)
#endif

And then use:

class MYAPI KinectRecognizer

#define MYAPI_EXPORTS before including the header in the DLL, but do not define it in the application using the header to import the DLL.

Upvotes: 1

Related Questions