简体   繁体   English

VS2010中的链接不一致

[英]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: 我正在遵循有关如何创建C ++ / Cli DLL的教程,由于某种原因,我会收到每个函数声明的警告,以下是整个代码:

// 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 http://pastie.org/3678144

I'm compiling with the /clr flag. 我正在使用/ clr标志进行编译。

The header declares DLL import, which means the definition of the class comes from a DLL. 标头声明DLL导入,这意味着类的定义来自DLL。 Since you are providing the definition, this gives the linkage error. 由于您提供的是定义,因此会出现链接错误。 You'll want to use __declspec(dllexport) instead when defining the DLL. 在定义DLL时,您将要使用__declspec(dllexport)代替。

Since you'll want to use the same header file in the app that will use the DLL, the following idiom is often used: 由于您要在将使用DLL的应用程序中使用相同的头文件,因此经常使用以下惯用法:

#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. 在将标头包含在DLL中之前,先#define MYAPI_EXPORTS ,但不要在应用程序中使用标头导入DLL来定义它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM