在 C++/CLI 中使用 unique_ptr 时出现链接器错误

2023-11-26

我目前正在转换我的实例auto_ptr to unique_ptr,但我遇到了一个问题。它在代码的 C++ 部分中运行良好,但在我的托管 C++/CLI 层(该软件同时使用 C# 和 C++)中执行此操作时,我收到链接错误。它编译得很好,但在链接时会中断。从来没有任何问题auto_ptr.

我目前正在使用 Visual Studio 2010。有人知道使用时有任何问题吗?unique_ptr在 C++/CLI 中?

我试图在下面的一段代码中总结我的问题,但请注意下面的代码实际编译并运行(我检查了指针的所有权是否已正确移动)。编译时我没有收到链接错误,但下面的代码是纯 C++ 而不是 C++/CLI。我只是想有一个关于如何构造代码的最小示例,以便链接器错误更易于阅读。

#include "stdafx.h"
#include <vector>
#include <memory>
#include <utility>

using namespace std;

namespace Test {

template< class T >
struct LinAlgPoint3 {
  LinAlgPoint3() { x = y = z = 0; };

  union {
    struct {T x,y,z;} ;
    T data_[3];
  };
};

class ContainerClass
{
public:
  void setUniquePtr(
    unique_ptr< vector< LinAlgPoint3< float > > > newUniquePtr1 ,
    unique_ptr< vector< unsigned char > > newUniquePtr2 )
  {
    m_uniquePtr1 = move(newUniquePtr1);
    m_uniquePtr2 = move(newUniquePtr2);
  }

private:
  unique_ptr< vector< LinAlgPoint3< float > > > m_uniquePtr1;
  unique_ptr< vector< unsigned char > > m_uniquePtr2;
};

int main(int argc, char** argv)
{
  auto pos = unique_ptr< vector< LinAlgPoint3< float > > >( new vector< LinAlgPoint3< float > >() );
  auto name = unique_ptr< vector< unsigned char > >(new vector< unsigned char >());
  ContainerClass container;
  container.setUniquePtr(move(pos), move(name));
}

} //namespace Test

我在链接时遇到的错误如下:

error LNK2028: unresolved token (0A0018A5) "private: __cdecl std::unique_ptr<class std::vector<struct LinAlgPoint3<float>,class std::allocator<struct LinAlgPoint3<float> > >,struct std::default_delete<class std::vector<struct LinAlgPoint3<float>,class std::allocator<struct LinAlgPoint3<float> > > > >::unique_ptr<class std::vector<struct LinAlgPoint3<float>,class std::allocator<struct LinAlgPoint3<float> > >,struct std::default_delete<class std::vector<struct LinAlgPoint3<float>,class std::allocator<struct LinAlgPoint3<float> > > > >(class std::unique_ptr<class std::vector<struct LinAlgPoint3<float>,class std::allocator<struct LinAlgPoint3<float> > >,struct std::default_delete<class std::vector<struct LinAlgPoint3<float>,class std::allocator<struct LinAlgPoint3<float> > > > > const &)" (??0?$unique_ptr@V?$vector@U?$LinAlgPoint3@M@Test@@V?$allocator@U?$LinAlgPoint3@M@Test@@@std@@@std@@U?$default_delete@V?$vector@U?$LinAlgPoint3@M@Test@@V?$allocator@U?$LinAlgPoint3@M@Test@@@std@@@std@@@2@@std@@$$FAEAA@AEBV01@@Z) referenced in function "public: static void __clrcall std::unique_ptr<class std::vector<struct LinAlgPoint3<float>,class std::allocator<struct LinAlgPoint3<float> > >,struct std::default_delete<class std::vector<struct LinAlgPoint3<float>,class std::allocator<struct LinAlgPoint3<float> > > > >::<MarshalCopy>(class std::unique_ptr<class std::vector<struct LinAlgPoint3<float>,class std::allocator<struct LinAlgPoint3<float> > >,struct std::default_delete<class std::vector<struct LinAlgPoint3<float>,class std::allocator<struct LinAlgPoint3<float> > > > > *,class std::unique_ptr<class std::vector<struct LinAlgPoint3<float>,class std::allocator<struct LinAlgPoint3<float> > >,struct std::default_delete<class std::vector<struct LinAlgPoint3<float>,class std::allocator<struct LinAlgPoint3<float> > > > > *)" (?<MarshalCopy>@?$unique_ptr@V?$vector@U?$LinAlgPoint3@M@Test@@V?$allocator@U?$LinAlgPoint3@M@Test@@@std@@@std@@U?$default_delete@V?$vector@U?$LinAlgPoint3@M@Test@@V?$allocator@U?$LinAlgPoint3@M@Test@@@std@@@std@@@2@@std@@$$FSMXPEAV12@0@Z)
1>TestClass.obj : error LNK2028: unresolved token (0A0018A6) "private: __cdecl std::unique_ptr<class std::vector<unsigned char,class std::allocator<unsigned char> >,struct std::default_delete<class std::vector<unsigned char,class std::allocator<unsigned char> > > >::unique_ptr<class std::vector<unsigned char,class std::allocator<unsigned char> >,struct std::default_delete<class std::vector<unsigned char,class std::allocator<unsigned char> > > >(class std::unique_ptr<class std::vector<unsigned char,class std::allocator<unsigned char> >,struct std::default_delete<class std::vector<unsigned char,class std::allocator<unsigned char> > > > const &)" (??0?$unique_ptr@V?$vector@EV?$allocator@E@std@@@std@@U?$default_delete@V?$vector@EV?$allocator@E@std@@@std@@@2@@std@@$$FAEAA@AEBV01@@Z) referenced in function "public: static void __clrcall std::unique_ptr<class std::vector<unsigned char,class std::allocator<unsigned char> >,struct std::default_delete<class std::vector<unsigned char,class std::allocator<unsigned char> > > >::<MarshalCopy>(class std::unique_ptr<class std::vector<unsigned char,class std::allocator<unsigned char> >,struct std::default_delete<class std::vector<unsigned char,class std::allocator<unsigned char> > > > *,class std::unique_ptr<class std::vector<unsigned char,class std::allocator<unsigned char> >,struct std::default_delete<class std::vector<unsigned char,class std::allocator<unsigned char> > > > *)" (?<MarshalCopy>@?$unique_ptr@V?$vector@EV?$allocator@E@std@@@std@@U?$default_delete@V?$vector@EV?$allocator@E@std@@@std@@@2@@std@@$$FSMXPEAV12@0@Z)
1>TestClass.obj : error LNK2019: unresolved external symbol "private: __cdecl std::unique_ptr<class std::vector<unsigned char,class std::allocator<unsigned char> >,struct std::default_delete<class std::vector<unsigned char,class std::allocator<unsigned char> > > >::unique_ptr<class std::vector<unsigned char,class std::allocator<unsigned char> >,struct std::default_delete<class std::vector<unsigned char,class std::allocator<unsigned char> > > >(class std::unique_ptr<class std::vector<unsigned char,class std::allocator<unsigned char> >,struct std::default_delete<class std::vector<unsigned char,class std::allocator<unsigned char> > > > const &)" (??0?$unique_ptr@V?$vector@EV?$allocator@E@std@@@std@@U?$default_delete@V?$vector@EV?$allocator@E@std@@@std@@@2@@std@@$$FAEAA@AEBV01@@Z) referenced in function "public: static void __clrcall std::unique_ptr<class std::vector<unsigned char,class std::allocator<unsigned char> >,struct std::default_delete<class std::vector<unsigned char,class std::allocator<unsigned char> > > >::<MarshalCopy>(class std::unique_ptr<class std::vector<unsigned char,class std::allocator<unsigned char> >,struct std::default_delete<class std::vector<unsigned char,class std::allocator<unsigned char> > > > *,class std::unique_ptr<class std::vector<unsigned char,class std::allocator<unsigned char> >,struct std::default_delete<class std::vector<unsigned char,class std::allocator<unsigned char> > > > *)" (?<MarshalCopy>@?$unique_ptr@V?$vector@EV?$allocator@E@std@@@std@@U?$default_delete@V?$vector@EV?$allocator@E@std@@@std@@@2@@std@@$$FSMXPEAV12@0@Z)
1>TestClass.obj : error LNK2019: unresolved external symbol "private: __cdecl std::unique_ptr<class std::vector<struct LinAlgPoint3<float>,class std::allocator<struct LinAlgPoint3<float> > >,struct std::default_delete<class std::vector<struct LinAlgPoint3<float>,class std::allocator<struct LinAlgPoint3<float> > > > >::unique_ptr<class std::vector<struct LinAlgPoint3<float>,class std::allocator<struct LinAlgPoint3<float> > >,struct std::default_delete<class std::vector<struct LinAlgPoint3<float>,class std::allocator<struct LinAlgPoint3<float> > > > >(class std::unique_ptr<class std::vector<struct LinAlgPoint3<float>,class std::allocator<struct LinAlgPoint3<float> > >,struct std::default_delete<class std::vector<struct LinAlgPoint3<float>,class std::allocator<struct LinAlgPoint3<float> > > > > const &)" (??0?$unique_ptr@V?$vector@U?$LinAlgPoint3@M@Test@@V?$allocator@U?$LinAlgPoint3@M@Test@@@std@@@std@@U?$default_delete@V?$vector@U?$LinAlgPoint3@M@Test@@V?$allocator@U?$LinAlgPoint3@M@Test@@@std@@@std@@@2@@std@@$$FAEAA@AEBV01@@Z) referenced in function "public: static void __clrcall std::unique_ptr<class std::vector<struct LinAlgPoint3<float>,class std::allocator<struct LinAlgPoint3<float> > >,struct std::default_delete<class std::vector<struct LinAlgPoint3<float>,class std::allocator<struct LinAlgPoint3<float> > > > >::<MarshalCopy>(class std::unique_ptr<class std::vector<struct LinAlgPoint3<float>,class std::allocator<struct LinAlgPoint3<float> > >,struct std::default_delete<class std::vector<struct LinAlgPoint3<float>,class std::allocator<struct LinAlgPoint3<float> > > > > *,class std::unique_ptr<class std::vector<struct LinAlgPoint3<float>,class std::allocator<struct LinAlgPoint3<float> > >,struct std::default_delete<class std::vector<struct LinAlgPoint3<float>,class std::allocator<struct LinAlgPoint3<float> > > > > *)" (?<MarshalCopy>@?$unique_ptr@V?$vector@U?$LinAlgPoint3@M@Test@@V?$allocator@U?$LinAlgPoint3@M@Test@@@std@@@std@@U?$default_delete@V?$vector@U?$LinAlgPoint3@M@Test@@V?$allocator@U?$LinAlgPoint3@M@Test@@@std@@@std@@@2@@std@@$$FSMXPEAV12@0@Z)
1>D:\Test\Test.dll : fatal error LNK1120: 4 unresolved externals

正如你所看到的(如果你能读懂那些令人难以置信的可怕消息),有一些对 MarshalCopy 的引用,这让我担心 C++/CLI 可能还不支持 unique_ptr。

软件的布局是

C# executable -> C++/CLI translation layer (dll) -> C++ dll

因此,C++ dll 使用 unique_ptr 可以正常编译,但 C++/CLI dll 无法正确链接。

我忘记提及一些非常重要的事情:如果我将 unique_ptr 用于更简单的数据类型(例如字符串),它会成功链接。例如:

  auto string1= unique_ptr< string >(new string(20000, 'S'));
  auto string2 = unique_ptr< string >(new string(20000, 'A'));
  string1= std::move(string2);

我还尝试确保使用该变量,这样编译器就不会优化它。

Edit:我刚刚测试了添加另一个接受的外部函数unique_ptr<string>,我尝试发送上面的内容string1这也打破了!因此问题一定出在生成的 DLL 之间,因为 std::move() 在每个文件/类中运行良好。


好吧,不知道它现在有多相关,但我在 C++/CLI 中遇到了完全相同的问题,并通过采用右值引用而不是值来解决它。我是说: 无效setUniquePtr(unique_ptr&&a、unique_ptr&& b)

虽然它不是最干净的方式,但它可以通过这种方式进行编译。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 C++/CLI 中使用 unique_ptr 时出现链接器错误 的相关文章

随机推荐

  • Calendar.Month 给出错误的输出

    我一直在使用java util对于所有日期和日历表示 但我在这里面临一个奇怪的问题 Calendar MONTH Calendar DAY OF MONTH等都给出错误的输出 但是当我使用Calendar getTime 我得到了正确的输出
  • Go 中的 Marshall 映射到 XML

    我尝试将地图输出为 XML 数据 但收到以下错误 xml unsupported type map string int 编组映射对于 JSON 工作得很好 所以我不明白为什么它对于 XML 不能同样工作 使用 Struct 真的是唯一的方
  • Git - 删除 Blob

    有没有一种方法或命令可以使用 ID 从 git 中删除 blob 我使用了命令 git rev list objects all git cat file batch check objectname objecttype rest gre
  • Ember:如何将 TinyMCE 文本区域字段值绑定到模型

    我在模板中嵌入了 TinyMCE 现在 我想对 TinyMCE 编辑器 实际上是一个文本区域 的内容进行值绑定 See http jsfiddle net cyclomarc wtktK 10 在文本字段中输入文本时 bodyText 中的
  • 嵌入式 HSQLDB 将数据保存到文件中

    我正在创建一个基于 spring 的 Web 应用程序 该应用程序使用嵌入式 hsqldb 我的 spring 配置非常简单
  • Xamarin 在 Android 中形成 Shadow on Frame

    Xamarin Forms 中的 Frame 类非常有限 不允许我在 Frame 后面获得阴影 我使用以下代码为 iOS 制作了一个自定义渲染器 public class RatingInfoFrameRenderer FrameRende
  • Azure Blob 存储的事务访问

    我想将文件存储在 Azure Blob 存储中 到目前为止 一切都很好 我还想存储有关该文件的附加元数据 为此 我使用 Azure SQL 数据库 因此我可以轻松查询 Blob 存储上的文件 因此 当我向存储添加新文件时 我想确保 blob
  • 单行嵌套 For 循环[重复]

    这个问题在这里已经有答案了 用Python编写这个转置矩阵的函数 def transpose m height len m width len m 0 return m i j for i in range 0 height for j i
  • 设计问题:电话拨打电话号码,还是电话号码在电话上拨打自己?

    这是从我在 DDD Yahoo 上发布的内容重新发布的 团体 在所有条件相同的情况下 您是写phone dial phoneNumber 还是phoneNumber dialOn phone 请记住未来可能的需求 除了电话号码之外的帐号 除
  • 密码中是否应该允许使用空格字符?

    我尝试过不同的网站 产品 这似乎分配得相当均匀 Windows 7 和 Gmail 允许您在密码中插入空格 Hotmail 和 Twitter 则不然 虽然在密码中允许空格会增加密码的复杂性 但似乎许多网站 程序不允许它们 是否有充分的理由
  • 如何在 SPSS 中循环变量?我想避免代码重复

    是否有 原生 SPSS 方法来循环某些变量名称 我想做的就是获取变量列表 我定义的 并为它们运行相同的过程 伪代码 这不是一个很好的例子 但很能说明问题 for i in varlist a b c do FREQUENCIES VARIA
  • CLI/C++ 到底是什么?它与“普通”c++ 有什么不同?

    首先让我澄清一下 普通 C 的含义 我目前正在阅读 Walter Savitch 的 C 中的问题解决 据我所知 这不是专门为 Microsoft 或 Unix 编写的 所以我的问题是 我在这本书中学到的内容 我用它来获取 C 的通用知识
  • 旋转下拉列表在滚动时跳跃

    为什么我的旋转器在滚动时会跳跃 我只是做以下事情 ArrayAdapter
  • 为什么可以等待 Rx observable? [复制]

    这个问题在这里已经有答案了 我刚刚注意到await关键字可以与 Rx Observable 一起使用 例如 await Observable Interval TimeSpan FromHours 1 我非常确定它只能与任务结合使用 那么是
  • 如何刷新数据网格

    我创建 dojox grid datagrid 并填充数组中的内容 如示例所示页面上的最后一个示例 在一段时间内 我在代码中更改了该数组的值 如何刷新该网格的内容 如何从更改的数组加载新数据 要更改网格中的值 您需要更改网格存储中的值 网格
  • 找到到任何子串的最小汉明距离的最快方法?

    给定一个长字符串L和一个较短的字符串S 约束条件是L length 必须 gt S length 我想找到之间的最小汉明距离S和任意子串L长度等于S 长度 让我们为此调用该函数minHamming 例如 minHamming ABCDEFG
  • 在 Laravel 中,使用 App::make('') 而不是构造函数注入有什么缺点吗?

    通常我只会通过构造函数注入依赖项 但是当父类具有依赖项并且必须将它们传递给所有子类时 它会变得非常冗长 另一种方法是使用 this gt dependancy App make Dependancy 单独在父类中 那么父构造函数和子构造函数
  • ASCII 调整和十进制调整指令如何工作?

    我一直在努力理解 x86 汇编语言的 ASCII 调整指令 我在互联网上看到所有信息告诉我不同 的事情 但我想这只是以不同形式解释的同一件事 但我仍然不明白 谁能用伪代码解释一下为什么AAA AAS我们必须从 AL 的低位半字节中加 减 6
  • 如何使用 DOJO 动态显示和隐藏整个 TabContainer?

    DOJO 似乎有一些怪癖 我特别需要在页面加载时隐藏 TabContainer 但在用户单击按钮后变得可见 我尝试的第一件事是设置 style display none 来启动 然后在单击事件上设置 style display block
  • 在 C++/CLI 中使用 unique_ptr 时出现链接器错误

    我目前正在转换我的实例auto ptr to unique ptr 但我遇到了一个问题 它在代码的 C 部分中运行良好 但在我的托管 C CLI 层 该软件同时使用 C 和 C 中执行此操作时 我收到链接错误 它编译得很好 但在链接时会中断