ReneNyffenegger / cpp-base64

base64 encoding and decoding with c++
Other
891 stars 311 forks source link

undefined reference to `base64_encode[abi:cxx11](unsigned char const*, unsigned int, bool)' #22

Closed parkergray221 closed 3 years ago

parkergray221 commented 3 years ago

Hello,

I'm trying to use cpp-base64 to convert a png into base64. I'm trying to use the std::string base64_encode(unsigned char const*, size_t len, bool url = false); constructor.

Whenever I try to compile my code I get the following error: undefined reference to base64_encode[abi:cxx11](unsigned char const*, unsigned int, bool)

My compiler must be converting size_t type variables into unsigned int which triggers this undefined reference. Both attempts to use base64_encode below (one using vec.size() as the length argument and the other using an explicit size_t variable as the length argument) result in the same undefined reference. I'm not sure why my compiler is failing here, as vector.size() http://www.cplusplus.com/reference/vector/vector/size/ return a size_t variable and the explicitly defined size_t len should certainly be read as a size_t variable and not as an unsigned int. I think I must be missing a compiler flag but I don't know what exactly it is I'm missing. My code is below:

#include <cstddef> // one of many headers that defines size_t
#include <fstream>
#include <iterator> 
#include <string>
#include <vector>

string base64encoder(){
  // https://renenyffenegger.ch/notes/development/Base64/Encoding-and-decoding-base-64-with-cpp/

  ifstream input("out.png", ios::in | ios::binary);

  // https://stackoverflow.com/questions/42136152/cannot-get-vector-size-when-constructed-from-istreambuf-iterator
  auto start = istreambuf_iterator<char>(input); // need to initialize start, stop like this so we can access vec.size(), see above link
  auto stop = istreambuf_iterator<char>();
  vector<unsigned char> vec(start, stop); // the unsigned char array that will hold binary string version of out.png.

  auto base64_png = reinterpret_cast<const unsigned char*>(vec.data()); // convert vec to const unsigned char* to conform to base64_encode arg reqs.
  size_t len = 50;
  // vec.size() should return a size_t, which is what base64.h wants for this version of its constructor. HOWEVER vec.size() keeps returning unsigned int and causing build errors.
  string result = base64_encode(base64_png, len); // feed vec to base64_encode and output the encoded string into result. results in compiler error
  string result2 = base64_encode(base64_png, vec.size()); // results in compiler error

  return result;
}

Thanks for any assistance you can give me!

parkergray221 commented 3 years ago

When I added an include for base64.cpp the compile error went away and both iterations work.

Bouska commented 3 years ago

There is indeed a bug in the header, it is assuming that a size_t definition is provided by <string> but that doesn't have to be the case. #include <stddef.h> should be added to the includes.