You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++跨DLL导入类/结构体时出现LNK2019错误的解决求助

DLL项目间类成员未解析外部符号问题解决

我有两个DLL项目,第一个定义了LinkedList链表数据结构,第二个项目需要使用该结构,但编译第二个DLL时出现LNK2019未解析外部符号错误。


第一个DLL项目代码

LinkedList.h

#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

#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;
}

第二个DLL项目代码

LeetCodeTasks.h

#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

#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

编译错误信息

LNK2019 reference to the unresolved external character "public: __cdecl ListNode::ListNode(void)" (??0ListNode@@QEAA@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).

我已经尝试将class改为struct但未解决问题,且已在第二个DLL的项目设置中添加了第一个DLL头文件的附加包含目录,请问该如何解决?


解决方法

1. 正确导出ListNode类成员

当前DS_LL_API宏未应用到ListNode类或其成员上,导致第一个DLL未导出构造函数等符号,第二个DLL无法找到。修改LinkedList.h,可以选择两种方式:

  • 直接给类添加导出宏:
    class DS_LL_API ListNode
    {
        // 原有成员不变
    };
    
  • 或者给每个成员函数单独添加导出宏:
    class ListNode
    {
    public:
        int val;
        ListNode * next;
     
        DS_LL_API ListNode();
        DS_LL_API ListNode(int x);
        DS_LL_API ListNode(int x, ListNode * next);
        DS_LL_API void toString(const char * start, const char * sep, const char * end);
    };
    

2. 链接第一个DLL的导入库

第二个项目仅包含头文件不够,必须链接第一个DLL编译生成的导入库文件(后缀为.lib,和DLL同名):

  • 在Visual Studio中,右键第二个项目 → 属性 → 链接器 → 输入 → 附加依赖项,添加该.lib文件的路径。
  • 确保第一个DLL已编译成功,生成了对应的.lib和.dll文件。

3. 确认预定义宏设置

第一个DLL项目需在设置中预定义DS_EXPORTS宏:

  • 右键第一个项目 → 属性 → C/C++ → 预处理器 → 预处理器定义,添加DS_EXPORTS。这样编译第一个DLL时,DS_LL_API会被定义为__declspec(dllexport)导出符号;第二个项目未定义该宏,DS_LL_API自动变为__declspec(dllimport),正确导入符号。

4. 检查头文件路径

确保第二个项目中#include "LinkedList.h"的路径正确,避免引入同名错误头文件。若头文件不在当前目录,确认附加包含目录设置无误。


内容的提问来源于stack exchange,提问作者Aynur19

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 16:31:34