首页 文章

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

提问于
浏览
0

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

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

Unresolved Symbol Errors

错误LNK2019未解析的外部符号“declspec(dllimport)public: thishisall 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中引用

我已经将我的dll头包含在客户端C项目中,我的项目引用了C / CLI包装器dll,以及我的import语句用于导入/导出 . 我的非常简单的代码如下所示 . 我没有使用任何MFC . I am using 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;
}

Reference to Wrapper

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

    我的印象是项目引用处理了幕后的任何其他依赖设置 . 显然,事实并非如此 . 需要将.lib文件添加为附加依赖项as is described here . 但是,作为described by this Microsoft document,当我使用非 /clr dll时,一切都没有额外的依赖性,所以我不确定为什么只有我的CLR引用需要额外的依赖 . 显然,我需要更多地阅读这些内容 .

    无论如何,我的C客户项目要求如下所列 . 我错过了第二个要求 . 此外,here is an example project帮助我诊断问题 .

    1.) Add project reference

    enter image description here

    2.) Add the .lib file as an `Additional Dependency.

    enter image description here

    [可选]使用 Additional Library Directories

    enter image description here

    3.) #include .h file in code where appropriate

    enter image description here

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

    enter image description here

    编辑:为什么需要额外的依赖关系,以及替代选项

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

相关问题