LNK2028和LNK2019在使用没有内联代码的dll时出错
LNK2028 and LNK2019 error when using dll with no inline code
这是我第一次使用。net,当使用多个项目时,它给我带来了困难。我做错了什么?我是这样做的:
1创建一个新项目(和解决方案)。一个叫做"Lib"的"类库"。
2添加文件"Comp1.h"到"Header Files",代码如下:
#ifndef COMP1_H
#define COMP1_H
class comp1
{
public:
comp1(void);
~comp1(void);
}
#endif
3添加文件"Comp1.cpp"到"Source Files",代码如下:
#include "stdafx.h"
#include "Comp1.h"
comp1::comp1(void){}
comp1::~comp1(void){}
我用:
覆盖自动创建的"Lib.h"文件的代码#ifndef LIB_H
#define LIB_H
#include "Comp1.h"
#endif
5添加一个名为"Test"的"CLR空项目",并将其设置为启动项目。
6将Test .cpp文件添加到Test项目的Source Files中,代码如下:
#include "../Lib/Lib.h"
#using "../Debug/Lib.dll"//Is this line mandatory?
int main()
{
comp1 component;
return 0;
}
7在"Test"属性中添加"Lib"作为引用。
8确保在"Project Dependencies"中,"Test"依赖于"Lib"。
9将它们都编译为/clr
这是我得到的:
1>test.obj : error LNK2028: unresolved token (0A000009) "public: __thiscall comp1::comp1(void)" (??0comp1@@$$FQAE@XZ) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>test.obj : error LNK2028: unresolved token (0A00000A) "public: __thiscall comp1::~comp1(void)" (??1comp1@@$$FQAE@XZ) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>test.obj : error LNK2019: unresolved external symbol "public: __thiscall comp1::~comp1(void)" (??1comp1@@$$FQAE@XZ) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>test.obj : error LNK2019: unresolved external symbol "public: __thiscall comp1::comp1(void)" (??0comp1@@$$FQAE@XZ) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
如果我创建内联函数,这个问题就不会发生。
在论坛上你可以找到关于包含文件错误的答案,但我很确定我的代码都是正确的。
谢谢。
当您创建DLL时,您必须显式标记要使用__declspec(dllexport)
指令导出的函数和类,从而使它们可用于客户端导入。当你导入这个类时,你必须使用__declspec(dllimport)
指令。
本文档展示了如何在头文件中标记类和全局函数,以便它们可以用于导出和导入。
相关文章:
- 是否可以在Linux上使用.Net Core 3.1创建C++/CLI代码的C#DLL
- 如何在 c# 代码中通过引用调用时从 c++ dll 更新数组值?
- 代码在 msvcrt .dll函数上设置断点失败
- 如何从支持linux/mac操作系统的第三方代码为我的c#项目创建dll
- 由于找不到cpprest_2_10.dll,代码执行无法继续
- 可以从本机代码(如C++/Delphi)中使用.netCore DLL
- 外部 "C" Visual Studio 2015 中的显式类型错误,DLL 测试代码
- CPP DLL 自定义操作代码在安装过程中未执行
- 在注入的 Dll 的 DllMain 中运行代码导致注入超时
- 通过 dll 注入在主线程中执行代码
- C++端口监视器 (mfilemon.dll) RDP 虚拟通道代码未执行
- C++ dll 在 C# 代码中抛出 EntryPointNotFoundException
- jvm.dll 上的加载库失败,错误代码为 183
- 将代码移动到函数中时,DLL "myApp.exe"中找不到序号错误0xC0000138
- 尝试将 c++ dll 导入 c 尖锐代码
- 在C#代码中调用C DLL
- .NET CORE 2 DLL引用带有C 代码运行时错误
- 附加到 C++/CLI dll 的性能探查器无法访问本机C++代码
- C - 将代码注入运行过程(使用DLL)
- Excel 2016 VSTO 加载项与本机代码 DLL