lundi 31 août 2020

Non-template function declared in header for use in template function causing /bin/ld: DWARF error: could not find variable specification

Thank you for taking the time too look at this question. I have searched for answers to this problem but it's difficult to find an answer when you don't know exactly what to search for.

I want to use a function declared in the header file in a template function declared in the same file. I have two files, a header and source file. The non-template function is defined in a cpp file. For some reason though this causes. This is a simplified example of the functions causing the issue. Please let me know if you need anymore information and thanks for any insight in advance.

Note, dropping the code from the function defined in func.cpp into the template function directly allows it to compile and run. The issue with that is the length that my function would reach if I can't define other functions to be used inside the template function. It would be a giant mess that I am trying to avoid.

func.h:

void getDerivative(std::map<std::string,double> comp, double* omega,std::map<std::string,size_t> indexMap);

template<class MATTYPE> void Precondition(Matrix<MATTYPE> *preconditioner)
{
  double* omega = new double[numberOfEquations];
  std::map<std::string,double> comp = getComp(); //Included function to get comp
  getOmega(omega); //Included function to get omega
  std::map<std::string,size_t> indexMap = getIndexMap(); //Included function to get index map
  getDerivative(comp,omega,indexMap);
  .
  .
}

func.cpp

void getDerivative(Composition comp, double* omega,std::map<std::string,size_t> indexMap)
{ 
  //flattened index for derivatives
  size_t idx; 
  
  for (std::map<std::string,double>::iterator iter1 = comp.begin(); iter1 != comp.end(); iter1++) //Independent variable
    {
      for (std::map<std::string,double>::iterator iter2 = comp.begin(); iter2 != comp.end(); iter2++) //Dependent variable
      {
        idx = indexMap[iter1->first]+indexMap[iter2->first]*indexMap.size();
        std::cout<<idx<<std::endl;
      }
    }
}

The error error

/bin/ld: /bin/ld: DWARF error: could not find variable specification at offset 2045
/bin/ld: DWARF error: could not find variable specification at offset 209e
.
.
.
undefined reference to getDerivative(std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, double, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, double> > >, double*, std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned long, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, unsigned long> > >)
collect2: error: ld returned 1 exit status
scons: *** [dev-test] Error 1
scons: building terminated because of errors.

Aucun commentaire:

Enregistrer un commentaire