c++ lnk 2028, lnk 2020, lnk 2019 and lnk 2001 when

2019-08-09 09:41发布

问题:

A few day ago I asked a similar question, which helped me look in the right direction with __declspec(), but I got stuck again. I'll be as clear as possible. Hopefully someone can tell me what I'm doing wrong. It is probably something small and simple.

Projects info:

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 files:

NOTE: I left resource.h, stdafx.h/cpp and AssemblyInfo.cpp unchanged.

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 files:

test.cpp

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

these are the error I get:

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)

I've been stuck with this problem for a few days, I hope someone can help me.

Thank you!

回答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."