2014-03-27 3 views
1

다른 프로젝트에서 사용할 DLL과 라이브러리를 작성하려고하는데 다음 코드, 헤더 및 소스 코드가 있습니다. dll과 lib 모두를 내보내는 적절한 단계를 밟은 것처럼 느껴지지만 lib 파일이 표시되지 않습니다. 나는 다른 사람들의 질문을 체크 아웃하고 EXPORTS를 전 처리기 정의에 추가했다.가져 오기 라이브러리가 없습니다. 내보낼 때 건물이 없습니다.

Matrix.h

#ifdef MATRIX_EXPORTS 
#define MATRIX_API __declspec(dllexport) 
#else 
#define MATRIX_API __declspec(dllimport) 
#endif 

#include <vector> 

using namespace std; 

template<typename T> class MATRIX_API Matrix 
{ 
public: 
typedef T value_type; 
~Matrix(); 
Matrix(); 
Matrix(int rows, int columns); 
int height; 
int width; 
int stride; 
size_t size; 

T &GetElement(int row, int column); 
void SetElement(int row, int column, T value); 
void SetElements(vector<T> value); 
vector<T>& GetElements(); 
T* GetPointer(); 
void Transpose(Matrix<T> &aMatrix); 
void Pivot(Matrix<T>&A, Matrix<T>&P); 
void LU_Decomp(Matrix<T>&A, Matrix<T>&L, Matrix<T>& U, Matrix<T>& P); 
bool isSingular(Matrix<T>&L, Matrix<T>&U); 
void CreateIdentity(Matrix<T>&I);*/ 
private: 
vector<T> elements; 
T* firstElement; 
}; 

Matrix.cpp는

#include "Matrix.h" 
#include <vector> 
#include <iostream> 

using namespace std; 

template<typename T> 
Matrix<T>::~Matrix() 
{ 
} 

template<typename T> 
Matrix<T>::Matrix() 
{ 
} 

template<typename T> 
Matrix<T>::Matrix(int rows, int columns) 
{ 
height = rows; 
width = columns; 
stride = columns; //in row major order this is equal to the # of columns 
elements.resize(rows*columns); 
firstElement = elements.data(); 
size = height*width*sizeof(T); 
} 

template<typename T> 
MATRIX_API T &Matrix<T>::GetElement(int row, int column) 
{ 
return elements[row*width + column]; //row major order return 
} 

template<typename T> 
MATRIX_API vector<T>& Matrix<T>::GetElements() 
{ 
return elements; //row major order return 
} 

template<typename T> 
MATRIX_API void Matrix<T>::SetElement(int row, int column, T value) 
{ 
elements[row*width + column] = value; //row major order return 
} 

template<typename T> 
MATRIX_API void Matrix<T>::SetElements(vector<T> value) 
{ 
elements = value; 
} 

template<typename T> 
MATRIX_API T* Matrix<T>::GetPointer() 
{ 
return firstElement; 
} 

template<typename T> 
MATRIX_API void Matrix<T>::Transpose(Matrix<T> &aMatrix) 
{ 
Matrix<T> theTranspose(aMatrix.width,aMatrix.height); 

for (int i = 0; i < theTranspose.height; i++) 
{ 
    for (int j = 0; j < theTranspose.width; j++) 
    { 
     theTranspose.SetElement(i,j,aMatrix.GetElement(j,i)); 
    } 
} 

*this = theTranspose; 
} 


template<typename T> 
//Calculates C=A*B 
MATRIX_API Matrix<T> operator * (Matrix<T> A, Matrix<T> B) 
{ 
Matrix<T> C(A.height, B.width); 

for (int m = 0; m < C.height; m++) 
{ 
    for (int p = 0; p < C.width; p++) 
    { 
     T value = (T)0; 
     for (int n = 0; n < A.width; n++) 
     { 
      value += A.GetElement(m,n)*B.GetElement(n,p); 
      C.SetElement(m,p,value); 
     } 
    } 
} 

return C; 
} 

template<typename T> 
MATRIX_API Matrix<T> operator + (Matrix<T> &A, Matrix<T> &B) 
{ 
if (A.height != B.height || A.width != B.width) 
{ 
    cout<<" \n can't add these two matrices. They are not of equal dimensions.   Program Quitting"<<endl; 
} 

Matrix<T> sum(A.height, A.width); 
T value = NULL; 

for (int i = 0; i < A.height; i++) 
{ 
    for (int j = 0; j < A.width; j++) 
    { 
     value = A.GetElement(i,j) + B.GetElement(i,j); 
     sum.SetElement(i,j, value); 
    } 
} 

return sum; 
} 

template<typename T> 
MATRIX_API Matrix<T> operator += (Matrix<T> &A, Matrix<T> &B) 
{ 
A = A + B; 

return A; 
} 

template<typename T> 
MATRIX_API void Matrix<T>::Pivot(Matrix<T>&A, Matrix<T>&P) 
{ 
for (int i = 0; i < A.height; i++) 
{ 
    for (int j = 0; j < A.height; j++) 
    { 
     P.SetElement(i,j,i==j); 
    } 
} 

for (int i = 0; i < A.height; i++) 
{ 
    int max_j = i; 

    for (int j = i; j < A.height; j++) 
    { 
     if (fabs(A.GetElement(j,i)) > fabs(A.GetElement(max_j, i))) 
     { 
      max_j = j; 
     } 
    } 

    if (max_j != i) 
    { 
     for (int k = 0; k < A.height; k++) 
     { 
      T temp = P.GetElement(i,k); 
      P.SetElement(i,k,P.GetElement(max_j,k)); 
      P.SetElement(max_j,k,temp); 
     } 
    } 
} 
} 

template<typename T> 
MATRIX_API void Matrix<T>::LU_Decomp(Matrix<T>&A, Matrix<T>&L, Matrix<T>& U, Matrix<T>& P) 
{ 
int size = L.height; 
pivot(A,P); 

Matrix<T> Aprime; 
Aprime = Aprime.prod(P,A); 

for (int i = 0; i < size; i++) 
{ 
    L.SetElement(i,i,1); 
} 

for (int i = 0; i < size; i++) 
{ 
    for (int j = 0; j < size; j++) 
    { 
     T s; 

     if (j <= i) 
     { 
      s = 0; 
      for (int k = 0; k < j; k++) 
      { 
       s+=L.GetElement(j,k)*U.GetElement(k,i); 
      } 

      T value = Aprime.GetElement(j,i) - s; 
      U.SetElement(j,i,value); 
     } 

     if (j >= i) 
     { 
      s = 0; 
      for (int k = 0; k < i; k++) 
      { 
       s+= L.GetElement(j,k)*U.GetElement(k,i); 
      } 

      T value = (Aprime.GetElement(j,i) - s)/U.GetElement(i,i); 
      L.SetElement(j,i,value); 
     } 
    } 
} 
} 

template<typename T> 
//Take determinant of L and U matrices of LU decomposition to determine if parent matrix is singular 
MATRIX_API bool Matrix<T>::isSingular(Matrix<T>&L, Matrix<T>&U) 
{ 
T detL = L.GetElement(0,0); 
T detU = U.GetElement(0,0); 

for (int i = 1; i < L.height; i++) 
{ 
    detL = detL*L.GetElement(i,i); 
    detU = detU*L.GetElement(i,i); 
} 

if (detL*detU == 0) 
{ 
    return true; 
} 
else 
{ 
    return false; 
} 
} 
+0

참조 http://stackoverflow.com/questions/495021/why-can-templates -only-be-implemented-in-the-header-file – SleuthEye

답변

1

어쩌면 문제가 있지만 처리기 정의에 MATRIX_EXPORTS을하지 EXPORTS을 추가해야합니다. 또한 라이브러리 내부에 템플릿을 인스턴스화해야합니다. 그렇지 않으면 내보낼 항목이 없습니다 (템플릿 기반 클래스 이외의 다른 것은 내보내지지 않는다고 가정)

+0

내가 EXPORTS를 말했을 때 나는 MATRIX_EXPORTS를 의미한다. 위의 헤더 파일에서 모든 것을 cpp로 옮겼습니다. #ifdef MATRIX_EXPORTS 등 헤더 파일에 보관 된 모든 항목이 포함됩니다. 그래도 라이브러리를 만들지 않습니다. – user3390212

+0

라이브러리 내부에서 템플릿을 인스턴스화했는지 확인 했습니까? 해결하지 못했다면 코드를 보내면 해결할 수 있습니다 :-) –