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:
I’m compiling with the /clr flag.
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:
And then use:
#define MYAPI_EXPORTSbefore including the header in the DLL, but do not define it in the application using the header to import the DLL.