visual-studio C++/CLI:链接器为 win32 函数提供“未解析的令牌”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/739952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++/CLI: linker gives "unresolved token" for win32 function
提问by Binary Worrier
Folks,
各位,
I just created my first C++/CLI project (Visual Studio 2008), it's a Library to allow my C# app access an point of sale tally printer.
我刚刚创建了我的第一个 C++/CLI 项目(Visual Studio 2008),它是一个允许我的 C# 应用程序访问销售点理货打印机的库。
My library builds well and trivial functions work when called from a C# exe.
当从 C# exe 调用时,我的库构建良好且琐碎的函数工作。
However as soon as I include a WinGDI call (DeleteObjectin this case), the linker complains with “unresolved token” errors.
但是,只要我包含一个 WinGDI 调用(在本例中为DeleteObject),链接器就会抱怨“未解析的令牌”错误。
Error 2 error LNK2028: unresolved token (0A000088) "extern "C" int __stdcall DeleteObject(void *)" (?DeleteObject@@$$J14YGHPAX@Z) referenced in function "private: __clrcall ReceiptPrinter::Epson::~Epson(void)" (??1Epson@ReceiptPrinter@@$$FA$AAM@XZ) ReceiptPrinter.obj ReceiptPrinter
错误 2 error LNK2028: unresolved token (0A000088) "extern "C" int __stdcall DeleteObject(void *)" (?DeleteObject@@$$J14YGHPAX@Z) 引用在函数“private: __clrcall ReceiptPrinter::Epson::~Epson( void)" (??1Epson@ReceiptPrinter@@$$FA$AAM@XZ) ReceiptPrinter.obj ReceiptPrinter
I haven't done any serious C++ in the last 4 years, and I have precious little experience of MS C++ compilers, as such I don't know what I'm looking for in the linker settings.
在过去的 4 年里我没有做过任何严肃的 C++,而且我对 MS C++ 编译器的经验很少,因此我不知道我在链接器设置中寻找什么。
Any help will be greatfully received.
任何帮助都将受到极大的欢迎。
Thanks
谢谢
回答by dirkgently
Additional dependancies was "NoInherit", when I looked "under" the setting, there was a list of libs, gdi32.lib was in the list. I checked "Inherit from parent project" and it now works. Dirk, if you add all that as an answer I'll select it and give you the rep.
附加依赖项是“NoInherit”,当我查看设置的“下方”时,有一个库列表,gdi32.lib 在列表中。我检查了“从父项目继承”,现在可以使用了。德克,如果您将所有这些添加为答案,我会选择它并为您提供代表。
Check if Gdi32.lib is there in the linker commandline(Properties > Linker > CommandLine).
检查链接器命令行中是否存在 Gdi32.lib(属性 > 链接器 > 命令行)。
(There you go -- you have successfully appealed to the my selfish, rep seeking part of soul ;) )
(你去 - 你已经成功地吸引了我自私的代表,寻求灵魂的一部分;))
回答by Idan K
You should link your dll with Gdi32.lib.
您应该将您的 dll 与 Gdi32.lib 链接。
You can either do it with a #pragma comment(lib, "gdi32.lib") or in your project's settings under Linker.
您可以使用 #pragma comment(lib, "gdi32.lib") 或在链接器下的项目设置中执行此操作。
回答by Graham Asher
The clue is in the __clrcall modifier on the function declarations. Your Windows Forms app uses pure Common Language Runtime code and calling conventions by default. Your external library, which you're linking to, does not. You need to change the Common Language Runtime support setting in your Project Defaults area of the project properties from /clr:pure to /clr. That worked for me.
线索在函数声明的 __clrcall 修饰符中。默认情况下,您的 Windows 窗体应用程序使用纯公共语言运行时代码和调用约定。您链接到的外部库没有。您需要将项目属性的“项目默认值”区域中的公共语言运行时支持设置从 /clr:pure 更改为 /clr。那对我有用。

