Visual C++/CLI - CLR 类 - LNK2020 错误 - 如何修复?
我有一个项目,必须连接到基于 C 的库,但我无法使用 DLLImport 从 C# 获取函数。我必须使用基于 C++/CLI 的项目来显示这些函数以供使用。 (更多关于这个故事,但并不重要)。
我十多年前学习了 C++,所以如果这看起来有点幼稚,请原谅我。
去年我购买了几本关于 C++/CLI 实现的书籍,并且对其中涉及的内容有了一些了解 - 我只是为了这个项目而深入研究了这些书籍。 (我当程序员已经很长时间了)。
我想我最好开始一个小项目示例项目来熟悉将涉及的内容,以确保我可以编译等。我使用 Visual Studio 2008 SP1 开始了一个项目;视觉C++>清除率>班级图书馆。在项目中 - 我想使用 DLL 中的托管和本机。因此使用 /clr 开关。
我在真实代码中使用了其他名称;但这已经非常非常接近了。 (此时没有其他文件或函数)
标题:
//myWrapper.h
#pragma once
using namespace System;
namespace myWrapper {
public ref class myWrappedService {
myWrappedService();
bool Connected(String ^user,String ^password,String ^domain);
}
};
并且实现有这个。
//myWrapper.cpp
//This is the main DLL file
#include "stdafx.h"
#include "myWrapper.h"
using namespace System;
public ref class myWrappedService
{
public:
myWrappedService() {}
bool Connected(String ^user,String ^password,String ^domain)
{
//just a simple result to start - no real functionality
bool result = false;
return result;
}
};
这就是代码 - 编译但出现链接器错误。
错误 LNK2020:无法解析的令牌 (06000002) myWrapper.myWrappedService::已连接
致命错误 LNK1120:1 个未解决的外部错误。
这看起来非常简单——而且我可能对 C# 方法思考得太多。我希望这很简单 - 但我不熟悉 CLI 方法中应该看到的内容。 (我花了几个小时寻找答案,最后觉得我需要发布一个可能得到答案的问题)。
I have a project, where I must connect to a library that is C based, and I was not able to get at the functions from C# using the DLLImport. I have to use a project that is C++/CLI based to surface these functions for use. (more to that story but not important).
I studied C++ over 10 years ago, so forgive me if this seems like it is naive.
I purchased a few books on the C++/CLI implementation last year, and have some grasp of what is involved - I only dug into those books for this project. (I have been a programmer for a long time).
I thought I had better start a small project example project to familiarize myself with what will be involved, to make sure I could compile, etc. I started a project using Visual Studio 2008 SP1; Visual C++ > CLR > Class Library. In the project - I want to use both managed and native from the DLL. So the /clr switch is used.
I have used other names in the real code; but this is very, very close. (there are no other files, or functions at this point)
Header:
//myWrapper.h
#pragma once
using namespace System;
namespace myWrapper {
public ref class myWrappedService {
myWrappedService();
bool Connected(String ^user,String ^password,String ^domain);
}
};
And the implementation has this.
//myWrapper.cpp
//This is the main DLL file
#include "stdafx.h"
#include "myWrapper.h"
using namespace System;
public ref class myWrappedService
{
public:
myWrappedService() {}
bool Connected(String ^user,String ^password,String ^domain)
{
//just a simple result to start - no real functionality
bool result = false;
return result;
}
};
That is the code - that compiles but gets linker errors.
error LNK2020: unresolved token (06000002)
myWrapper.myWrappedService::Connectedfatal error LNK1120: 1 unresolved external.
This looked dead easy - and I might be thinking to much from a C# approach. I expect this to be something simple - but I am not familiar with what I should be seeing in the CLI approach. (I spent a few hours looking for answers and finally feel I need to post a question where it might get answered).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从那里开始就出了问题。您的 .h 文件在该命名空间内声明了一个类,其名称为 myWrapper::myWrappedService。您的 .cpp 文件声明了另一个不在命名空间中的类,其名称为 ::myWrappedService。不同的名称,编译器不会抱怨看到同一个类定义了两次。第一个类没有 Connected 方法的实现,这就是链接器抱怨的原因。
Sad_man 的片段也存在同样的缺陷。第一个代码片段定义了 ::myWrappedService::Connected() 方法。命名空间错误。第二个片段与您的片段具有相同的缺陷。
当您在托管代码中编写类库时,您不需要 .h 文件。程序集中的元数据起着相同的作用,它包含程序集中类型的声明。您的项目中没有其他 .cpp 文件需要类的声明,不需要 .h。所以只需将所有内容写入 .cpp 文件中即可:
It went wrong from there. Your .h file declares a class inside that namespace, its name is myWrapper::myWrappedService. Your .cpp file declares another class that is not in a namespace, its name is ::myWrappedService. Different names, the compiler won't complain about seeing the same class defined twice. The first class doesn't have an implementation of the Connected method, that's why the linker complained.
sad_man's snippets suffer the same flaw. The first snippet defines the ::myWrappedService::Connected() method. Wrong namespace. The second snippet has the same flaw as yours.
When you write a class library in managed code then you don't need a .h file. Metadata in an assembly plays the same role, it contains the declarations of the types in the assembly. There is no other .cpp file in your project that needs the declaration of the class, no need for the .h. So just write everything in .cpp file:
你的cpp文件全错了。有关 cpp 头文件的使用,请参阅此处 和 此处。
C++/Cli 具有相同的头文件和 cpp 文件概念。实际上你的cpp文件就是头文件应该有的样子。
您也可以像这样:
或者像这样(您的案例的一个标头):
编辑:我忘记了名称空间。但我认为你应该了解什么是头文件和cpp 文件。
Your cpp file is all wrong. For usage of cpp an header files look at here and here.
C++/Cli has the same concept of header and cpp files. Actually your cpp file is what a header file is supposed to be.
Also you can go like this :
Or like this (One header for your case):
EDIT : I have forgotten about the namespace. But I think you should learn what header and cpp files are.