Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用没有内联代码的dll时LNK2028和LNK2019错误_C++_.net_Dllimport_Unmanaged_Lnk2019 - Fatal编程技术网

C++ 使用没有内联代码的dll时LNK2028和LNK2019错误

C++ 使用没有内联代码的dll时LNK2028和LNK2019错误,c++,.net,dllimport,unmanaged,lnk2019,C++,.net,Dllimport,Unmanaged,Lnk2019,这是我第一次使用.NET,当我使用多个项目时,它会给我带来困难。我做错了什么?这就是我所做的: 1创建一个新项目(和解决方案)。一个叫做“Lib”的“类库” 2使用以下代码将文件“Comp1.h”添加到“头文件”中: #ifndef COMP1_H #define COMP1_H class comp1 { public: comp1(void); ~comp1(void); } #endif #include "stdafx.h" #include "Comp1.h" com

这是我第一次使用.NET,当我使用多个项目时,它会给我带来困难。我做错了什么?这就是我所做的:

1创建一个新项目(和解决方案)。一个叫做“Lib”的“类库”

2使用以下代码将文件“Comp1.h”添加到“头文件”中:

#ifndef COMP1_H
#define COMP1_H
class comp1
{
public:
    comp1(void);
    ~comp1(void);
}
#endif
#include "stdafx.h"
#include "Comp1.h"
comp1::comp1(void){}
comp1::~comp1(void){}
#include "../Lib/Lib.h"
#using "../Debug/Lib.dll"//Is this line mandatory?
int main()
{
comp1 component;
return 0;
}
3使用以下代码将文件“Comp1.cpp”添加到“源文件”中:

#ifndef COMP1_H
#define COMP1_H
class comp1
{
public:
    comp1(void);
    ~comp1(void);
}
#endif
#include "stdafx.h"
#include "Comp1.h"
comp1::comp1(void){}
comp1::~comp1(void){}
#include "../Lib/Lib.h"
#using "../Debug/Lib.dll"//Is this line mandatory?
int main()
{
comp1 component;
return 0;
}
4我用以下内容覆盖自动创建的“Lib.h”文件的代码:

#ifndef LIB_H
#define LIB_H
#include "Comp1.h"
#endif
5添加一个名为“测试”的“CLR空项目”,并将其设置为启动项目

6使用以下代码将文件“test.cpp”添加到“test”项目的“源文件”中:

#ifndef COMP1_H
#define COMP1_H
class comp1
{
public:
    comp1(void);
    ~comp1(void);
}
#endif
#include "stdafx.h"
#include "Comp1.h"
comp1::comp1(void){}
comp1::~comp1(void){}
#include "../Lib/Lib.h"
#using "../Debug/Lib.dll"//Is this line mandatory?
int main()
{
comp1 component;
return 0;
}
7在“测试”属性中添加“Lib”作为引用

8在“项目依赖项”中确保“测试”依赖于“库”

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时,必须使用
\uuu declspec(dllexport)
指令明确标记要导出的函数和类,从而使它们可供客户端导入。导入类时,必须使用
\uu declspec(dllimport)
指令


演示如何在标题中标记类和全局函数,以便它们可用于导出和导入。

创建DLL时,必须使用
\uuuu declspec(dllexport)
指令明确标记要导出的函数和类,从而使它们可供客户端导入。导入类时,必须使用
\uu declspec(dllimport)
指令


演示如何在标题中标记类和全局函数,以便它们可以用于导出和导入。

感谢您的快速响应,这确实有效。但是我正在尝试创建一个跨平台的GUI库,因为我不想依赖于我以前使用的API。是否有一种使用函数而不使用y-yDeScript(dLimPurt)的方式来给用户一个更标准的C++感觉?对于其他平台,比Win32/.NET,你可以定义相同的宏到零。参见此处第22至34行,了解使该进近跨平台的方法。您需要插入自己的测试,而不是测试
OGRE\u PLATFORM==OGRE\u PLATFORM\u WIN32
。对于Win32,在导入dll时,恐怕您必须使用
\u declspec(dllimport)
。我不知道如何解决这个问题。谢谢你的快速回复,这确实有效。但是我正在尝试创建一个跨平台的GUI库,因为我不想依赖于我以前使用的API。是否有一种使用函数而不使用y-yDeScript(dLimPurt)的方式来给用户一个更标准的C++感觉?对于其他平台,比Win32/.NET,你可以定义相同的宏到零。参见此处第22至34行,了解使该进近跨平台的方法。您需要插入自己的测试,而不是测试
OGRE\u PLATFORM==OGRE\u PLATFORM\u WIN32
。对于Win32,在导入dll时,恐怕您必须使用
\u declspec(dllimport)
。我不知道该怎么办。