【问题标题】:Mixing native C++ and C++/CLI混合原生 C++ 和 C++/CLI
【发布时间】:2013-01-28 19:03:00
【问题描述】:

我有一个由原生 C++ DLL 和 C++/CLI 包装器组成的解决方案。我的目标是在原生 C++ DLL 的 C++/CLI 中创建一个包装器。

每当我尝试在包装器中创建本机 C++ 类的实例时,都会遇到许多链接器错误(见下文)。

2>  .NETFramework,Version=v4.0.AssemblyAttributes.cpp
2>NFileOperation.obj : error LNK2028: unresolved token (0A000208) "public: static bool __cdecl CFileOperation::FileExists(class ATL::CStringT<wchar_t,class ATL::StrTraitATL<wchar_t,class ATL::ChTraitsCRT<wchar_t> > > const &)" (?FileExists@CFileOperation@@$$FSA_NABV?$CStringT@_WV?$StrTraitATL@_WV?$ChTraitsCRT@_W@ATL@@@ATL@@@ATL@@@Z) referenced in function "[T2M] void __clrcall `dynamic initializer for 'public: static float * tagVARIANT::* ATL::CVarTypeInfo<float *>::pmField''(void)" (__t2m@???__E?pmField@?$CVarTypeInfo@PAM@ATL@@2QQtagVARIANT@@PAMQ3@@@YMXXZ@?A0x22b777aa@@YMXXZ)
2>DeskUpdateManaged.obj : error LNK2028: unresolved token (0A00021C) "public: static bool __cdecl CFileOperation::FileExists(class ATL::CStringT<wchar_t,class ATL::StrTraitATL<wchar_t,class ATL::ChTraitsCRT<wchar_t> > > const &)" (?FileExists@CFileOperation@@$$FSA_NABV?$CStringT@_WV?$StrTraitATL@_WV?$ChTraitsCRT@_W@ATL@@@ATL@@@ATL@@@Z) referenced in function "public: bool __clrcall DeskUpdateManaged::Conversion::FileExist(class System::String ^)" (?FileExist@Conversion@DeskUpdateManaged@@$$FQ$AAM_NP$AAVString@System@@@Z)
2>DeskUpdateManaged.obj : error LNK2019: unresolved external symbol "public: static bool __cdecl CFileOperation::FileExists(class ATL::CStringT<wchar_t,class ATL::StrTraitATL<wchar_t,class ATL::ChTraitsCRT<wchar_t> > > const &)" (?FileExists@CFileOperation@@$$FSA_NABV?$CStringT@_WV?$StrTraitATL@_WV?$ChTraitsCRT@_W@ATL@@@ATL@@@ATL@@@Z) referenced in function "public: bool __clrcall DeskUpdateManaged::Conversion::FileExist(class System::String ^)" (?FileExist@Conversion@DeskUpdateManaged@@$$FQ$AAM_NP$AAVString@System@@@Z)
2>NFileOperation.obj : error LNK2001: unresolved external symbol "public: static bool __cdecl CFileOperation::FileExists(class ATL::CStringT<wchar_t,class ATL::StrTraitATL<wchar_t,class ATL::ChTraitsCRT<wchar_t> > > const &)" (?FileExists@CFileOperation@@$$FSA_NABV?$CStringT@_WV?$StrTraitATL@_WV?$ChTraitsCRT@_W@ATL@@@ATL@@@ATL@@@Z)
2>C:\Users\ABGZAMANK\Music\DeskUpdate\Dev\Source\Solution\Debug\DeskUpdateManaged.dll : fatal error LNK1120: 3 unresolved externals

在 C++/CLI 中对本地 C++ DLL 的函数调用:

bool NFileOperation::FileExists(CAtlString sPathName)
{
    return CFileOperation::FileExists(sPathName);
}

对于我想要实现的目标,是否有更充分的方法? 非常感谢与此问题相关的任何建议。

【问题讨论】:

  • 您是否将本机库设置为 CLI 链接器的输入?
  • 我不确定,我该如何检查我是否有?
  • 右键单击您的项目 -> 属性 -> 链接器。在一般站点上指定附加库目录。在输入站点上,将输出 .lib 指定为附加依赖项。
  • 我已经添加了引用,但问题仍然存在。

标签: c++ .net visual-studio mfc c++-cli


【解决方案1】:

您的本机 C++ 代码似乎正在使用 ATL/MFC。假设你是在VS下编译,进入C++/CLI项目的项目属性->配置属性->常规,选择“使用MFC”和“使用ATL”为静态库或共享dll(取决于你的项目类型)。此标志会将必要的包含路径、链接路径和库添加到您的项目中,以便在您的项目中使用 ATL/MFC 类型。此外,请确保将 C++/CLI 项目与本机 C++ 项目链接,以避免项目中的链接错误(很容易在 General 和 Input 的项目属性的 Linker 部分中找到)。

【讨论】:

  • 我尝试使用 ATL 和 MFC 将项目编译为静态库和动态库,但它们无法在调试模式下编译。然而,该项目确实在发布模式下编译,并且仍然出现链接器错误。请您通过将 C++/CLI 项目与本机 C++ 链接来解释您的意思(我不确定我是否做得正确)?
  • @Khalid:您的本机 C++ 代码和托管包装器是同一个项目的一部分吗?这是非常不可取的。您应该将它们分开放置在不同的程序集(项目)中。见stackoverflow.com/questions/4642702/…
  • @Khalid:另外,您的原生 C++ dll 是否创建了导出库?它绝对应该,否则您将需要使用 LoadLibraryEx 调用方法...如果是这样,您需要链接到您的 C++/CLI 包装器项目中的导入库,就像 Nico 在您的问题中评论的那样。
  • 抱歉回复晚了。本机 C++ dll 和 C++/CLI 包装器位于同一解决方案中的不同项目中。库已导出,我已经引用了 Nico 提到的文件夹和库。
猜你喜欢
  • 2011-09-15
  • 2017-05-09
  • 1970-01-01
  • 2015-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-07
  • 1970-01-01
相关资源
最近更新 更多