简体   繁体   中英

Including libsodium in a CMakeLists.txt file

I currently have libsodium installed and working, ie If I run gcc -lsodium foo.c -o foo from my command line, the compiler successfully compiles and links the executable. However, I don't know how to include libsodium in my CMakeLists.txt file and although I read the documentation, I don't know how to follow the instructions. Verbatim, the instructions from the docs are:

"Projects using CMake can include the Findsodium.cmake file from the Facebook Fizz project in order to detect and link the library."

I found the Findsodium.cmake file on github ( https://github.com/facebookincubator/fizz/blob/main/build/fbcode_builder/CMake/FindSodium.cmake ) but I don't know how to "include" it in my CMakeLists.txt and I haven't found any help anywhere on previous stackoverflow questions or on the docs anywhere.

The line in my CMakeLists.txt file which is throwing the error is:

find_package(sodium REQUIRED) and the error is as follows:

CMake Error at CMakeLists.txt:5 (find_package):By not providing "Findsodium.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "sodium", but
CMake did not find one.

Could not find a package configuration file provided by "sodium" with any
of the following names:

  sodiumConfig.cmake
  sodium-config.cmake

Add the installation prefix of "sodium" to CMAKE_PREFIX_PATH or set
"sodium_DIR" to a directory containing one of the above files.  If "sodium"
provides a separate development package or SDK, be sure it has been
installed.

Any help on fixing this error and successfully helping me include libsodium in my project would be much appreciated. I'm relatively new to CMake so forgive the ignorance if this has an obvious fix. If you answer or attempt to answer this question, thank you for your time.

You need to install pkg-config on any Unix like system such as Linux. Sodium does not properly support detection via cmake without pkg-config to identify it's library and include paths.

On Debian or Ubuntu:

apt install pkg-config

You can see the checks looking for this on line 52 of FindSodium.cmake: https://github.com/facebookincubator/fizz/blob/a009078d01f476c19a05d3ebc74055a64563d33a/build/fbcode_builder/CMake/FindSodium.cmake#L52

if (UNIX)
    # import pkg-config
    find_package(PkgConfig QUIET)
    if (PKG_CONFIG_FOUND)
        pkg_check_modules(sodium_PKG QUIET libsodium)
    endif()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM