1

前几天我问了一个类似的问题,这帮助我用 __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

定义.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

文件.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

文件.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(){}

测试文件:

测试.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)

我被这个问题困扰了几天,希望有人能帮助我。

谢谢!

4

1 回答 1

1

Found the answer. The mistake wasn't in the code. I didn't know I had to include the ".lib" file to "additional dependencies" in "linker/input" options of the "dll" properties. This video made it clear: "http://www.youtube.com/watch?v=yEqRyQhhto8."

于 2013-01-13T20:47:03.657 回答