Home > Software design >  Error when importing a class/structures from one dll to another dll (c , lnk2019)
Error when importing a class/structures from one dll to another dll (c , lnk2019)

Time:09-17

I have 2 DLLs. The first describes the LinkedList data structure. In the second, this structure is used.

LinkedList.h (from first .ddl):

#pragma once

#ifdef DS_EXPORTS
#define DS_LL_API __declspec(dllexport)
#else
#define DS_LL_API __declspec(dllimport)
#endif

#include "pch.h"
#include <iostream>

using namespace std;

class ListNode
{
public:
 
   int val;
 
   ListNode * next;
 
   ListNode();
 
   ListNode(int x);
 
   ListNode(int x, ListNode * next);
 
   void toString(const char * start, const char * sep, const char * end);
};

LinkedList.cpp (from first .dll):

#include "pch.h"
#include "LinkedList.h"


ListNode::ListNode() : val(0), next(nullptr) { }

ListNode::ListNode(int x) : val(x), next(nullptr) { }

ListNode::ListNode(int x, ListNode * next) : val(x), next(next) { }

void ListNode::toString(const char * start, const char * sep, const char * end)
{
    ListNode * ln = this;

    cout << start << ln->val;
    while(ln->next != nullptr)
    {
        cout << sep << ln->next->val;
        ln = ln->next;
    }
    cout << end << endl;
}

LeetCodeTasks.h (from second .dll):

#pragma once

#ifdef LCT_EXPORTS
#define LCT_API __declspec(dllexport)
#else
#define LCT_API __declspec(dllimport)
#endif

#include "pch.h"
#include <vector>
#include <unordered_map> 
//#include "../DataStructures/LinkedList.h"
#include "LinkedList.h"


using namespace std;


extern LCT_API ListNode * task2_addTwoNumbers(ListNode * l1, ListNode * l2);

LeetCodeTasks.cpp (from second .dll):

#include "pch.h"
#include "LeetCodeTasks.h"


#pragma region Task 2
ListNode * task2_addTwoNumbers(ListNode * l1, ListNode * l2)
{
    ListNode * result = new ListNode();
    int number = 0;

    if(l1 != nullptr)
    {
        number  = l1->val;
    }

    if(l2 != nullptr)
    {
        number  = l2->val;
    }

    result->val  = number % 10;

    if(l1 != nullptr && l1->next != nullptr && l2 != nullptr && l2->next != nullptr)
    {
        l1->next->val  = number / 10;
        result->next = task2_addTwoNumbers(l1->next, l2->next);
    }
    else if(l1 != nullptr && l1->next != nullptr)
    {
        l1->next->val  = number / 10;
        result->next = task2_addTwoNumbers(l1->next, nullptr);
    }
    else if(l2 != nullptr && l2->next != nullptr)
    {
        l2->next->val  = number / 10;
        result->next = task2_addTwoNumbers(nullptr, l2->next);
    }
    else if(number / 10 > 0)
    {
        result->next = new ListNode(number / 10);
    }

    return result;
}
#pragma endregion

But when assembling the 2nd and DLL, the following errors appear:

LNK2019 reference to the unresolved external character "public: __cdecl ListNode::ListNode(void)" (??0 ListNode@@QUEUE@XZ) in the function "class ListNode * __cdecl task2_addTwoNumbers(class ListNode *,class ListNode *)" (?task2_addTwoNumbers@@YAPEAVListNode@@PEAV1@0@Z).


LNK2019 reference to the unresolved external character "public: __cdecl ListNode::ListNode(int)" (??0ListNode@@QEAA@H@Z) in the function "class ListNode * __cdecl task2_addTwoNumbers(class ListNode *,class ListNode *)" (?task2_addTwoNumbers@@YAPEAVListNode@@PEAV1@0@Z).

I tried first to write struct instead of class - it didn't help.

For the second .dll in the project settings specified as additional directories of included files - the path to the headers from the first one .dll

Does anyone know how to solve this problem?

CodePudding user response:

1.Follow drescherjm 's comment.

2.I noticed another problem:

Your project one did not generate lib file, so you have no way to add LinkedList.lib in Linker->input of project two's properties. But you still got the link errors.

I deduce that you did not actually add LinkedList.lib file in Linker->input->Additional Dependencies. You need to fix it.

  • Related