0

我正在编写一些我想发布为 .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
4

1 回答 1

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”向前移动。总之,问题解决了。

于 2013-10-16T21:23:19.567 回答