问题描述
我创建一个DLL(例如CORE.DLL),我声明了以下类/函数:
I create a DLL (say CORE.DLL) ,I have classes/functions declared as follows:
#ifdef RINZOCORE_SHARED
#define RINZO_LIB __declspec(dllexport)
#else
#define RINZO_LIB __declspec(dllimport)
#endif
我用 dllexport宏定义了许多内联函数,
I have defined many inline functions with "dllexport" macro ,
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),它会产生以下编译错误:
but when I use this DLL (import) compile another DLL (say PluginA.DLL) it gives following compile errors :
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一起使用的任何技巧?
Any tips on how to use inline functions with dllexport/dllimport ?
推荐答案
注意:由于这是可接受的答案,因此无法删除。请考虑逻辑已删除。欢迎您对此投票。
Note: since this is the accepted answer, it cannot be deleted. Please consider it logically deleted. You are welcome to downvote it.
下面保留了历史记录的原始内容。
Original content preserved below for history.
内联和dllexport / dllimport不要混用。
Inline and dllexport/dllimport don't mix.
您要么
- 内联函数,并针对使用它们的每个源文件分别对其进行编译;或
- 将它们存储在库中,也就是说,仅将它们编译一次(导出),然后将程序的其余部分与该单个编译版本链接(导入)。
尝试同时执行这两个操作几乎没有意义。
There's little sense in trying to do both at the same time.
删除每个函数定义中的 inline
或 RINZO_LIB
都可以,并且都可以。
Remove either inline
or RINZO_LIB
from each function definition that have both, and you should be fine.
编辑:消除任何误解:可以 导出和导入内联函数,并且实际上只是将dllexport / dllimport放在声明应该起作用。
Edit To eliminate any misunderstanding: it is possible to export and import inline functions, and in fact simply placing a dllexport/dllimport on a declaration should just work.
这篇关于C ++:具有dllimport / dllexport的内联函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!