[英]c++ class template explicit instantiation work on macos, doesn't work on ubuntu
我正在編寫一些要發布為.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
我通過替換compile命令解決了這個問題
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”向前移動。 無論如何,問題解決了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.