Getting Multiple Definition error when including a utils file in multiple components #2634
-
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
These are linker multiple define errors. The issue is you've defined the functions inside a c++ file that you are treating as a header. You should instead create |
Beta Was this translation helpful? Give feedback.
-
Here is a quick refactor that will help: utils.hpp: has definitions of #include <vector>
#include <Os/Mutex.hpp>
#ifndef UTILS
#define UTILS
class Lock {
public:
Lock(Os::Mutex _mutex) : mutex(_mutex) { mutex.lock(); }
~Lock() { mutex.unlock(); }
private:
Os::Mutex mutex;
};
uint64_t bytesToUint(const char* bytes, uint16_t len);
std::vector<uint64_t> getDataFromFields(const char* data, std::vector<uint16_t> &sizes);
void ProcessData(uint32_t messageSize, std::vector<char> receivedBytes, std::vector<uint16_t> &fieldSizes, void (*SubsystemDataProcessor) (uint8_t msgType));
#endif utils.cpp: has implementation of functions. #include <utils.hpp>
uint64_t bytesToUint(const char* bytes, uint16_t len) { // implementation }
std::vector<uint64_t> getDataFromFields(const char* data, std::vector<uint16_t> &sizes) { // implementation }
void ProcessData(uint32_t messageSize, std::vector<char> receivedBytes, std::vector<uint16_t> &fieldSizes, void (*SubsystemDataProcessor) (uint8_t msgType)) { // implementation } |
Beta Was this translation helpful? Give feedback.
Here is a quick refactor that will help:
utils.hpp: has definitions of
class Lock
and function prototypes.utils.cpp: has implementation of functions.