导入dll时c ++ lnk 2028,lnk 2020,lnk 2019和lnk 2001

时间:2013-01-11 14:50:49

标签: c++ .net dllimport unmanaged

前几天我问了一个类似的问题,这个问题帮助我用__declspec()找到了正确的方向,但我又被卡住了。我会尽可能清楚。希望有人可以告诉我我做错了什么。这可能是一件小而简单的事情。

项目信息:

jc:
    project type:                    Class Library
    configuration type:              Dynamic Library (.dll)
    common runtime language support: /clr
    references:                      System
                                     System.Data
                                     System.Drawing
                                     System.Windows.Forms
                                     System.Xml
test (start up project):
    project type:                    CLR Empty project
    configuration type:              Application (.exe)
    common runtime language support: /clr
    references:                      jc
    project dependencies:            jc

jc文件:

注意:我保留了resource.h,stdafx.h / cpp和AssemblyInfo.cpp不变。

jc.h

#ifndef JC_H
#define JC_H
#include "def.h"
#include "file.h"
#endif

def.h

#ifndef JC_DEF_H
#define JC_DEF_H
#ifdef JC_API_DEFINITIONS
#   define JC_API __declspec(dllexport)
#else
#   define JC_API __declspec(dllimport)
#endif
#endif

file.h

#ifndef JC_FILE_H
#define JC_FILE_H
#include "def.h"
extern JC_API int test_var;
JC_API void test_func(void);
class JC_API test_class
{
public:
    __thiscall test_class();//I inserted __thiscall for compatibility with other compilers. Is this necessary and should I use it for the definition as well?
};
#endif

file.cpp

#include "StdAfx.h"
#define JC_API_DEFINITIONS
#include "file.h"
JC_API int test_var = 10;
JC_API void test_func(void){}
__thiscall test_class::test_class(){}

测试文件:

TEST.CPP

#include "../jc/jc.h"
int main(void)
{
    int x = test_var;
    test_func();
    test_class obj;
    return 0;
}

这些是我得到的错误:

1>main.obj : error LNK2028: unresolved token (0A000009) "public: __thiscall test_class::test_class(void)" (??0test_class@@$$FQAE@XZ) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>main.obj : error LNK2028: unresolved token (0A00000A) "void __cdecl test_func(void)" (?test_func@@$$FYAXXZ) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>main.obj : error LNK2020: unresolved token (0A00000B) "__declspec(dllimport) int test_var" (__imp_?test_var@@3HA)
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall test_class::test_class(void)" (??0test_class@@$$FQAE@XZ) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>main.obj : error LNK2019: unresolved external symbol "void __cdecl test_func(void)" (?test_func@@$$FYAXXZ) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>main.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) int test_var" (__imp_?test_var@@3HA)

我已经坚持这个问题几天了,我希望有人可以帮助我。

谢谢!

1 个答案:

答案 0 :(得分:1)

找到答案。错误不在代码中。我不知道我必须在“dll”属性的“链接器/输入”选项中将“.lib”文件包含到“其他依赖项”中。该视频明确表示:“http://www.youtube.com/watch?v=yEqRyQhhto8。”