0

I would like to use the password-hashing function Argon2 in my C++ application. But I get a error if I build the application:

error while loading shared libraries: libargon2.so.0: cannot open shared object file: No such file or directory

What I did so far: I downloaded the source in a subfolder of my Qt project folder (thirdparty > Argon2). Called make to build the Argon .so and verified with make test that everything is ok. The project structure looks like this:

testproject > CMakeLists.txt
testproject > application > test > impl > src > Main.cpp
testproject > thirdparty > Argon2 > include > argon2.h
testproject > thirdparty > Argon2 > libargon2.so

In my CMakeLists I added Argon include path and TRIED to link against the .so file:

find_library(Argon2 NAMES libargon2 PATHS ${CMAKE_SOURCE_DIR}/thirdparty/Argon2)
# Additional include directories
INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}                     
                    ${CMAKE_SOURCE_DIR}/thirdparty/Argon2/include
target_link_libraries(${COMPONENT_NAME} ${Argon2})

But this simple test program will give me the above mentioned error.

#include "argon2.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define HASHLEN 32
#define SALTLEN 16
#define PWD "password"  

int main(){  

    uint8_t hash1[HASHLEN];

    uint8_t salt[SALTLEN];
    memset( salt, 0x00, SALTLEN );

    uint8_t *pwd = (uint8_t *)strdup(PWD);
    uint32_t pwdlen = strlen((char *)pwd);

    uint32_t t_cost = 2;            // 1-pass computation
    uint32_t m_cost = (1<<16);      // 64 mebibytes memory usage
    uint32_t parallelism = 1;       // number of threads and lanes

    argon2i_hash_raw(t_cost, m_cost, parallelism, pwd, pwdlen, salt, SALTLEN, hash1, HASHLEN);     
}

I'm still very new to C++ and CMake, so I don't know if my procedure was correct (obviously not, because it does not work).

  1. Is it possible to link only the .so file?
  2. Will I have to include the whole directory of the Argon library in my project (like I tried)?
  3. What are the necessary steps to tell my linker how to find the library correctly?

EDIT

This post seems to be similar to mine. But I can't figure out be their answers if the .so file would be sufficient and how to link against a library which is not installed by the package manager of my debian system.

Community
  • 1
  • 1
little_planet
  • 1,005
  • 1
  • 16
  • 35
  • Possible duplicate of [How to Link a third Party Library (LibUSB) in CMake](http://stackoverflow.com/questions/34995936/how-to-link-a-third-party-library-libusb-in-cmake) – Tsyvarev Jan 29 '16 at 17:53
  • I don't think this is a duplicate. At least I can't figure out how to solve my problem by the answers in the question you mentioned. – little_planet Jan 30 '16 at 09:32
  • Well, the question post has been *changed* and it is no more duplicate of one I suggest. Now you link with the library (`target_link_libraries`), and instead of "undefined reference" you get error about inability to find the library. Is this error a *link error* (you failed to *create* executable) or *run error* (you failed to *run* executable)? Looks like the second case, because such error normally arizes when run executables. – Tsyvarev Jan 30 '16 at 11:27
  • Yes it's an error during runtime. However I was able to link the static library (same folder as .so) with target_link_libraries. But this is not what I intended. – little_planet Jan 30 '16 at 11:30
  • Probably, your executable lacks of RPATH tuning. See [this wiki](https://cmake.org/Wiki/CMake_RPATH_handling) about RPATH and how CMake can handle it. You can always check correctness of RPATH settings using `ldd `. For the future: try to describe situation more precizely. You write `I get a error if I build the application`, and it is normally understood as error you catch during the *build*. But actually this is an error during the *run* of the application. – Tsyvarev Jan 30 '16 at 11:45

2 Answers2

1

What worked for me is to download the repository and run the make command inside of it. This should create the static library libargon2.a. Then in your CMakeLists.txt add

include_directories(libs/argon2/include)
link_directories(libs/argon2)
add_executable(${PROJECT_NAME} src/main.cpp)
target_link_libraries(${PROJECT_NAME} argon2)

where libs/argon2 represents the relative path to the downloaded repository.

nils
  • 338
  • 2
  • 11
0

In my project in project/ directory I have cloned Argon2 into project/argon2/ with CMakeLists.txt

...
target_include_directories(target PUBLIC argon2/include)
target_link_libraries(target argon2)

In code I include Argon2 via #include <argon2.h> and it works for me.

ventaquil
  • 2,780
  • 3
  • 23
  • 48