C ++项目中C ++ / CLI dll的未解决的符号错误

时间:2018-06-15 18:34:09

标签: c++ dll c++-cli

我正在尝试一个非常简单的C ++ / CLI包装器实现,以允许传统的C ++代码引用.Net代码,如described here。我只是试图让我的基本C ++ / CLI非托管(本机)对象链接起来,而不包括任何托管/ IL / .Net代码,我感到困惑。

我的问题是,继这个基本设置和我在下面描述的内容之后,我对这些错误感到非常困惑吗?我缺少一些考虑因素吗?也许答案是这应该有效,所以不清楚什么是错的。这仍然有用。类似的工作示例会很棒。

未解决的符号错误

  

错误LNK2019未解析的外部符号“__declspec(dllimport)   public:__ thiscall Wrapper :: Test :: Test(void)“   (__imp _ ?? 0Test @ Wrapper @@ QAE @ XZ)在函数_main NativeApp中引用

     

错误LNK2019未解析的外部符号“__declspec(dllimport)   public:__thiscall Wrapper :: Test :: ~Test(void)“   (__imp _ ?? 1Test @ Wrapper @@ QAE @ XZ)在函数_main NativeApp中引用

我已经在没有任何运气的情况下审阅了相关问题。我的客户端C ++项目中包含了我的dll头文件,我的项目引用了C ++ / CLI包装器dll,以及我的导入/导出定义语句。我的非常简单的代码如下所示。我没有使用任何MFC。 我正在使用VS2017 DumpBin.exe /exports显示的导出符号似乎与链接器错误所说的内容相匹配。

      1    0 000010D0 ??0Test@Wrapper@@QAE@XZ = ??0Test@Wrapper@@QAE@XZ (public: __thiscall Wrapper::Test::Test(void))
      2    1 000010E0 ??1Test@Wrapper@@QAE@XZ = ??1Test@Wrapper@@QAE@XZ (public: __thiscall Wrapper::Test::~Test(void))
      3    2 000010C0 ??4Test@Wrapper@@QAEAAV01@ABV01@@Z = ??4Test@Wrapper@@QAEAAV01@ABV01@@Z (public: class Wrapper::Test & __thiscall Wrapper::Test::operator=(class Wrapper::Test const &))

这是基本代码......

NativeApp.exe(项目)

NativeApp.cpp (文件)

#include "stdafx.h"
#include <iostream>
#include "Wrapper.h" //From additional includes directory

int main()
{
    std::cout << "Program Started" << std::endl;
    Wrapper::Test shell = Wrapper::Test::Test(); //Use dll
    std::cin.get();
    return 0;
}

对包装器的引用

enter image description here

Wrapper.dll(项目)

Wrapper.cpp (文件)

#include "Wrapper.h"

#pragma unmanaged
namespace Wrapper {
    Test::Test() {
    }
    Test::~Test() {
    }
}

Wrapper.h (文件)

#pragma once

#ifdef WRAPPER_EXPORTS  
#define WRAPPER_API __declspec(dllexport)   
#else  
#define WRAPPER_API __declspec(dllimport)   
#endif  

#pragma unmanaged
namespace Wrapper {
    class WRAPPER_API Test {
    public:
        Test();
        ~Test();
    };
}

1 个答案:

答案 0 :(得分:0)

给我的印象是,项目引用负责幕后的所有其他依赖项设置。显然,事实并非如此。需要添加.lib文件作为附加依赖项as is described here。但是,与described by this Microsoft document一样,当我使用非/clr dll时,一切都没有附加依赖项,因此我不确定为什么仅我的CLR参考需要附加依赖项。显然,我需要阅读更多内容。

无论如何,下面列出了我的C ++客户端项目要求。我错过了第二个要求。另外,here is an example project可以帮助我诊断问题。

1。)添加项目引用

enter image description here

2。)将.lib文件添加为“其他依赖项”。

enter image description here

[可选]使用Additional Library Directories

enter image description here

3。)#include .h文件中适当的代码

enter image description here

[可选]使用其他包含目录

enter image description here

编辑:为什么需要附加依赖项以及替代选项

如上所述,我开始迷惑为什么/ clr dll需要.lib附加依赖关系,而非clr dll则不需要。答案是因为默认情况下将/ clr项目配置为忽略导入库。因此,当该项目被另一个C ++项目引用时,该项目引用将忽略导入库。在/ clr dll项目中将此设置(“链接器”>“常规”>“忽略导入库”)更改为“否”可以解决此问题,因此不需要其他依赖项,并且项目引用的工作方式与非clr C ++ dll相同。

相关问题