【发布时间】:2012-02-11 04:10:31
【问题描述】:
我创建了一个 DLL(比如 CORE.DLL),我有如下声明的类/函数:
#ifdef RINZOCORE_SHARED
#define RINZO_LIB __declspec(dllexport)
#else
#define RINZO_LIB __declspec(dllimport)
#endif
我已经用“dllexport”宏定义了很多内联函数,
class RINZO_LIB CVector
{
public:
CVector();//!< default constructor
..
real& x();//!< accessor for the x component (can be used as l-value too)
real& y();//!< accessor for the y component (can be used as l-value too)
real& z();//!< accessor for the z component (can be used as l-value too)
CVector& operator=(const CVector& other);//!< the assignment
CVector& operator+=(const CVector& other);//!< the sum & assign
CVector& operator-=(const CVector& other);//!< the subtract & assign
CVector& operator*=(const real& fact);//!< the short multiply by a scalar factor & assign
CVector& operator/=(const real& fact);//!< the short divide by a scalar factor & assign
..
}
RINZO_LIB inline CVector& CVector::operator=(const CVector& other)
{
//check for 'a=a' case
if (this==&other) return *this;
vec[0]=other.vec[0];
vec[1]=other.vec[1];
vec[2]=other.vec[2];
return *this;
}
RINZO_LIB inline CVector& CVector::operator+=(const CVector& other)
{
vec[0]+=other.vec[0];
vec[1]+=other.vec[1];
vec[2]+=other.vec[2];
return *this;
}
RINZO_LIB inline CVector& CVector::operator-=(const CVector& other)
{
vec[0]-=other.vec[0];
vec[1]-=other.vec[1];
vec[2]-=other.vec[2];
return *this;
}
RINZO_LIB inline CVector& CVector::operator*=(const real& fact)
{
vec[0]*=fact;
vec[1]*=fact;
vec[2]*=fact;
return *this;
}
RINZO_LIB inline CVector& CVector::operator/=(const real& fact)
{
assert(fabs(fact) >= epsilon);
vec[0]/=fact;
vec[1]/=fact;
vec[2]/=fact;
return *this;
}
但是当我使用这个 DLL(导入)编译另一个 DLL(比如 PluginA.DLL)时,它会出现以下编译错误:
Info: resolving std::cout by linking to __imp___ZSt4cout (auto-import)
CMakeFiles\ContourViewer.dir/objects.a(RzStateDoAnimation.cpp.obj):C:/svn/osaka3d/trunk/osaka3d/rinzo-platform/src/dlplugins/contourviewer/statemachine/RzStateDoAnimation.cpp:79: undefined reference to `operator!=(quaternion const&, quaternion const&)'
Info: resolving vtable for __cxxabiv1::__vmi_class_type_info by linking to __imp___ZTVN10__cxxabiv121__vmi_class_type_infoE (auto-import)
CMakeFiles\ContourViewer.dir/objects.a(RzStateDoAnimation.cpp.obj):C:/svn/osaka3d/trunk/osaka3d/rinzo-platform/src/dlplugins/contourviewer/statemachine/RzStateDoAnimation.cpp:146: undefined reference to `operator==(quaternion const&, quaternion const&)'
CMakeFiles\ContourViewer.dir/objects.a(BallController.cpp.obj):C:/svn/osaka3d/trunk/osaka3d/rinzo-platform/src/dlplugins/contourviewer/trackball/BallController.cpp:159: undefined reference to `operator*(CVector const&, CVector const&)'
CMakeFiles\ContourViewer.dir/objects.a(BallController.cpp.obj):C:/svn/osaka3d/trunk/osaka3d/rinzo-platform/src/dlplugins/contourviewer/trackball/BallController.cpp:165: undefined reference to `operator^(CVector const&, CVector const&)'
CMakeFiles\ContourViewer.dir/objects.a(BallController.cpp.obj):C:/svn/osaka3d/trunk/osaka3d/rinzo-platform/src/dlplugins/contourviewer/trackball/BallController.cpp:168: undefined reference to `operator-(CVector const&, CVector const&)'
CMakeFiles\ContourViewer.dir/objects.a(BallController.cpp.obj):C:/svn/osaka3d/trunk/osaka3d/rinzo-platform/src/dlplugins/contourviewer/trackball/BallController.cpp:292: undefined reference to `operator*(CVector const&, CVector const&)'
CMakeFiles\ContourViewer.dir/objects.a(BallController.cpp.obj):C:/svn/osaka3d/trunk/osaka3d/rinzo-platform/src/dlplugins/contourviewer/trackball/BallController.cpp:292: undefined reference to `operator*(CVector const&, float const&)'
CMakeFiles\ContourViewer.dir/objects.a(BallController.cpp.obj):C:/svn/osaka3d/trunk/osaka3d/rinzo-platform/src/dlplugins/contourviewer/trackball/BallController.cpp:292: undefined reference to `operator-(CVector const&, CVector const&)'
关于如何通过 dllexport/dllimport 使用内联函数的任何提示?
【问题讨论】:
-
你想达到什么目的?显而易见的解决方案有什么问题 - 删除
inline? -
@DavidSchwartz 实际上这是代码的一部分,我定义了很多类和很多内联函数。
-
@AshikaUmangaUmagiliya 如果我错了,请纠正我,但
inline函数不作为单独的代码段存在。事实上,你甚至不能在头文件之外定义它们——它们必须与类声明一起出现并包含在它们所使用的每个编译单元中。因此,对于你的PluginA.cpp编译,你必须为Core.hpp提供类声明应该包含所有inline函数的定义。 -
将 classes 与
__declspec(dllexport)一起使用是一个坏主意。由于整个定义都存在于客户端正在使用的头文件中,因此无需导入任何内容。
标签: c++ inline dllimport dllexport