[CodeStudy] Undefined reference to one function in CPP

Published: by Creative Commons Licence (Last updated: )

Undefined reference to one function in CPP

I met the error info in this code line peUnitArray[unitCol]->peUnitMpy(&weightDataBV, &inActBV);

/usr/bin/ld: CMakeFiles/Arch_v2.dir/pe_row_array.cpp.o: in function `PERowArray::peRowArrayTh()':
/home/singularity/Arch_v2/pe_row_array.cpp:91: undefined reference to `PE_Unit<4u, 3u, 3u>::peUnitMpy(sc_dt::sc_bv<32>*, sc_dt::sc_bv<96>*)'

The class PE_Unit is a template class, and the function peUnitMpy is implemented in .cpp file instead of .h file.

So after I move the implementation into the head file, it works.

// Before

// file.h
template<unsigned int A, unsigned int B, unsigned int C>
class PE_Unit{
    void peUnitMpy(XXXX);
}
// file.cpp
template<unsigned int A, unsigned int B, unsigned int C>
void PE_Unit<A, B, C>::peUnitMpy(XXXX){
    //implementations 
}

From this blog, I know that it's better to implementation the functions of template class in the .h files.

For detailed information, see the link above.

Back to Top
Share Post: