1

I try to write a simple login script using bcrypt. I've tried to use the C wrapper libbcrypt library - https://github.com/trusch/libbcrypt - but got stuck with the compiler complaining about undefined references to bcrypt_checkpw, bcrypt_hashpw, bcrypt_gensalt in the BCrypt class.

First thing I've done was to follow the instructions from the readme.md - https://github.com/trusch/libbcrypt/blob/master/README.md :

git clone https://github.com/trusch/libbcrypt
cd libbcrypt
mkdir build
cd build
cmake ..
make
sudo make install
sudo ldconfig

Then I have my main.cpp file

#include <iostream>
#include "User_Class.hpp"

void checkPermission();

int main(int argc, char* argv[]) {    
    checkPermission();
    return 0;
}

void checkPermission() {
    User_Class User;
    User.Login("test");
}

My User_Class.cpp file

#include <iostream>
#include "Password_Class.hpp"
#include "User_Class.hpp"

bool User_Class::Login(std::string password) {
    return Password_Class::authenticate(password);
}

My User_Class.hpp file

class User_Class {
    public:
        bool Login(std::string password);
};

My Password_Class.cpp file

#include <iostream>
#include "Password_Class.hpp"
#include "bcrypt/BCrypt.hpp"

bool Password_Class::authenticate(std::string password) {

    // this fails!
    std::string hash = BCrypt::generateHash(password);  
    std::cout << BCrypt::validatePassword(password,hash) << std::endl;

    return true;

}

And my Password_Class.hpp file

class Password_Class {
    public:
        static bool authenticate(Glib::ustring password);
};

When compiling with this command

g++ -lbcrypt main.cpp User_Class.cpp Password_Class.cpp -o main `pkg-config --libs --cflags gtkmm-3.0`

I get errors

/tmp/ccc3Va1h.o: In Funktion »BCrypt::generateHash(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)«:
Password_Class.cpp:(.text._ZN6BCrypt12generateHashERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEi[_ZN6BCrypt12generateHashERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEi]+0x44): Warnung: undefinierter Verweis auf »bcrypt_gensalt«
Password_Class.cpp:(.text._ZN6BCrypt12generateHashERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEi[_ZN6BCrypt12generateHashERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEi]+0xb0): Warnung: undefinierter Verweis auf »bcrypt_hashpw«
/tmp/ccc3Va1h.o: In Funktion »BCrypt::validatePassword(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&)«:
Password_Class.cpp:(.text._ZN6BCrypt16validatePasswordERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES7_[_ZN6BCrypt16validatePasswordERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES7_]+0x33): Warnung: undefinierter Verweis auf »bcrypt_checkpw«
collect2: error: ld returned 1 exit status

So please can someone tell me what's going on here? What have I done wrong so how do I use bcrypt in C++? I guess some libraries couldn't be found but I'm really no expert in linux or C++ so please forgive me ;)

Thank you for your help!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    Put the `-lbrypt` flag after the `cpp` files on the command line. Order of linker flags / source files matters. (I am not saying that this definitively resolves the issue though.) – walnut Dec 11 '19 at 20:37
  • In fact that was the problem. Thank you so much! I was searching for the answer for so long. Best regards! – JustAFurryCow Dec 11 '19 at 21:17
  • 1
    Possible duplicate of [Why does the order in which libraries are linked sometimes cause errors in GCC?](https://stackoverflow.com/questions/45135/why-does-the-order-in-which-libraries-are-linked-sometimes-cause-errors-in-gcc) – walnut Dec 11 '19 at 21:19
  • Wow that is really complicated. I have to take a look into this. Thank you! Edit: tbh I really don't have any idea of how the compiler works. All I've done was copy pasta and was glad that I was able to compile all files based on a 1 file example. I really have to look what's happening there. – JustAFurryCow Dec 11 '19 at 21:55
  • You might want to read through [this question](https://stackoverflow.com/questions/6264249/how-does-the-compilation-linking-process-work) and click yourself through other linked questions. That should give you a rough overview, although the details are indeed often quite complicated and compiler-dependent. A [good introductory book to C++](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) should also explain the basic ideas. – walnut Dec 11 '19 at 22:24

0 Answers0