abedra / libvault

A C++ library for Hashicorp Vault
MIT License
34 stars 25 forks source link

undefined reference error while compiling the example code #75

Closed VinayIsHere closed 3 years ago

VinayIsHere commented 3 years ago

Hy I successfully installed the libvault in my ubuntu, and was trying to run the example you have provided in your documentation, I was trying to run the following code

#include <iostream>
#include "VaultClient.h"

int main(void)
{
  Vault::HttpErrorCallback httpErrorCallback = [&](std::string err) {
    std::cout << err << std::endl;
  };

  Vault::AppRoleStrategy authStrategy{
    Vault::RoleId{"<role_id>"},
    Vault::SecretId{"<secret_id>"}
  };

  Vault::Config config = Vault::ConfigBuilder().build();
  Vault::Client vaultClient{config, authStrategy, httpErrorCallback};
  Vault::SecretMount mount{"/test"};
  Vault::KeyValue kv{vaultClient, mount};
  Vault::Path key{"hello"};
  Vault::Parameters parameters(
    {
      {"foo","world"},
      {"baz","quux"},
      {"something", "something else"},
    }
  );

  kv.create(key, parameters);

  std::cout << kv.read(key).value() << std::endl;
}

so when I save this code into a file named hellowworldincpp.cpp and trying to compile it with g++ in terminal with the following flags

g++ -std=c++17 -L/usr/local/include -L/usr/local/lib helloworldincpp.cpp

I am getting the following error

/usr/bin/ld: /tmp/cc9RokV1.o: in function `main':
helloworldincpp.cpp:(.text+0x113): undefined reference to `Vault::AppRoleStrategy::AppRoleStrategy(Vault::Tiny<Vault::RoleIdDetail, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, Vault::Tiny<Vault::SecretIdDetail, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >)'
/usr/bin/ld: helloworldincpp.cpp:(.text+0x1a2): undefined reference to `Vault::ConfigBuilder::build()'
/usr/bin/ld: helloworldincpp.cpp:(.text+0x203): undefined reference to `Vault::Client::Client(Vault::Config&, Vault::AuthenticationStrategy&, std::function<void (std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)>)'
/usr/bin/ld: helloworldincpp.cpp:(.text+0x2ae): undefined reference to `Vault::KeyValue::KeyValue(Vault::Client const&, Vault::Tiny<Vault::SecretMountDetail, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >)'
/usr/bin/ld: helloworldincpp.cpp:(.text+0x449): undefined reference to `Vault::KeyValue::create(Vault::Tiny<Vault::PathDetail, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > const&, std::unordered_map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::hash<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::equal_to<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > const&)'
/usr/bin/ld: helloworldincpp.cpp:(.text+0x478): undefined reference to `Vault::KeyValue::read(Vault::Tiny<Vault::PathDetail, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > const&)'
/usr/bin/ld: /tmp/cc9RokV1.o: in function `Vault::AppRoleStrategy::~AppRoleStrategy()':
helloworldincpp.cpp:(.text._ZN5Vault15AppRoleStrategyD2Ev[_ZN5Vault15AppRoleStrategyD5Ev]+0x13): undefined reference to `vtable for Vault::AppRoleStrategy'
/usr/bin/ld: /tmp/cc9RokV1.o: in function `Vault::HttpClient::~HttpClient()':
helloworldincpp.cpp:(.text._ZN5Vault10HttpClientD2Ev[_ZN5Vault10HttpClientD5Ev]+0x13): undefined reference to `vtable for Vault::HttpClient'
/usr/bin/ld: /tmp/cc9RokV1.o: in function `Vault::Client::~Client()':
helloworldincpp.cpp:(.text._ZN5Vault6ClientD2Ev[_ZN5Vault6ClientD5Ev]+0x13): undefined reference to `vtable for Vault::Client'
collect2: error: ld returned 1 exit status

I think I am missing the -l{libraryname} flag but when i add -llibvault it says -llibvault not found. So can you please guide how to compile it?

VinayIsHere commented 3 years ago

hy I just add the flag -lvault and now it is not giving error -llibvault not found but now it is giving me the following error

/usr/bin/ld: /tmp/cclGetWj.o: in function `main':
helloworldincpp.cpp:(.text+0x113): undefined reference to `Vault::AppRoleStrategy::AppRoleStrategy(Vault::Tiny<Vault::RoleIdDetail, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, Vault::Tiny<Vault::SecretIdDetail, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >)'
/usr/bin/ld: helloworldincpp.cpp:(.text+0x1a2): undefined reference to `Vault::ConfigBuilder::build()'
/usr/bin/ld: helloworldincpp.cpp:(.text+0x203): undefined reference to `Vault::Client::Client(Vault::Config&, Vault::AuthenticationStrategy&, std::function<void (std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)>)'
/usr/bin/ld: helloworldincpp.cpp:(.text+0x2ae): undefined reference to `Vault::KeyValue::KeyValue(Vault::Client const&, Vault::Tiny<Vault::SecretMountDetail, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >)'
/usr/bin/ld: helloworldincpp.cpp:(.text+0x449): undefined reference to `Vault::KeyValue::create(Vault::Tiny<Vault::PathDetail, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > const&, std::unordered_map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::hash<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::equal_to<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > const&)'
/usr/bin/ld: helloworldincpp.cpp:(.text+0x478): undefined reference to `Vault::KeyValue::read(Vault::Tiny<Vault::PathDetail, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > const&)'
/usr/bin/ld: /tmp/cclGetWj.o: in function `Vault::AppRoleStrategy::~AppRoleStrategy()':
helloworldincpp.cpp:(.text._ZN5Vault15AppRoleStrategyD2Ev[_ZN5Vault15AppRoleStrategyD5Ev]+0x13): undefined reference to `vtable for Vault::AppRoleStrategy'
/usr/bin/ld: /tmp/cclGetWj.o: in function `Vault::HttpClient::~HttpClient()':
helloworldincpp.cpp:(.text._ZN5Vault10HttpClientD2Ev[_ZN5Vault10HttpClientD5Ev]+0x13): undefined reference to `vtable for Vault::HttpClient'
/usr/bin/ld: /tmp/cclGetWj.o: in function `Vault::Client::~Client()':
helloworldincpp.cpp:(.text._ZN5Vault6ClientD2Ev[_ZN5Vault6ClientD5Ev]+0x13): undefined reference to `vtable for Vault::Client'
collect2: error: ld returned 1 exit status

I am running my g++ command as below

g++ -std=c++17 -L/usr/local/include -L/usr/local/lib -lvault helloworldincpp.cpp

abedra commented 3 years ago

I encourage you to take a look at the examples folder. The examples there are designed to be complete and have all the compilation settings in the corresponding Makefile. You have a few things incorrect in your compilation statement above. You are using the -L option where you should be using -I for headers, and you are missing the link against libcurl -lcurl.

VinayIsHere commented 3 years ago

Successfully run by running the following command

g++ -std=c++17 -c example.cpp
g++ example.o -o example -lvault -lcurl