How can I use mbedtls in CMake? - c++

I'm trying to use mbedtls in my CMakelists.txt file. I used the vcpkg package manager to install mbedtls: vcpkg install mbedtls
I added my find_package() statement before add_executable() but despite adding -DCMAKE_TOOLCHAIN_FILE=/bin/vcpkg/scripts/buildsystems/vcpkg.cmake to my CLion CMake options, CLion does not find mbedtls.
CMakeLists.txt:
cmake_minimum_required(VERSION 3.22)
project(mbedtls_ssl_server)
set(CMAKE_CXX_STANDARD 20)
find_package(mbedtls REQUIRED)
add_executable(mbedtls_ssl_server
main.cpp)
target_link_libraries(mbedtls_ssl_server mbedtls::mbedtls)
The error message is:
CMake Error at /bin/vcpkg/scripts/buildsystems/vcpkg.cmake:838 (_find_package):
By not providing "Findmbedtls.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "mbedtls", but
CMake did not find one.
Could not find a package configuration file provided by "mbedtls" with any
of the following names:
mbedtlsConfig.cmake
mbedtls-config.cmake
Add the installation prefix of "mbedtls" to CMAKE_PREFIX_PATH or set
"mbedtls_DIR" to a directory containing one of the above files. If
"mbedtls" provides a separate development package or SDK, be sure it has
been installed.
I also tried running sudo apt install libmbedtls-dev on Ubuntu but that didn't help either, the previous error persists.

I had the same error and i had to set the variable MbedTLS_DIR in CMakelists.txt.
Exemple for MacOS:
set(MbedTLS_DIR /opt/homebrew/Cellar/mbedtls/3.3.0/cmake/) # <-- Here
find_package(MbedTLS REQUIRED )
target_link_libraries(${target_lib} PUBLIC MbedTLS::mbedtls)
This point on folder that contain MbedTLSConfig.cmake

Related

CMake error while using vcpkg and find_package() "configuration file was considered but not accepted"

I'm trying to add the header-only library frugally-deep into my CMake project using vcpkg and the findpackage() command. I installed the frugally-deep package and its dependencies (Eigen3, nlohmannjson and FunctionalPlus) beforehand with the vcpkg install [package_name] command.
My CMake file currently looks like this:
cmake_minimum_required(VERSION 3.22)
project(untitled)
set(CMAKE_CXX_STANDARD 17)
find_package(Eigen3 CONFIG REQUIRED)
find_package(nlohmann_json CONFIG REQUIRED)
find_package(FunctionalPlus CONFIG REQUIRED)
find_package(frugally-deep CONFIG REQUIRED)
add_executable(untitled main.cpp)
I added the directories of the packages to the CMake arguments:
-DEigen3_DIR=C:\vcpkg\packages\eigen3_x86-windows\share\eigen3
-Dnlohmann_json_DIR=C:\vcpkg\packages\nlohmann-json_x86-windows\share\nlohmann_json
-DFunctionalPlus_DIR=C:\vcpkg\packages\fplus_x86-windows\share\FunctionalPlus
-Dfrugally-deep_DIR=C:\vcpkg\packages\frugally-deep_x86-windows\share\frugally-deep
CMake was able to find the packages for Eigen3, nlohmannjson and FunctionalPlus. But when I try to add the frugally-deep package I get the following error message:
CMake Error at CMakeLists.txt:9 (find_package):
Could not find a configuration file for package "frugally-deep" that is
compatible with requested version "".
The following configuration files were considered but not accepted:
C:/vcpkg/packages/frugally-deep_x86-windows/share/frugally-deep/frugally-deepConfig.cmake, version: 0.15.19 (32bit)
How do I resolve this issue?
Wild guess: You configured CMake to use x64 instead of x86. As such you cannot use the x86-windows triplet. Also you shouldn't use the packages folder. That folder is just a staging area for vcpkg. Consider using vcpkg.cmake using a manifest (vcpkg.json) to avoid such problems in the future.

cmake Could not find a package configuration file provided by "glfw3"

I am trying to add glfw to my c++ project. I am using cmake and CLion. I am getting the error:
CMake Error at CMakeLists.txt:11 (find_package):
By not providing "Findglfw3.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "glfw3", but
CMake did not find one.
Could not find a package configuration file provided by "glfw3" (requested
version 3.3) with any of the following names:
glfw3Config.cmake
glfw3-config.cmake
Add the installation prefix of "glfw3" to CMAKE_PREFIX_PATH or set
"glfw3_DIR" to a directory containing one of the above files. If "glfw3"
provides a separate development package or SDK, be sure it has been
installed.
I downloaded glfw using homebrew (brew install glfw) and put the downloaded contents in my project. My project structure looks like this
project structure
my CMakeLists.txt looks like this
cmake_minimum_required(VERSION 3.20)
project(open_gl)
set(CMAKE_CXX_STANDARD 14)
add_executable(open_gl src/Application.cpp)
list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")
# glfw
find_package(glfw3 3.3 REQUIRED)
target_link_libraries(open_gl glfw)
Does anybody know what I am doing wrong? Might just be making some beginner mistake but I have been at this for way too long now.

CMake file cannot locate Qt charts module

I'm trying to start writing my Qt project inside JetBrains' Clion but I need to link some libraries in my Cmake file first. There's no problem when trying to find packages like Qt5Core, Qt5Widgets, Qt5Gui but when it come to finding Qt5Charts an error is thrown:
By not providing "FindQt5Charts.cmake" in CMAKE_MODULE_PATH this project has asked CMake to find a package configuration file
provided by "Qt5Charts", but CMake did not find one.
Could not find a package configuration file provided by "Qt5Charts"
with any of the following names:
Qt5ChartsConfig.cmake
qt5charts-config.cmake
Add the installation prefix of "Qt5Charts" to CMAKE_PREFIX_PATH or
set "Qt5Charts_DIR" to a directory containing one of the above
files. If "Qt5Charts" provides a separate development package or
SDK, be sure it has been installed.
This is my CMake file right now.
All packages are installed via the Qt's Linux(ubuntu) maintanence tool.
Any ideas how to help Cmake find the Charts module ?
Using the following and see if it helps:
sudo apt install libqt5charts5-dev
Src: https://stackoverflow.com/a/46765025
Typically when including Qt5 in a project I use the follow basic script for CMake, though I should note I haven't tested this on Linux.
cmake_minimum_required(VERSION 3.10.0 FATAL_ERROR)
project(<YOUR_PROJECT_NAME>)
find_package(Qt5 REQUIRED COMPONENTS Core Gui Widgets Charts)
# set your project sources and headers
set(project_sources src/blah.cpp)
set(project_headers include/headers/blah.h)
# wrap your qt based classes with qmoc
qt5_wrap_cpp(project_source_moc ${project_headers})
# add your build target
add_executable(${PROJECT_NAME} ${project_sources} ${project_headers} ${project_source_moc})
# link to Qt5
target_link_libraries(${PROJECT_NAME}
PUBLIC
Qt5::Core
Qt5::Gui
Qt5::Widgets
Qt5::Charts)
# require C++ 14
target_compile_features(${PROJECT_NAME} PUBLIC cxx_std_14)
When configuring your project via cmake, you just need to pass in the path to you qt5 installation directory (cmake variable name is Qt5_DIR) that contains the Qt5Config.cmake file and then cmake should be able to find the rest of the components that you request.
Also double check that Qt5Charts was installed, not sure if it's installed by default.
Maybe try this?
sudo apt install libqt5charts5-dev

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

Include cpp-netlib with CLion and Make on macOS

I'm very new to CLion, CMake and macOS at all so I need a little bit of help while trying to include cppnetlib into my CLion project.
I started out with creating an new CLion project which gave me a new CMakeLists.txt which I edited so that it would include boost as well:
CMAKE_MINIMUM_REQUIRED(VERSION 3.7)
PROJECT(myapp)
SET(CMAKE_CXX_STANDARD 11)
SET(SOURCE_FILES main.cpp)
ADD_EXECUTABLE(myapp ${SOURCE_FILES})
FIND_PACKAGE(Boost 1.63.0 REQUIRED)
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIR})
TARGET_LINK_LIBRARIES(dxcheck ${Boost_LIBRARIES})
I've installed boost through homebrew, but I did not find a package for cppnetlib with homebrew so I downloaded cppnetlib and placed it in /usr/local/Cellar/cpp-netlib. Now, when I follow the tutorial on their page and put
set (CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} /usr/local/Cellar/cpp-netlib)
find_package (cppnetlib 0.12.0 REQUIRED)
include_directories (${CPPNETLIB_INCLUDE_DIRS})
target_link_libraries (myapp ${CPPNETLIB_LIBRARIES})
into the CMakeLists.txt, I get the following output:
-- Boost version: 1.63.0
CMake Error at CMakeLists.txt:14 (find_package):
By not providing "Findcppnetlib.cmake" in CMAKE_MODULE_PATH this project
has asked CMake to find a package configuration file provided by
"cppnetlib", but CMake did not find one.
Could not find a package configuration file provided by "cppnetlib"
(requested version 0.11.0) with any of the following names:
cppnetlibConfig.cmake
cppnetlib-config.cmake
Add the installation prefix of "cppnetlib" to CMAKE_PREFIX_PATH or set
"cppnetlib_DIR" to a directory containing one of the above files. If
"cppnetlib" provides a separate development package or SDK, be sure it has
been installed.
I've already done some research about finding packages but all I could achieve was that I was able to include cppnetlib by manually adding the include path to the CMakeLists.txt but then I got an error saying that the compiler could not find the headers which are located inside the "deps" folder in cpp-netlib.
I know want to know how to include a package like cppnetlib properly into my CLion project using CMake. I am using CLion 2017.1, macOS Sierra and I think CLion is using CMake 3.7.