当模板类在共享库和静态库中编译时,我如何使用和消费它?
例如,我有一个库项目:
/// Lib/Config.hpp
// if building a shared library
#ifdef LIB_BUILD_DLL
# define LIB_API __declspec(dllexport)
# define LIB_TEMPLATE_API __declspec(dllexport)
# define LIB_EXTERN
// exe projects use the shared library
#elif defined(LIB_DLL)
# define LIB_API __declspec(dllimport)
# define LIB_TEMPLATE_API
# define LIB_EXTERN extern
// if building a static library, and exe projects use the static library
#else
# define LIB_API
# define LIB_TEMPLATE_API
# define LIB_EXTERN
#endif
/// End of Lib/Config.hpp
/// Lib/Location.hpp
template<typename T>
LIB_EXTERN class Vector2;
class LIB_API Location
{
public:
Vector2<float> point;
};
/// End of Lib/Location.hpp
/// Lib/Vector2.hpp
template<typename T>
LIB_EXTERN class LIB_TEMPLATE_API Vector2
{
public:
template <typename U>
explicit Vector2(const Vector2<U>& vector)
{
x = vector.x;
y = vector.y;
}
Vector2<T> operator -(const Vector2<T>& right)
{
return Vector2<T>(x - right.x, y - right.y);
}
T x;
T y;
};
typedef Vector2<float> Vector2f;
/// End of Lib/Vector2.hpp和一个exe主项目:
#include "Lib/Config.hpp"
#include "Lib/Location.hpp"
#include "Lib/Vector2.hpp"
class Application
{
public:
Location location_;
Vector2<float> point_;
Vector2f float_point_;
}我想问的是,我是否正确地使用了LIB_EXTERN?
如何将Vector2f对象传递给复制构造函数Vector2(const Vector2<U>& vector)
发布于 2018-02-03 19:40:42
通常不会导出模板函数或类。它们针对每个翻译单元进行编译,并在必要时由链接器进行清理。只需包含.hpp文件,就可以在需要的地方编译必要的代码。
https://stackoverflow.com/questions/48596465
复制相似问题