我正在编写一些我想要发布为.h文件和静态库的代码。代码使用c ++模板编写,在.h文件中使用类声明,而在.cpp文件中定义。我使用“显式实例化”技巧使代码可以编译成静态库。在.cpp文件的末尾添加显式实例化。我写了一些代码来测试这个库。它在macos和windows上工作正常,但在ubuntu上它返回“未定义的引用”链接错误。似乎“显式实例化”技巧对ubuntu不起作用。有人知道为什么吗?
ubuntu上的g ++版本是4.5.3。
修改
这是模板类的.h文件:
#ifndef PHYSIKA_CORE_MATRICES_MATRIX_2X2_H_
#define PHYSIKA_CORE_MATRICES_MATRIX_2X2_H_
#include "Physika_Core/Utilities/global_config.h"
#include "Physika_Core/Matrices/matrix_base.h"
namespace Physika{
template <typename Scalar>
class Matrix2x2: public MatrixBase<Scalar,2,2>
{
public:
Matrix2x2();
Matrix2x2(Scalar x00, Scalar x01, Scalar x10, Scalar x11);
~Matrix2x2();
inline int rows() const{return 2;}
inline int cols() const{return 2;}
Scalar& operator() (int i, int j);
const Scalar& operator() (int i, int j) const;
Matrix2x2<Scalar> operator+ (const Matrix2x2<Scalar> &) const;
//other operation declarations
//omitted here
};
//overriding << for Matrix2x2
template <typename Scalar>
std::ostream& operator<< (std::ostream &s, const Matrix2x2<Scalar> &mat)
{
s<<mat(0,0)<<", "<<mat(0,1)<<std::endl;
s<<mat(1,0)<<", "<<mat(1,1)<<std::endl;
return s;
}
} //end of namespace Physika
#endif //PHYSIKA_CORE_MATRICES_MATRIX_2X2_H_
模板类的.cpp文件如下:
#include "Physika_Core/Matrices/matrix_2x2.h"
namespace Physika{
template <typename Scalar>
Matrix2x2<Scalar>::Matrix2x2()
{
}
template <typename Scalar>
Matrix2x2<Scalar>::Matrix2x2(Scalar x00, Scalar x01, Scalar x10, Scalar x11)
{
#ifdef PHYSIKA_USE_EIGEN_MATRIX
eigen_matrix_2x2_(0,0) = x00;
eigen_matrix_2x2_(0,1) = x01;
eigen_matrix_2x2_(1,0) = x10;
eigen_matrix_2x2_(1,1) = x11;
#endif
}
//other operation definitions
//omitted here
//explicit instantiation of template so that it could be compiled into a lib
template class Matrix2x2<float>;
template class Matrix2x2<double>;
} //end of namespace Physika
测试代码如下:
#include <iostream>
#include "Physika_Core/Matrices/matrix_2x2.h"
using namespace std;
using Physika::Matrix2x2;
int main()
{
cout<<"Matrix2x2 Test"<<endl;
Matrix2x2<double> mat_double(2.0,1.0,1.0,2.0);
cout<<"A 2x2 matrix of double numbers:"<<endl;
cout<<mat_double;
return 0;
}
错误信息是:
g++ -Wall -Wno-write-strings -O2 -I ../../Public_Library/include -L ../../Public_Library/lib -l matrices matrix2x2_test.cpp -o matrix2x2_test
/tmp/ccUrtwwf.o: In function `main':
matrix2x2_test.cpp:(.text+0x91): undefined reference to `Physika::Matrix2x2<double>::Matrix2x2(double, double, double, double)'
matrix2x2_test.cpp:(.text+0x1b9): undefined reference to `Physika::Matrix2x2<double>::Matrix2x2(double, double, double, double)'
//more moitted here
collect2: ld returned 1 exit status
make: *** [matrix2x2_test] Error 1
答案 0 :(得分:0)
我通过替换编译命令
解决了这个问题g++ -Wall -Wno-write-strings -O2 -I ../../Public_Library/include -L ../../Public_Library/lib -l matrices matrix2x2_test.cpp -o matrix2x2_test
使用:
g++ matrix2x2_test.cpp -o matrix2x2_test -Wall -Wno-write-strings -O2 -I ../../Public_Library/include -L ../../Public_Library/lib -l matrices
这太奇怪了,我所做的只是向前移动“matrix2x2_test.cpp -o matrix2x2_test”。无论如何,问题解决了。