简体   繁体   English

将类/结构从一个 dll 导入另一个 dll 时出错(c++,lnk2019)

[英]Error when importing a class/structures from one dll to another dll (c++, lnk2019)

I have 2 DLLs.我有 2 个 DLL。 The first describes the LinkedList data structure.第一个描述了 LinkedList 数据结构。 In the second, this structure is used.第二,使用这种结构。

LinkedList.h (from first.ddl): LinkedList.h (来自 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): LinkedList.cpp (来自 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): LeetCodeTasks.h (来自 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): LeetCodeTasks.cpp (来自 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:但是在组装2号和DLL时,出现如下错误:

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.我首先尝试编写 struct 而不是 class - 它没有帮助。

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对于第二个.dll 在项目设置中指定为包含文件的附加目录 - 从第一个.dll 到头文件的路径

Does anyone know how to solve this problem?有谁知道如何解决这个问题?

1.Follow drescherjm 's comment. 1.关注drescherjm的评论。

2.I noticed another problem: 2.我注意到另一个问题:

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.你的项目一没有生成lib文件,所以你无法在项目二的属性的Linker->input中添加LinkedList.lib。 But you still got the link errors.但是您仍然遇到链接错误。

I deduce that you did not actually add LinkedList.lib file in Linker->input->Additional Dependencies .我推断您实际上并没有在Linker->input->Additional Dependencies添加LinkedList.lib文件。 You need to fix it.你需要修复它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM