隐式链接DLL后无法调用Shape类,报错无法解析符号求解决
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_EXPORTSto the preprocessor definitions,SHAPE_APIbecomes__declspec(dllexport), which tells the compiler to export theShapeclass to the DLL and its accompanying.libfile. - In your ShapeTester project: Do NOT add
SHAPE_EXPORTSto its preprocessor definitions. This makesSHAPE_APIresolve to__declspec(dllimport), letting the compiler know it needs to pull theShapeclass from the externalShape.dllviaShape.lib.
Final Checks to Ensure Success
- Double-check that
SHAPE_EXPORTSis not present in the preprocessor definitions of your ShapeTester project. - Rebuild your Shape.dll project to generate fresh
.liband.dllfiles. - Rebuild your ShapeTester project—this should resolve the "unresolved symbol Shape" error.
内容的提问来源于stack exchange,提问作者Alvaromon

