LNK2028和LNK2019在使用没有内联代码的dll时出错

LNK2028 and LNK2019 error when using dll with no inline code

本文关键字:代码 dll 出错 LNK2019 LNK2028      更新时间:2023-10-16

这是我第一次使用。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)指令。

本文档展示了如何在头文件中标记类和全局函数,以便它们可以用于导出和导入。