LNK2028 LNK2029 在托管 c++/clr 中编译静态 c++ 库时

LNK2028 LNK2029 when compiling static c++ library in managed c++/clr

因为我几乎没有想法而且 google 结果现在已经达到了 ~10 或更少的地步,我现在转向这里希望看到答案或至少是一个线索这让我找到了实际的解决方案。

我正在创建一个可以作为可执行文件、非静态库和静态库分发的项目。 我想在 WPF 项目 (C#) 中使用该库,这就是为什么我创建了一个简单的 CLR 包装器。

在基础库中引入最新更改之前,一切都运行良好。 现在,我在库项目中对 "main" class 的所有调用都出现链接错误(总共 5 个):

1>wrapper.obj : error LNK2028: unresolved token (0A0016EB) "public: class std::vector<struct sqf::diagnostics::stackdump,class std::allocator<struct sqf::diagnostics::stackdump> > __thiscall sqf::vmstack::dump_callstack_diff(class std::shared_ptr<class sqf::callstack>)" (?dump_callstack_diff@vmstack@sqf@@$$FQAE?AV?$vector@Ustackdump@diagnostics@sqf@@V?$allocator@Ustackdump@diagnostics@sqf@@@std@@@std@@V?$shared_ptr@Vcallstack@sqf@@@4@@Z) referenced in function "public: class std::vector<struct sqf::diagnostics::stackdump,class std::allocator<struct sqf::diagnostics::stackdump> > __thiscall SqfVm::wrapper::get_stackdump(void)" (?get_stackdump@wrapper@SqfVm@@$$FQAE?AV?$vector@Ustackdump@diagnostics@sqf@@V?$allocator@Ustackdump@diagnostics@sqf@@@std@@@std@@XZ)
1>wrapper.obj : error LNK2028: unresolved token (0A0016F5) "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall sqf::virtualmachine::preprocess(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,bool &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?preprocess@virtualmachine@sqf@@$$FQAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V34@AA_N0@Z) referenced in function "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall SqfVm::wrapper::preprocess(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,bool &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?preprocess@wrapper@SqfVm@@$$FQAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V34@AA_N0@Z)
1>wrapper.obj : error LNK2028: unresolved token (0A0016F6) "public: struct sqf::parse::astnode __thiscall sqf::virtualmachine::parse_sqf_cst(class std::basic_string_view<char,struct std::char_traits<char> >,bool &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?parse_sqf_cst@virtualmachine@sqf@@$$FQAE?AUastnode@parse@2@V?$basic_string_view@DU?$char_traits@D@std@@@std@@AA_NV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@6@@Z) referenced in function "public: struct sqf::parse::astnode __thiscall sqf::virtualmachine::parse_sqf_cst(class std::basic_string_view<char,struct std::char_traits<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?parse_sqf_cst@virtualmachine@sqf@@$$FQAE?AUastnode@parse@2@V?$basic_string_view@DU?$char_traits@D@std@@@std@@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@6@@Z)
1>wrapper.obj : error LNK2019: unresolved external symbol "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall sqf::virtualmachine::preprocess(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,bool &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?preprocess@virtualmachine@sqf@@$$FQAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V34@AA_N0@Z) referenced in function "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall SqfVm::wrapper::preprocess(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,bool &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?preprocess@wrapper@SqfVm@@$$FQAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V34@AA_N0@Z)
1>wrapper.obj : error LNK2019: unresolved external symbol "public: struct sqf::parse::astnode __thiscall sqf::virtualmachine::parse_sqf_cst(class std::basic_string_view<char,struct std::char_traits<char> >,bool &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?parse_sqf_cst@virtualmachine@sqf@@$$FQAE?AUastnode@parse@2@V?$basic_string_view@DU?$char_traits@D@std@@@std@@AA_NV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@6@@Z) referenced in function "public: struct sqf::parse::astnode __thiscall sqf::virtualmachine::parse_sqf_cst(class std::basic_string_view<char,struct std::char_traits<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?parse_sqf_cst@virtualmachine@sqf@@$$FQAE?AUastnode@parse@2@V?$basic_string_view@DU?$char_traits@D@std@@@std@@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@6@@Z)
1>wrapper.obj : error LNK2019: unresolved external symbol "public: class std::vector<struct sqf::diagnostics::stackdump,class std::allocator<struct sqf::diagnostics::stackdump> > __thiscall sqf::vmstack::dump_callstack_diff(class std::shared_ptr<class sqf::callstack>)" (?dump_callstack_diff@vmstack@sqf@@$$FQAE?AV?$vector@Ustackdump@diagnostics@sqf@@V?$allocator@Ustackdump@diagnostics@sqf@@@std@@@std@@V?$shared_ptr@Vcallstack@sqf@@@4@@Z) referenced in function "public: class std::vector<struct sqf::diagnostics::stackdump,class std::allocator<struct sqf::diagnostics::stackdump> > __thiscall SqfVm::wrapper::get_stackdump(void)" (?get_stackdump@wrapper@SqfVm@@$$FQAE?AV?$vector@Ustackdump@diagnostics@sqf@@V?$allocator@Ustackdump@diagnostics@sqf@@@std@@@std@@XZ)
1>wrapper.obj : error LNK2019: unresolved external symbol "public: __thiscall sqf::virtualmachine::virtualmachine(class Logger &,unsigned __int64)" (??0virtualmachine@sqf@@QAE@AAVLogger@@_K@Z) referenced in function "public: __thiscall sqf::virtualmachine::virtualmachine(class Logger &)" (??0virtualmachine@sqf@@QAE@AAVLogger@@@Z)
1>D:\Git\SQFvm\clrBridge\Release\clrBridge.dll : fatal error LNK1120: 7 unresolved externals

项目,都可以在这里找到:

  1. C++ 项目https://github.com/SQFvm/vm
  2. C++/CLR 项目https://github.com/SQFvm/ClrBridge

能够通过添加另一个分离层(wrapper.cpp & wrapper.h)然后在 cpp 文件顶部放置一个 #pragma unmanaged 来解决 LNK2028 问题。 现在只剩下LNK2019期

对于那些将来可能会发现这个问题的用户,以下是对我有帮助的内容:

  1. 分离所有 classes
  2. #pragma managed 添加到实际 class 的非托管包装器(摆脱了 LNK2028)
  3. 以下提交之一:

希望对您有所帮助。