samedi 26 août 2023

linker loading same file even though have header guards

It seems like my linker is including the serialize.h file multiple times based on the error but this file is of the format

// // Created by mctrivia on 06/07/23. //

#ifndef DIGIASSET_CORE_SERIALIZE_H
#define DIGIASSET_CORE_SERIALIZE_H

//all code here

#endif //DIGIASSET_CORE_SERIALIZE_H

I will post entire file at end but pretty sure this part is what should prevent everything from being added more than once. The error codes I am getting are bellow. What am I doing wrong?

error message:

/home/mctrivia/Programs/clion-2023.1.3/bin/cmake/linux/x64/bin/cmake --build /home/mctrivia/Code/CLionProjects/DigiAsset-Core/cmake-build-debug --target Google_Tests_run -j 18
[1/1] Linking CXX executable tests/Google_Tests_run
FAILED: tests/Google_Tests_run 
: && /usr/bin/c++ -std=c++11 -g -Wall -g  tests/CMakeFiles/Google_Tests_run.dir/BitIO.cpp.o tests/CMakeFiles/Google_Tests_run.dir/DatabaseChain.cpp.o tests/CMakeFiles/Google_Tests_run.dir/DigiByteCore.cpp.o tests/CMakeFiles/Google_Tests_run.dir/IPFS.cpp.o tests/CMakeFiles/Google_Tests_run.dir/Base58Tests.cpp.o tests/CMakeFiles/Google_Tests_run.dir/DigiAssetTest.cpp.o tests/CMakeFiles/Google_Tests_run.dir/DigiAssetRulesTest.cpp.o tests/CMakeFiles/Google_Tests_run.dir/DigiAssetTransactionTest.cpp.o tests/CMakeFiles/Google_Tests_run.dir/TestHelpers.cpp.o -o tests/Google_Tests_run  src/libdigiasset_core_lib.a  -ldigibyteapi  -lsqlite3  -lcryptopp  -lcurl  lib/libgtest.a  lib/libgtest_main.a  lib/libgtest.a && :
/usr/bin/ld: src/libdigiasset_core_lib.a(DigiAssetTypes.cpp.o): in function `std::vector<unsigned char, std::allocator<unsigned char> >::_S_nothrow_relocate(std::integral_constant<bool, true>)':
/home/mctrivia/Code/CLionProjects/DigiAsset-Core/src/serialize.h:22: multiple definition of `serialize(std::vector<unsigned char, std::allocator<unsigned char> >&, unsigned char const&)'; src/libdigiasset_core_lib.a(DigiAssetRules.cpp.o):/home/mctrivia/Code/CLionProjects/DigiAsset-Core/src/serialize.h:22: first defined here
/usr/bin/ld: src/libdigiasset_core_lib.a(DigiAssetTypes.cpp.o): in function `std::move_iterator<unsigned char*>::move_iterator(unsigned char*)':
/home/mctrivia/Code/CLionProjects/DigiAsset-Core/src/serialize.h:26: multiple definition of `deserialize(std::vector<unsigned char, std::allocator<unsigned char> > const&, unsigned long&, unsigned char&)'; src/libdigiasset_core_lib.a(DigiAssetRules.cpp.o):/home/mctrivia/Code/CLionProjects/DigiAsset-Core/src/serialize.h:26: first defined here
/usr/bin/ld: src/libdigiasset_core_lib.a(DigiAssetTypes.cpp.o): in function `serialize(std::vector<unsigned char, std::allocator<unsigned char> >&, unsigned long const&)':
/home/mctrivia/Code/CLionProjects/DigiAsset-Core/src/serialize.h:32: multiple definition of `serialize(std::vector<unsigned char, std::allocator<unsigned char> >&, unsigned long const&)'; src/libdigiasset_core_lib.a(DigiAssetRules.cpp.o):/home/mctrivia/Code/CLionProjects/DigiAsset-Core/src/serialize.h:32: first defined here
/usr/bin/ld: src/libdigiasset_core_lib.a(DigiAssetTypes.cpp.o): in function `deserialize(std::vector<unsigned char, std::allocator<unsigned char> > const&, unsigned long&, unsigned long&)':
/home/mctrivia/Code/CLionProjects/DigiAsset-Core/src/serialize.h:39: multiple definition of `deserialize(std::vector<unsigned char, std::allocator<unsigned char> > const&, unsigned long&, unsigned long&)'; src/libdigiasset_core_lib.a(DigiAssetRules.cpp.o):/home/mctrivia/Code/CLionProjects/DigiAsset-Core/src/serialize.h:39: first defined here
/usr/bin/ld: src/libdigiasset_core_lib.a(DigiAssetTypes.cpp.o): in function `serialize(std::vector<unsigned char, std::allocator<unsigned char> >&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
/home/mctrivia/Code/CLionProjects/DigiAsset-Core/src/serialize.h:50: multiple definition of `serialize(std::vector<unsigned char, std::allocator<unsigned char> >&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'; src/libdigiasset_core_lib.a(DigiAssetRules.cpp.o):/home/mctrivia/Code/CLionProjects/DigiAsset-Core/src/serialize.h:50: first defined here
/usr/bin/ld: src/libdigiasset_core_lib.a(DigiAssetTypes.cpp.o): in function `deserialize(std::vector<unsigned char, std::allocator<unsigned char> > const&, unsigned long&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&)':
/home/mctrivia/Code/CLionProjects/DigiAsset-Core/src/serialize.h:60: multiple definition of `deserialize(std::vector<unsigned char, std::allocator<unsigned char> > const&, unsigned long&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&)'; src/libdigiasset_core_lib.a(DigiAssetRules.cpp.o):/home/mctrivia/Code/CLionProjects/DigiAsset-Core/src/serialize.h:60: first defined here
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.

full serialize.h:

//
// Created by mctrivia on 06/07/23.
//

#ifndef DIGIASSET_CORE_SERIALIZE_H
#define DIGIASSET_CORE_SERIALIZE_H

#include <vector>
#include <cstdint>
#include <cstddef>
#include <string>
#include <stdexcept>

using namespace std;




/**
 * Serialize function.  Takes input serializes it and adds it to the serializedData
 */
void serialize(vector<uint8_t>& serializedData, const uint8_t& input) {
    serializedData.push_back(input);
}

void deserialize(const vector<uint8_t>& serializedData, size_t& i, uint8_t& output) {
    if (i >= serializedData.size()) throw out_of_range("read past end of data");
    output = serializedData[i];
    i++;
}

void serialize(vector<uint8_t>& serializedData, const uint64_t& input) {
    for (size_t shift = 56; shift > 0; shift -= 8) {
    serializedData.push_back((input >> shift) & 0xff);
    }
    serializedData.push_back(input & 0xff);
}

void deserialize(const vector<uint8_t>& serializedData, size_t& i, uint64_t& output) {
    if (i + 8 > serializedData.size()) throw out_of_range("read past end of data");
    output = 0;
    for (size_t shift = 56; shift > 0; shift -= 8) {
    output += ((uint64_t) serializedData[i] << shift);
    i++;
    }
    output += serializedData[i];
    i++;
}

void serialize(vector<uint8_t>& serializedData, const string& input) {
    //store number of elements
    serialize(serializedData, (uint64_t) input.size());

    //store elements
    for (const char& letter: input) {
    serialize(serializedData, (uint8_t) letter);
    }
}

void deserialize(const vector<uint8_t>& serializedData, size_t& i, string& output) {
    //get length
    uint64_t size;
    deserialize(serializedData, i, size);

    //error check
    if (i + size > serializedData.size()) throw out_of_range("read past end of data");

    //decode element
    output.clear();
    output.resize(size);
    for (size_t ii = 0; ii < size; ii++) {
    output[ii] = serializedData[i];
    i++;
    }
}

/**
 * Generic function to serialize vector of any type
 * Still need a function to serialize the type inside the vector for this to work
 */
template<typename T>
void serialize(vector<uint8_t>& serializedData, const vector<T>& input) {
    //store number of elements
    serialize(serializedData, (uint64_t) input.size());

    //store elements
    for (const T& element: input) {
    serialize(serializedData, element);
    }
}


template<typename T>
void deserialize(const vector<uint8_t>& serializedData, size_t& i, vector<T>& output) {
    //get length
    uint64_t size;
    deserialize(serializedData, i, size);

    //decode element
    output.clear();
    output.resize(size);
    for (T& value: output) {
    deserialize(serializedData, i, value);
    }
}


#endif //DIGIASSET_CORE_SERIALIZE_H

Aucun commentaire:

Enregistrer un commentaire