Including libsodium in a CMakeLists.txt file - c++

I currently have libsodium installed and working, i.e. 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()

Related

Cannot open source file error with vcpkg on vscode mac

I'm trying to use external libraries (specifically opencv) with C++ but I'm having some trouble. It seems like using vcpkg is the easiest way to go about this. So far I've followed these tutorials: https://www.youtube.com/watch?v=b7SdgK7Y510 and https://www.youtube.com/watch?v=iZeK3Ie5Fz0 but I keep getting these errors when I try to include a file:
cannot open source file "opencv2/core.hpp"
'opencv2/core.hpp' file not found
The quick fix provided by vscode says to install opencv through vcpkg but I've already done that. I've linked vcpkg according to the tutorials and included the path to the cmake toolchain file in my settings.json file.
Here is the change I made to my settings.json file:
{
"cmake.configureSettings": {
"CMAKE_TOOLCHAIN_FILE": "/Users/oliverpasquesi/coding/dev/vcpkg/scripts/buildsystems/vcpkg.cmake"
}
}
Here is the CMakeLists.txt file (it is the same as the one from the 2nd tutorial listed):
cmake_minimum_required(VERSION 3.0.0)
project(photoText.cpp VERSION 0.1.0)
add_executable(photoText.cpp main.cpp)
Here is the includePath portion of the c_cpp_properties.json file (including the 2nd path doesn't make any difference in the errors being thrown):
"includePath": [
"${default}",
"/Users/oliverpasquesi/coding/dev/vcpkg/installed/x64-osx/include/opencv2/**"
]
If it's useful, I'm using vscode 1.49.2 on a mac running Darwin x64 21.5.0.
I appreciate the help!
You need to reference your library in the CMakeLists.txt file as well, since that's the file CMake uses to prepare your project for a build.
A modern way to do this is to add two lines:
A find_package() line to find the libraries you are using. As you already referenced the vcpkg.cmake toolchain file, CMake should be able to find any libraries already installed by vcpkg when you add that line. See https://cmake.org/cmake/help/latest/command/find_package.html for documentation.
A target_link_libraries() line that lists libraries to link against your project. See the CMake documentation here: https://cmake.org/cmake/help/latest/command/target_link_libraries.html
As you are using the opencv library, you may want to look at the answers to this question as well: Linking Opencv in a project using cmake
Specifically, try adding these lines to your CMakeLists.txt:
find_package(OpenCV REQUIRED)
target_link_libraries(photoText.cpp ${OpenCV_LIBS})
vcpkg documentation on its CMake integration (for additional reference): https://vcpkg.io/en/docs/users/buildsystems/cmake-integration.html
Hopefully this helps!
Disclaimer: I work on the vcpkg tool.

Build uWebSockets on Windows 10 with CMake

I want to use uWebSockets(UWS) in my C++ project to transfer images over the network. The setup will be running on multiple operating systems, so the best way of creating the build files seemed like using CMake.
However, I am new to CMake and having a hard time building UWS. I don't understand how to start working with the official repository on Windows 10, so I found another repository that includes a CMakeFiles.txt file and some of the dependencies (openssl, zlib, libuv, but it doesn't include uSockets for some reason). The root CMakeFiles.txt includes:
[...]
find_package(OpenSSL REQUIRED)
find_package(ZLIB REQUIRED)
find_path(LIBUV_INCLUDE_DIR uv.h)
find_library(LIBUV_LIBRARY NAMES uv uv1)
[...]
It looks straightforward, but when I try to run mkdir build && cd build; cmake .., it cannot find OpenSSL. Here is the error message it spits out:
[...]
[cmake] Could not find a package configuration file provided by "OpenSSL" with any
[cmake] of the following names:
[cmake]
[cmake] OpenSSLConfig.cmake
[cmake] openssl-config.cmake
[...]
The above error message suggests that I need to set up a config file for each of these libraries. Yet, if I understand the find_package docs correctly, the command itself searches the library in various locations under the root folder. What kind of a folder structure does the find_package need in order to work?
More generally, am I wasting my time with this alternative repo? Is there a better way of using UWS with Windows 10? The official repo has a question about how to use it on Windows but I don't understand how that's an answer to the question. It only points to this page where it says any specific build systems will not officially be supported.
Any help would be appreciated.
Importing dependencies with add_subdirectory seems like a good way around this. When I ran cmake, I was receiving LNK2019 Error. I realized the following code snippet I found online was causing the problem, and the system works when I delete it.
if(MSVC)
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} /NODEFAULTLIB:libcmt.lib /NODEFAULTLIB:msvcrt.lib")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /SUBSYSTEM:WINDOWS")
endif()

CMake find_package for FindLibXml2

I'm trying to create a CMake file that will detect the location of libxml2. From what see in examples and CMake documentation the find_package simply works. I'm running CLion on Ubuntu, the libxml2 is installed using apt-get, the FindLibXml2.cmake is located under CMake's modules. However CMake returns cryptic message:
Could not find a package configuration file provided by "FindLibXml2"
with any of the following names:
FindLibXml2Config.cmake
findlibxml2-config.cmake
Add the installation prefix of "FindLibXml2" to CMAKE_PREFIX_PATH or
set "FindLibXml2_DIR" to a directory containing one of the above
files. If "FindLibXml2" provides a separate development package or
SDK, be sure it has been installed.
Why it is trying to find this -config file? what I'm doing wrong?
CMake snippet
find_package(FindLibXml2 CONFIG REQUIRED)
I've also tried
find_package(FindLibXml2 REQUIRED)
Not sure which one to use
You should not have the Find in FindLibXml2; do:
find_package(LibXml2 REQUIRED)
As explained in the documentation:
CMake searches for a file called Find<package>.cmake

How to know variable such as 'OpenCV' in CMake

I am using OpenCV with gcc and cmake. And I found a tutorial https://docs.opencv.org/3.4.0/db/df5/tutorial_linux_gcc_cmake.html .In the file CMakeLists.txt, there are some variables such as OpenCV and OpenCV_INCLUDE_DIRS.
cmake_minimum_required(VERSION 3.9)
project(VideoRecord)
set(CMAKE_CXX_STANDARD 11)
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(VideoRecord main.cpp)
target_link_libraries(VideoRecord ${OpenCV_LIBS})
I want to know where to find these variables definition.
EDIT
Thanks #qbranchmaster's answer. I tried to search FindOpenCV.cmake but failed.
First try.
➜ ~ cmake --help-module-list | grep "FindOpen"
FindOpenACC
FindOpenAL
FindOpenCL
FindOpenGL
FindOpenMP
FindOpenSSL
FindOpenSceneGraph
FindOpenThreads
Another try.
➜ / find . "FindOpenCV.cmake"
In addition, my os is osx and I install cmake with brew. I comiple and install OpenCV manually.
These variables are part of the package config script shipping with OpenCV.
Note that find_package is a two-headed beast. The classic mode of operation is finding libraries through find-scripts. This is still the approach being used today for third-party libraries that are not aware of CMake. However, if your dependency is itself being built with CMake, it can provide a package config file instead, which allows for a more powerful mode of operation.
The idea here is that instead of you telling CMake how to find a dependency, the dependency itself tells CMake how clients can find it. This is the approach that is taken by libraries like OpenCV and Qt.
To answer your question, those variables are being set by the package config file in your local OpenCV installation, the template of which can be found in the OpenCV source code under cmake/templates/OpenCVConfig.cmake.in.
They are defined in CMake OpenCV module. CMake has numerous modules that aid in finding various libraries like OpenCV (FindOpenCV.cmake module).
Using this command you can get a list of modules that your CMake supports:
cmake --help-module-list
Some libraries come with their own *.cmake modules which should be installed in some system path. If you are using Ubuntu, your cmake modules should be localised in:
/usr/share/cmake/Modules/
If not, just search system for file FindOpenCV.cmake. In that file you will find these variables.
In general, you get variable names from the documentation or source code of the package you want to find.
Often you can derive the name to put into find_package from the provided FindFoo.cmake module file name, because "Foo" would be the name. The find module is either part of CMake or comes with the third-party library.
If there is no find module, some modules provide FooConfig.cmake files, where "Foo" is again the string to put into find_package.
If you have neither a find nor a config file, you need to find the library by other means, e.g., FindPkgConfig or find_library / find_file.

CMake Error at CMakeLists.txt:3 (find_package)

I'm trying to control raspberry pi camera via c++. I found api (raspicam), installed, checked. It works with an example provided by developer. Now I create my own project and got an error of CMakeLists:
CMake Error at CMakeLists.txt:3 (find_package):
By not providing "Findraspicam.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "raspicam",
but CMake did not find one.
Could not find a package configuration file provided by "raspicam" with any
of the following names:
raspicamConfig.cmake
raspicam-config.cmake
Add the installation prefix of "raspicam" to CMAKE_PREFIX_PATH or set
"raspicam_DIR" to a directory containing one of the above files. If
"raspicam" provides a separate development package or SDK, be sure it has
been installed.
-- Configuring incomplete, errors occurred!
See also "/home/pi/raspicam/build/CMakeFiles/CMakeOutput.log".
And it is my CmakeLists.txt file:
make_minimum_required (VERSION 2.8)
project (raspicam_test)
find_package(raspicam REQUIRED)
add_executable (simpletest_raspicam simpletest_raspicam.cpp)
target_link_libraries (simpletest_raspicam ${raspicam_LIBS})
I just have started and already got a problem... Could you help please guys? :/
For those who used default instructions in the readme file of the raspicam (as of v 0.1.3, might differ in future):
you can also add
set(raspicam_DIR "/usr/local/lib/cmake")
to your cmake file before you use
find_package(raspicam REQUIRED)
That is when you have raspicamConfig.cmake at /usr/local/lib/cmake. If you did not have it at that directory, you can simply search your raspberry to find where it is or you can go to "build" folder in raspicam you used to install before. Then type sudo make install again, which will return "Up-to-date" message along with all a list of files and their locations, including raspicamConfig.cmake. Hope this helps to some.
This works for me.
$ brew install vcpkg
$ vcpkg install raspicam
$ vcpkg integrate install # get path
$ cmake -DCMAKE_TOOLCHAIN_FILE=/usr/local/Cellar/vcpkg/2021.05.12/libexec/scripts/buildsystems/vcpkg.cmake ..