I'm getting undefined reference to ´long long fromBigEndin<long long>(unsigned char*)´
for a template specialization.
See code here: https://onlinegdb.com/AagKTQJ2B
I have this structure:
util.h
template <class T>
T fromBigEndin(uint8_t *buf);
template <>
int64_t fromBigEndin<int64_t>(uint8_t *buf);
template <>
long long fromBigEndin<long long>(unsigned char *buf);
util.cpp
template<class T>
T fromBigEndin(uint8_t *buf) {
T number = 0;
uint8_t nBytes = sizeof(T);
uint8_t i;
for (i = 0; i < nBytes; i += 1) {
number += buf[i] << (16 * (nBytes - i - 1));
}
return number;
}
headerImpl.h
#include "util.h"
void handleOpenSession(uint8_t *data) {
uint8_t *uid = (uint8_t *)malloc(8);
memcpy(uid, data + 1, 8);
int64_t uidNbr = fromBigEndin<int64_t>(uid);
}
From @AnoopRana response, putting the implementation in header file works, I would like to know if it is possible to put the implementation in a separate file.
Any idea on how could I force the compilation of fromBigEndin<int64_t>()
?
I've also tried to move the specializations to util.cpp
but doesn't work either.
The code itself works when in a single file and with different declarations: