1
前幾天我問了一個類似的問題,它幫助我用__declspec()找正確的方向,但是我又被卡住了。我會盡可能清楚。希望有人能告訴我我做錯了什麼。這可能是小而簡單的事情。C++ lnk 2028,lnk 2020,lnk 2019 and lnk 2001當導入dll時
項目信息:
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)" ([email protected]@[email protected]) referenced in function "int __cdecl main(void)" ([email protected]@$$HYAHXZ)
1>main.obj : error LNK2028: unresolved token (0A00000A) "void __cdecl test_func(void)" ([email protected]@$$FYAXXZ) referenced in function "int __cdecl main(void)" ([email protected]@$$HYAHXZ)
1>main.obj : error LNK2020: unresolved token (0A00000B) "__declspec(dllimport) int test_var" ([email protected]@3HA)
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall test_class::test_class(void)" ([email protected]@[email protected]) referenced in function "int __cdecl main(void)" ([email protected]@$$HYAHXZ)
1>main.obj : error LNK2019: unresolved external symbol "void __cdecl test_func(void)" ([email protected]@$$FYAXXZ) referenced in function "int __cdecl main(void)" ([email protected]@$$HYAHXZ)
1>main.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) int test_var" ([email protected]@3HA)
我已經遇到這個問題了幾天,我希望可以有人幫幫我。
謝謝!