对库中函数的未定义引用
Undefined Reference to Function in Library
我正在开发自己的数学库,并且在库使用的名称空间中有几个非成员函数("cml",好像很重要)。无论如何,库(我将其静态地构建为.a/。库文件)编译得很好,但是当我在测试程序中使用库时,我得到一个错误,函数是未定义的引用。
这是头文件:
namespace cml
{
template <typename T>
T toRadians(T val);
template <typename T>
T toDegrees(T val);
template <typename T>
T fastSqrt(T val);
template <typename T>
T fastInverseSqrt(T val);
}
这是函数。cpp文件:
#include <CML/func.h>
#include <cmath>
#include <math.h>
#include <limits>
#include <cassert>
namespace cml
{
template <typename T>
T toRadians(T val)
{
static_assert(std::numeric_limits<T>::is_iec559, "toRadians() requires the template parameters to be floating points.");
return val * MATH_PI / 180;
}
template <typename T>
T toDegrees(T val)
{
static_assert(std::numeric_limits<T>::is_iec559, "toDegrees() requires the template parameters to be floating points.");
return val * 180 / MATH_PI;
}
template <typename T>
T fastSqrt(T val)
{
static_assert(std::numeric_limits<T>::is_iec559, "fastSqrt() requires the template parameters to be floating points.");
return T(1) / fastInverseSqrt(val);
}
template <typename T>
T fastInverseSqrt(T val)
{
static_assert(std::numeric_limits<T>::is_iec559, "fastInverseSqrt() requires the template parameters to be floating points.");
T tmp = val;
T half = val * T(0.5);
uint32* p = reinterpret_cast<uint32*>(const_cast<T*>(&val));
uint32 i = 0x5f3759df - (*p >> 1);
T* ptmp = reinterpret_cast<T*>(&i);
tmp = *ptmp;
tmp = tmp * (T(1.5) - half * tmp * tmp);
#if defined(CML_ACCURATE_FUNC) // If more accuracy is requested, run Newton's Method one more time
tmp = tmp * (T(1.5) - half * tmp * tmp);
#endif // defined
return tmp;
}
}
我得到了所有四个函数的错误。我正在使用最新版本的Code::Blocks,在Windows8上使用GCC。我正在编译Win32。我试过不同的建议,我发现像标记功能extern
,但它似乎没有解决它。我肯定是缺少了一些简单的东西,如果是这样的话,另一双眼睛可能会有所帮助。
undefined reference to
float cml::toRadians(float)' '
需要在头文件中定义模板函数。我要做的是
- 将function .cpp重命名为function。
- 在function .h底部添加
#include "func.inl"
行
模板函数不像普通函数那样编译,而是在后面的编译阶段处理(像内联函数)。
相关文章:
- 对C宏的未定义引用,但在定义它时会出现重新定义错误
- 编译时的 CImg 库返回对"__imp_SetDIBitsToDevice"的未定义引用
- 对Py_Initialize()的未定义引用
- 使用mysql c++连接器的未定义引用
- 对 Scalar ::Scalar() 的未定义引用
- 对复制 CTOR 和 CTOR 的未定义引用
- 对显式实例化的模板函数的未定义引用
- TensorRT (C++ API) 对"createNvOnnxParser_INTERNAL"的未定义引用
- 2个模板化类的非模板友元函数未定义引用错误
- 编译 libfluid 样本控制器时对"event_base_del_virtual"的未定义引用
- 获取对function_name的未定义引用
- 对 'std::thread::_M_start_thread CMake 的未定义引用进行基准测试
- 对结构方法的未定义引用
- 使用内联函数 c++ 的未定义引用
- 对 CMake 中'cudaRegisterLinkedBinary'链接错误的未定义引用?
- 对 DLOPEN 的未定义引用
- QT C++中对全局变量的未定义引用
- 快速数学导致对"__pow_finite"的未定义引用
- 对 boost::system::d etail::system_category_instance 的未定义引用,从
- OpenCV 3.4.3 中对 'cv::String::d eallocate()' 错误的未定义引用