#Error trying to include sodium.h and argon2.h

91 messages · Page 1 of 1 (latest)

meager smelt
#

Hello. I'm trying to include these 2 libraries into my c++ project. But it keeps saying "Unable to find file". Then I asked Gemini, and it provided me working with CMake. I do stuff it says but still can't fix anything. Here's the current status:

  • All paths are correct and exist.
  • vcpkg, Visual Studio 2022 (and the other things that needed to install), Git, msys64, libsodium and argon2 installed.
  • Here's the code in CMakeLists.txt right now:
short scaffoldBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

meager smelt
#
set(CMAKE_GENERATOR "Ninja") # Force Ninja generator

cmake_minimum_required(VERSION 3.15)
project(SodiumTest)

message(STATUS "CMake Version: ${CMAKE_VERSION}")

set(CMAKE_TOOLCHAIN_FILE "e:/Software_Core/vcpkg/scripts/buildsystems/vcpkg.cmake" CACHE STRING "Vcpkg Toolchain File")
set(Sodium_DIR "E:/Software_Core/vcpkg/installed/x64-windows/share/unofficial-sodium") # SET Sodium_DIR - NO CACHE PATH

message(STATUS "Sodium_DIR (Absolute): ${Sodium_DIR}")
message(STATUS "CMAKE_MODULE_PATH: ${CMAKE_MODULE_PATH}")
message(STATUS "Sodium_DIR: ${Sodium_DIR}")

find_package(unofficial-sodium CONFIG REQUIRED
  CONFIGS unofficial-sodiumConfig.cmake # Explicitly specify the CONFIG file name
  PATH_SUFFIXES share # Tell find_package to look in "share" subdirectory (though it should already be there via Sodium_DIR)
)

add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} PRIVATE Sodium::Sodium) # Keep target_link_libraries as Sodium::Sodium for now
#

And here is the Output when configure:

#
[main] Configuring project: RandomStuff 
[proc] Executing command: "E:\Software_Core\Microsoft Visual Studio\2022\BuildTools\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe" -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_C_COMPILER:FILEPATH=E:\Software_Core\msys64\ucrt64\bin\gcc.exe -DCMAKE_CXX_COMPILER:FILEPATH=E:\Software_Core\msys64\ucrt64\bin\g++.exe --no-warn-unused-cli -SE:/Byte_Forge/Projects/C++/RandomStuff -Be:/Byte_Forge/Projects/C++/RandomStuff/build -G "MinGW Makefiles"
[cmake] Not searching for unused variables given on the command line.
[cmake] -- CMake Version: 3.30.5-msvc23
[cmake] -- Sodium_DIR (Absolute): E:/Software_Core/vcpkg/installed/x64-windows/share/unofficial-sodium
[cmake] -- CMAKE_MODULE_PATH: 
[cmake] -- Sodium_DIR: E:/Software_Core/vcpkg/installed/x64-windows/share/unofficial-sodium
[cmake] CMake Error at CMakeLists.txt:15 (find_package):
[cmake]   Could not find a package configuration file named
[cmake]   "unofficial-sodiumConfig.cmake" provided by package "unofficial-sodium".
[cmake] 
[cmake]   Add the installation prefix of "unofficial-sodium" to CMAKE_PREFIX_PATH or
[cmake]   set "unofficial-sodium_DIR" to a directory containing one of the above
[cmake]   files.  If "unofficial-sodium" provides a separate development package or
[cmake]   SDK, be sure it has been installed.
[cmake] 
[cmake] 
[cmake] -- Configuring incomplete, errors occurred!
[proc] The command: "E:\Software_Core\Microsoft Visual Studio\2022\BuildTools\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe" -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_C_COMPILER:FILEPATH=E:\Software_Core\msys64\ucrt64\bin\gcc.exe -DCMAKE_CXX_COMPILER:FILEPATH=E:\Software_Core\msys64\ucrt64\bin\g++.exe --no-warn-unused-cli -SE:/Byte_Forge/Projects/C++/RandomStuff -Be:/Byte_Forge/Projects/C++/RandomStuff/build -G "MinGW Makefiles" exited with code: 1
#

I will provide anything you need just please, I struggle with this for a week now, I just really need someone to help me

keen lichen
#

You shouldn't mix msys2 and visual studio

#

vcpkg is kinda VS-centric. It's possible to use it with mingw, but I'd just install the library in msys2

#

(assuming your compiler of choice is mingw)

meager smelt
#

Alright so now what I need to do is to redownload the libs from msys2 right

#

Do I need to uninstall the libs in vcpkg first

keen lichen
#

No, just remove the mention of vcpkg from your cmakelists.txt

meager smelt
#

Okay thank you! Let me just uh go install

#
[main] Configuring project: RandomStuff 
[driver] Removing e:/Byte_Forge/Projects/C++/RandomStuff/build/CMakeCache.txt
[driver] Removing e:\Byte_Forge\Projects\C++\RandomStuff\build\CMakeFiles
[proc] Executing command: "E:\Software_Core\Microsoft Visual Studio\2022\BuildTools\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe" -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_C_COMPILER:FILEPATH=E:\Software_Core\msys64\ucrt64\bin\gcc.exe -DCMAKE_CXX_COMPILER:FILEPATH=E:\Software_Core\msys64\ucrt64\bin\g++.exe --no-warn-unused-cli -SE:/Byte_Forge/Projects/C++/RandomStuff -Be:/Byte_Forge/Projects/C++/RandomStuff/build -G "MinGW Makefiles"
[cmake] Not searching for unused variables given on the command line.
[cmake] -- The C compiler identification is GNU 13.2.0
[cmake] -- The CXX compiler identification is GNU 13.2.0
[cmake] -- Detecting C compiler ABI info
[cmake] -- Detecting C compiler ABI info - done
[cmake] -- Check for working C compiler: E:/Software_Core/msys64/ucrt64/bin/gcc.exe - skipped
[cmake] -- Detecting C compile features
[cmake] -- Detecting C compile features - done
[cmake] -- Detecting CXX compiler ABI info
[cmake] -- Detecting CXX compiler ABI info - done
[cmake] -- Check for working CXX compiler: E:/Software_Core/msys64/ucrt64/bin/g++.exe - skipped
#
[cmake] -- Detecting CXX compile features
[cmake] -- Detecting CXX compile features - done
[cmake] -- CMake Version: 3.30.5-msvc23
[cmake] -- CMAKE_MODULE_PATH: 
[cmake] CMake Error at CMakeLists.txt:11 (find_package):
[cmake]   By not providing "FindSodium.cmake" in CMAKE_MODULE_PATH this project has
[cmake]   asked CMake to find a package configuration file provided by "Sodium", but
[cmake]   CMake did not find one.
[cmake] 
[cmake]   Could not find a package configuration file provided by "Sodium" with any
[cmake]   of the following names:
[cmake] 
[cmake]     SodiumConfig.cmake
[cmake]     sodium-config.cmake
[cmake] 
[cmake]   Add the installation prefix of "Sodium" to CMAKE_PREFIX_PATH or set
[cmake]   "Sodium_DIR" to a directory containing one of the above files.  If "Sodium"
[cmake]   provides a separate development package or SDK, be sure it has been
[cmake]   installed.
[cmake] 
[cmake] 
[cmake] -- Configuring incomplete, errors occurred!
[proc] The command: "E:\Software_Core\Microsoft Visual Studio\2022\BuildTools\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe" -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_C_COMPILER:FILEPATH=E:\Software_Core\msys64\ucrt64\bin\gcc.exe -DCMAKE_CXX_COMPILER:FILEPATH=E:\Software_Core\msys64\ucrt64\bin\g++.exe --no-warn-unused-cli -SE:/Byte_Forge/Projects/C++/RandomStuff -Be:/Byte_Forge/Projects/C++/RandomStuff/build -G "MinGW Makefiles" exited with code: 1
#

Sorry I'm just too new to install libraries from the outside

keen lichen
#

You're using cmake that VS installed

#

You should install it in msys2 too

#

I swear, half of the problems with msys2 are solved by telling people to reinstall all their shit in msys2

meager smelt
#

Using the UCRT64 right

short scaffoldBOT
#

@meager smelt Has your question been resolved? If so, type !solved :)

meager smelt
#

I installed CMake in msys2 successfully but what now

#

it still doesnt work

keen lichen
#

Show the new log

meager smelt
#
[main] Configuring project: RandomStuff 
[proc] Executing command: E:\Software_Core\msys64\ucrt64\bin\cmake.EXE -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_C_COMPILER:FILEPATH=E:\Software_Core\msys64\ucrt64\bin\gcc.exe -DCMAKE_CXX_COMPILER:FILEPATH=E:\Software_Core\msys64\ucrt64\bin\g++.exe --no-warn-unused-cli -SE:/Byte_Forge/Projects/C++/RandomStuff -Be:/Byte_Forge/Projects/C++/RandomStuff/build -G "MinGW Makefiles"
[cmake] Not searching for unused variables given on the command line.
[cmake] -- CMake Version: 3.28.1
[cmake] -- CMAKE_MODULE_PATH: 
[cmake] CMake Error at CMakeLists.txt:9 (find_package):
[cmake]   By not providing "FindSodium.cmake" in CMAKE_MODULE_PATH this project has
[cmake]   asked CMake to find a package configuration file provided by "Sodium", but
[cmake]   CMake did not find one.
[cmake] 
[cmake]   Could not find a package configuration file provided by "Sodium" with any
[cmake]   of the following names:
[cmake] 
[cmake]     SodiumConfig.cmake
[cmake]     sodium-config.cmake
[cmake] 
[cmake]   Add the installation prefix of "Sodium" to CMAKE_PREFIX_PATH or set
[cmake]   "Sodium_DIR" to a directory containing one of the above files.  If "Sodium"
[cmake]   provides a separate development package or SDK, be sure it has been
[cmake]   installed.
[cmake] 
[cmake] 
[cmake] -- Configuring incomplete, errors occurred!
[proc] The command: E:\Software_Core\msys64\ucrt64\bin\cmake.EXE -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_C_COMPILER:FILEPATH=E:\Software_Core\msys64\ucrt64\bin\gcc.exe -DCMAKE_CXX_COMPILER:FILEPATH=E:\Software_Core\msys64\ucrt64\bin\g++.exe --no-warn-unused-cli -SE:/Byte_Forge/Projects/C++/RandomStuff -Be:/Byte_Forge/Projects/C++/RandomStuff/build -G "MinGW Makefiles" exited with code: 1
#

I think that's the same

#

Here's my CMakeLists.txt code:

cmake_minimum_required(VERSION 3.15)
project(PasswordHashingExample) # Or your project name

set(CMAKE_PREFIX_PATH "E:/Software_Core/msys64/ucrt64" CACHE PATH "MSYS2 UCRT64 Prefix Path" FORCE) # NEW: Add MSYS2 UCRT64 prefix

message(STATUS "CMake Version: ${CMAKE_VERSION}")
message(STATUS "CMAKE_MODULE_PATH: ${CMAKE_MODULE_PATH}")

find_package(Sodium REQUIRED) # Try find_package for Sodium again
find_package(Argon2 REQUIRED) # Try find_package for Argon2 again

add_executable(${PROJECT_NAME} main.cpp)

target_link_libraries(${PROJECT_NAME} PRIVATE
    Sodium::Sodium # Link to Sodium
    Argon2::Argon2 # Link to Argon2
)
#

Wait do I really need this CMakeLists thing at first when I download the libs using msys2 now?

#

Can I just #include them

keen lichen
#

You don't need set(CMAKE_PREFIX_PATH

keen lichen
#

Did you only start using cmake to be able to use those libs?

meager smelt
#

Yes really

#

I'm new to downloading libs from the outside (I was so dumb I even tried to install the files from Github of the libs) so I tried to search for how to install libsodium and argon2 but like I couldn't file anything so I used Gemini and it led me here.

meager smelt
# keen lichen You don't need `set(CMAKE_PREFIX_PATH`
[main] Configuring project: RandomStuff 
[proc] Executing command: E:\Software_Core\msys64\ucrt64\bin\cmake.EXE -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_C_COMPILER:FILEPATH=E:\Software_Core\msys64\ucrt64\bin\gcc.exe -DCMAKE_CXX_COMPILER:FILEPATH=E:\Software_Core\msys64\ucrt64\bin\g++.exe --no-warn-unused-cli -SE:/Byte_Forge/Projects/C++/RandomStuff -Be:/Byte_Forge/Projects/C++/RandomStuff/build -G "MinGW Makefiles"
[cmake] Not searching for unused variables given on the command line.
[cmake] -- CMake Version: 3.28.1
[cmake] -- CMAKE_MODULE_PATH: 
[cmake] CMake Error at CMakeLists.txt:7 (find_package):
[cmake]   By not providing "FindSodium.cmake" in CMAKE_MODULE_PATH this project has
[cmake]   asked CMake to find a package configuration file provided by "Sodium", but
[cmake]   CMake did not find one.
[cmake] 
[cmake]   Could not find a package configuration file provided by "Sodium" with any
[cmake]   of the following names:
[cmake] 
[cmake]     SodiumConfig.cmake
[cmake]     sodium-config.cmake
[cmake] 
[cmake]   Add the installation prefix of "Sodium" to CMAKE_PREFIX_PATH or set
[cmake]   "Sodium_DIR" to a directory containing one of the above files.  If "Sodium"
[cmake]   provides a separate development package or SDK, be sure it has been
[cmake]   installed.
[cmake] 
[cmake] 
[cmake] -- Configuring incomplete, errors occurred!
[proc] The command: E:\Software_Core\msys64\ucrt64\bin\cmake.EXE -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_C_COMPILER:FILEPATH=E:\Software_Core\msys64\ucrt64\bin\gcc.exe -DCMAKE_CXX_COMPILER:FILEPATH=E:\Software_Core\msys64\ucrt64\bin\g++.exe --no-warn-unused-cli -SE:/Byte_Forge/Projects/C++/RandomStuff -Be:/Byte_Forge/Projects/C++/RandomStuff/build -G "MinGW Makefiles" exited with code: 1

I removed the line, here's the error

meager smelt
keen lichen
#

Learning CMake is useful by itself, but you don't have to use it just to use some libraries

#

Apparently Sodium doesn't include a CMake config, so you can't use find_package with it, at least in msys2

#

Who told you to do it like this? Not chatgpt I hope?

meager smelt
#

Ye it's Gemini 💀

short scaffoldBOT
#
Large Language Models (LLMs)

We highly recommend against the use of LLMs and AI assistants because:

  • LLMs are bad at C and C++
  • LLMs are wrong more often than not
  • LLMs answer with complete confidence even when wrong
  • If you're new to C or C++ you likely don't know enough to know when answers are wrong
meager smelt
#

Okay

#

Well cuz I'm so stucky I need to use it

#

But yeah I found out there's a C++ server on Discord now

keen lichen
#

Well it doesn't help as you see 😛

meager smelt
#

xdd

keen lichen
#

But what I see is this:

#

This means that you need g++ whatever.cpp -lsodium to use it (-l__ uses lib__.a or lib__.dll.a)

#

Or if you prefer CMake, add sodium to target_link_libraries (instead of Sodium::Sodium)

meager smelt
#

Hm okay I think I will try to use CMake cuz yea I accidently used it. Here's the current code:

cmake_minimum_required(VERSION 3.15)
project(PasswordHashingExample)

message(STATUS "CMake Version: ${CMAKE_VERSION}")
message(STATUS "CMAKE_MODULE_PATH: ${CMAKE_MODULE_PATH}")

add_executable(${PROJECT_NAME} main.cpp)

target_link_libraries(${PROJECT_NAME} PRIVATE
    sodium
    argon2
)

Now it built with no errors. So I can include the libraries into main.cpp now? Do I need to do anything next

keen lichen
#

Dunno, try including them

meager smelt
#

ok

#

main.cpp when I pressed F5 ran fine, but I hasn't coded anything using the libs yet so uh guess it's cooking time

meager smelt
#
Starting build...
cmd /c chcp 65001>nul && E:\Software_Core\msys64\ucrt64\bin\g++.exe -fdiagnostics-color=always -g E:\Byte_Forge\Projects\C++\RandomStuff\random.cpp -o E:\Byte_Forge\Projects\C++\RandomStuff\random.exe
E:/Software_Core/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\buiha\AppData\Local\Temp\ccfLFXWD.o: in function `hashPassword(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
E:/Byte_Forge/Projects/C++/RandomStuff/random.cpp:70:(.text+0x271): undefined reference to `sodium_init'
E:/Software_Core/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: E:/Byte_Forge/Projects/C++/RandomStuff/random.cpp:74:(.text+0x32f): undefined reference to `crypto_pwhash_str'
E:/Software_Core/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\buiha\AppData\Local\Temp\ccfLFXWD.o: in function `verifyPassword(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&)':
E:/Byte_Forge/Projects/C++/RandomStuff/random.cpp:84:(.text+0x3f5): undefined reference to `sodium_init'
E:/Software_Core/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: E:/Byte_Forge/Projects/C++/RandomStuff/random.cpp:87:(.text+0x46f): undefined reference to `crypto_pwhash_str_verify'
collect2.exe: error: ld returned 1 exit status

Build finished with error(s).

got an error

#

here's what in main.cpp (what i added)

#include <argon2.h>
#include <sodium.h>

std::string hashPassword(const std::string& password) {
    if (sodium_init() == -1) {
        throw std::runtime_error("sodium_init() failed");
    }
    std::string hashedPassword(crypto_pwhash_STRBYTES, '\0');
    if (crypto_pwhash_str(
        (char*)hashedPassword.data(), password.c_str(), password.length(),
        crypto_pwhash_OPSLIMIT_MODERATE, crypto_pwhash_MEMLIMIT_MODERATE
    ) != 0) {
        throw std::runtime_error("Password hashing failed.");
    }
    return hashedPassword;
}

bool verifyPassword(const std::string& password, const std::string& hashedPassword) {
    if (sodium_init() == -1) {
        throw std::runtime_error("sodium_init() failed.");
    }
    if (crypto_pwhash_str_verify(hashedPassword.c_str(), password.c_str(), password.length()) != 0) {
        return false;
    } else {
        return true;
    }
}
meager smelt
#

bruh.

#

CMakeLists still building fine bruh (did the "delete cache and reconfigure" thing)

meager smelt
keen lichen
#
 E:\Software_Core\msys64\ucrt64\bin\g++.exe -fdiagnostics-color=always -g E:\Byte_Forge\Projects\C++\RandomStuff\random.cpp -o E:\Byte_Forge\Projects\C++\RandomStuff\random.exe
#

They should've been here

#

You aren't using cmake at all right now

#

Having the CMakeLists.txt isn't enough, you need to actuall run CMake and tell it to build your program, instead of doing whatever you're doing now 😛

meager smelt
#

So I got a launch.json when switching to CMake debug and idk what to put in scriptPath:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "CMake: CMake Script",
            "type": "cmake",
            "request": "launch",
            "cmakeDebugType": "script",
            "scriptPath": "scriptPath"
        }
    ]
}
keen lichen
#

Maybe forget cmake for now. Do you know how to compile manually in the terminal?

meager smelt
#

yea i do

keen lichen
#

Okay, then do that and add -lsodium -largon2 to the command, that's all you need

meager smelt
#

ok

#

let me try

#

ok it worked so what does this mean

keen lichen
#

So you got no errors? Even when using those libraries in your code?

meager smelt
#

It compiled, i ran the exe, everything through terminal, and it worked smoothly

keen lichen
#

So you're all good 😛

#

You might want to set up "tasks" in VSC so you don't have to type it manually every time

#

Here's a small tutorial

meager smelt
#

ok thank you so much!

#

uhh

#

last thing to do

#

!solved

short scaffoldBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

meager smelt
#

i dont have access to that channel

#

oh and btw i have 1 last question can i just delete build folder and CMakeLists.txt

keen lichen
#

Yeah