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

隐式链接DLL后无法调用Shape类,报错无法解析符号求解决

Fixing the "Unresolved Symbol Shape" Error When Calling a DLL Class

Let's break down why you're running into this unresolved symbol issue with your Shape class from Shape.dll—the problem lies in how you've structured your DLL export/import macros in Shape.h.

The Root Problem

Right now, your Shape.h only applies __declspec(dllexport) to the Shape class when SHAPE_EXPORTS is defined (which you've done in your DLL project). But in your ShapeTester project, you need the opposite: you want the compiler to know this class is being imported from an external DLL, not exported. Without that marker, the linker can't find the symbol references in Shape.lib.

Corrected Shape.h Code

Update your header file to handle both export (for the DLL project) and import (for the tester project) scenarios:

// Shape.h
#ifndef SHAPE_H
#define SHAPE_H

// Proper export/import macro setup
#ifdef SHAPE_EXPORTS
#define SHAPE_API __declspec(dllexport)
#else
#define SHAPE_API __declspec(dllimport)
#endif

class SHAPE_API Shape {
public:
    Shape();
    Shape(int sides, int sideLength, int apothem);
    ~Shape();
    int Perimeter();
    double Area();
private:
    int sides;
    int sideLength;
    int apothem;
};

#endif // SHAPE_H

What This Does

  • In your Shape.dll project: Since you've already added SHAPE_EXPORTS to the preprocessor definitions, SHAPE_API becomes __declspec(dllexport), which tells the compiler to export the Shape class to the DLL and its accompanying .lib file.
  • In your ShapeTester project: Do NOT add SHAPE_EXPORTS to its preprocessor definitions. This makes SHAPE_API resolve to __declspec(dllimport), letting the compiler know it needs to pull the Shape class from the external Shape.dll via Shape.lib.

Final Checks to Ensure Success

  • Double-check that SHAPE_EXPORTS is not present in the preprocessor definitions of your ShapeTester project.
  • Rebuild your Shape.dll project to generate fresh .lib and .dll files.
  • Rebuild your ShapeTester project—this should resolve the "unresolved symbol Shape" error.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 07:13:55