Cmake find_package does not detect path in windows - c++

I am trying to build the ceres sovler using Cmake_gui and Visual Studio
I have ceres-solver cloned to E:\Code\libs\ceres-solver
and the required library Eigen3 cloned to E:\Code\libs\eigen-3.3.9
When I run cmake-gui on ceres-solver, I get the following error:
-- Detected available Ceres threading models: [CXX_THREADS, OPENMP, NO_THREADS]
-- Building with C++14
CMake Error at CMakeLists.txt:242 (find_package):
By not providing "FindEigen3.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "Eigen3", but
CMake did not find one.
Could not find a package configuration file provided by "Eigen3" (requested
version 3.3) with any of the following names:
Eigen3Config.cmake
eigen3-config.cmake
Add the installation prefix of "Eigen3" to CMAKE_PREFIX_PATH or set
"Eigen3_DIR" to a directory containing one of the above files. If "Eigen3"
provides a separate development package or SDK, be sure it has been
installed.
I modified ceres CMakeLists.txt line 241
from:
find_package(Eigen3 3.3 REQUIRED)
to
find_package(Eigen3 3.3 REQUIRED PATH "E:/Code/libs/eigen-3.3.9")
But I still get this error above
I also tried
list(APPEND CMAKE_PREFIX_PATH "E:/Code/libs/eigen-3.3.9")
set(Eigen3_DIR "E:/Code/libs/eigen-3.3.9")
My questions are:
What is the correct way to specify the path for find_package? Do I need to use path environment variable within windows or modify CMakeLists.txt?
Am I specifying this path correctly in Windows? Do I need to link to some internal directory of Eigen3? Like E:\Code\libs\eigen-3.3.9\cmake, use forward slashes instead of back slashes, or use quotation marks for the path? I tried all of these things without success.
Does find_package recursively search for the package within the directories specified? Or do I need to point to the exact directory?
Thanks

According to comments:
I must run Cmake-Gui on Eigen3 first, to generate a Build directory. However, it does not necessarily need to be built in VS afterwards,
Then setting on line 240 in CMakeLists.txt for ceres-solver
list(APPEND CMAKE_PREFIX_PATH "E:/Code/libs/eigen-3.3.9/build")
Was sufficient for Cmake-Gui to find the Eigen3Config.cmake file
Notes:
Eigen3 does not have any required dependencies it seems, but it does throw a lot of warnings when generating with Cmake-Gui, I ignored these
glog library or any other libraries are not required, but I did have to check "Enable mini-glog" option and re-run config for ceres-solver.

Related

Setting up a Vulkan project with CMake on Windows

Until this point I have installed MinGW, CMake, and the Vulkan SDK. I also downloaded the GLFW precompiled binaries, GLM and PkgConfig according to this answer. Then I created a CMake project in CLion. This is the content of the CMakeLists.txt (which I got from here):
cmake_minimum_required(VERSION 3.16)
project(VulkanTest)
set(CMAKE_CXX_STANDARD 17)
add_executable(VulkanTest main.cpp)
find_package(Vulkan REQUIRED)
target_include_directories(${PROJECT_NAME} PUBLIC ${Vulkan_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} Vulkan::Vulkan)
find_package(PkgConfig REQUIRED)
pkg_search_module(GLM REQUIRED glm)
include_directories(${GLM_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} ${GLM_LIBRARY_DIRS})
find_package(glfw3 3.2 REQUIRED)
include_directories(${GLFW_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} ${GLFW_LIBRARIES})
The error message is as follows:
CMake Error at CMakeLists.txt:15 (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.2) 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 also tried replacing find_package(glfw3 3.2 REQUIRED) with pkg_search_module(GLFW REQUIRED glfw3) as described on the GLFW website, but I get the errors "None of the required 'glfw3' found" and "None of the required 'glm' found".
First, the question, as this is likely what the all of the people visiting this thread are interested in. The problem demonstrated in the post seems very different from the question others probably want answered.
Answer For Visitors: You need to do three things in order to link with the Vulkan library on Windows when using cmake.
set(ENV{VULKAN_SDK} "Path/To/Vulkan/Version/Installation")
find_package(Vulkan REQUIRED)
target_link_libraries(target ${Vulkan_LIBRARIES})
The path should reference the specific version of Vulkan you are using. For me, this is C:/VulkanSDK/1.2.198.1, but it will be different for you depending on where Vulkan is installed and the version you want to use.
Don't forget to also add an include directory with something along the lines of target_include_directories(target PUBLIC "C:/VulkanSDK/1.2.198.1/Include") to avoid using absolute include paths for Vulkan headers in your code.
Explanation: The find_package command will search through a directory within your cmake installation for details about the package. For me, this directory is <cmake_install_dir>/share/cmake-3.18/Modules (3.18 should be replaced with the version you have installed.) In this directory you'll find a nice chunk of files named Find<PackageName>.cmake and FindVulkan.cmake should be among them. This file is what find_package is running under the hood. You'll notice a few instances of $ENV{VULKAN_SDK} in that file. This is why that VULKAN_SDK environment variable must be set before calling find_package. Cmake will throw errors if it isn't.
lizardsudoku's Problem (even though you probably already figured it out): As explained above, cmake is expecting to find a Findglfw3.cmake entry in that Modules directory and it didn't. Instead of creating one of those files yourself, it's easier to specify the glfw3 library directly in your CMakeLists.txt file like so.
list(APPEND CMAKE_PREFIX_PATH "path/to/lib/directory")
find_library(glfw NAMES glfw3 REQUIRED)
target_link_libraries(target ${glfw})
As someone mentioned, you want to make use of CMAKE_PREFIX_PATH to specify the directory the .lib is in. The find_library call can then search that directory for the glfw3.lib entry before it is specified as a linker input. Even though CMAKE_PREFIX_PATH affects what directories are searched through when find_package is used, the package file does not exist, hence the error does not change.

CMake is having problems to compile a project which uses OpenCV

I'm on Linux Mint 19.1 Tessa.
I followed the instructions described here to install OpenCV. Now I have it installed at the directory "/home/dell/opencv".
I tryed to run the example project located at "/home/dell/opencv/samples/cpp/example_cmake/" by running the command "cmake ." on terminal and I got the following error:
CMake Error at CMakeLists.txt:14 (find_package):
By not providing "FindOpenCV.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "OpenCV", but
CMake did not find one.
Could not find a package configuration file provided by "OpenCV" with any
of the following names:
OpenCVConfig.cmake
opencv-config.cmake
Add the installation prefix of "OpenCV" to CMAKE_PREFIX_PATH or set
"OpenCV_DIR" to a directory containing one of the above files. If "OpenCV"
provides a separate development package or SDK, be sure it has been
installed.
-- Configuring incomplete, errors occurred!
See also "/home/dell/opencv/samples/cpp/example_cmake/CMakeFiles/CMakeOutput.log".
The "CMakeLists.txt" file contains the following:
# cmake needs this line
cmake_minimum_required(VERSION 3.1)
# Enable C++11
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
# Define project name
project(opencv_example_project)
# Find OpenCV, you may need to set OpenCV_DIR variable
# to the absolute path to the directory containing OpenCVConfig.cmake file
# via the command line or GUI
find_package(OpenCV REQUIRED)
# If the package has been found, several variables will
# be set, you can find the full list with descriptions
# in the OpenCVConfig.cmake file.
# Print some message showing some of them
message(STATUS "OpenCV library status:")
message(STATUS " config: ${OpenCV_DIR}")
message(STATUS " version: ${OpenCV_VERSION}")
message(STATUS " libraries: ${OpenCV_LIBS}")
message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
# Declare the executable target built from your sources
add_executable(opencv_example example.cpp)
# Link your application with OpenCV libraries
target_link_libraries(opencv_example LINK_PRIVATE ${OpenCV_LIBS})
I've searched a lot through the internet and it seems to be a common problem. However, I still didn't manage to solve it by following the intructions that I've found.
One thing that I notice when going through the OpenCV folder is that the version that I have (which I think it's the most recent one) indeed doesn't contain any "OpenCVConfig.cmake" file, as you can see here. However, an oldest version of OpenCV that I found on github have this file, as you can see here.
So, maybe some configuration is set to this oldest version and it's causing conflict? How to change that and get it working for the newest version? I think it must be something simple to solve but I'm quite newbie.
Thanks in advance.
CMake writes you what to do:
Add the installation prefix of "OpenCV" to CMAKE_PREFIX_PATH or set
"OpenCV_DIR" to a directory containing one of the above files.
Another option - please, read comments in the file CMakeLists.txt that you've pasted, once more.
They say:
you may need to set OpenCV_DIR variable
to the absolute path to the directory containing OpenCVConfig.cmake
Another issue - in-source build, performed with passing dot to cmake. It is better to create separate directory for build files.
Building this way allows you easily adding your build directory to .gitignore and not polluting your source code repository with compiled binaries. Also, if something goes wrong, you can just remove that build directory and start again from scratch. That is much easier than messing with files one by one.
So, you need to call CMake like following
mkdir build
cd build
OpenCV_DIR=/home/dell/opencv cmake ..
or
mkdir build
cd build
cmake .. -DCMAKE_PREFIX_PATH=/home/dell/opencv
or the following also should do the trick
mkdir build
cd build
cmake .. -DOpenCV_DIR=/home/dell/opencv
Update your OpenCV search directory path as below,
find_package(OpenCV 3.4 REQUIRED PATHS "/usr/local/opencv/" NO_DEFAULT_PATH)

CMake "find_package" command on a package that was not installed is unexpectedly successful

I am following chapter-02/recipe-06 in "CMake Cookbook". This particular example requires the Eigen C++ libraries.
I attempted to build the example and got the error that Eigen was not found.
CMake Error at CMakeLists.txt:9 (find_package):
Could not find a package configuration file provided by "Eigen3" (requested
version 3.3) with any of the following names:
Eigen3Config.cmake
eigen3-config.cmake
Add the installation prefix of "Eigen3" to CMAKE_PREFIX_PATH or set
"Eigen3_DIR" to a directory containing one of the above files. If "Eigen3"
provides a separate development package or SDK, be sure it has been
installed.
This was expected because the library was not installed on my system.
I then downloaded the ".zip" file for the Eigen libraries and unzipped it to an arbitrary location outside of my project. I created a "build" folder in the Eigen directory and ran cmake .. in the "build" folder. (I only ran cmake - I did NOT build or install the package.)
After running CMake on the Eigen build directory, I went back to the example code for "recipe-06" and it was magically able to find the Eigen library and built successfully even though Eigen was never built or installed.
Somehow just running CMake in the Eigen project made CMake aware of the Eigen libraries location. After doing this, any projects that do find_package to find Eigen3 somehow get the ${Eigen3_DIR} variable defined and are able to find it.
Looking at the CMake documentation for find_package I don't see any explanation of why this works. Eigen is not in any of the typical locations that find_package searches. According to the documentation it looks like it should NOT be found.
Even more interesting - it doesn't matter where I put Eigen on my system. I can put it literally anywhere and it will still find it.
According to everything I see in the documentation it should not be found... but it is found. So the question is how? Why does this work?
Additional info: I am using CMake version 3.13.3
There are 2 "origins" of XXXConfig.cmake files, used internally by find_package() call.
Usually, XXXConfig.cmake file is produced when the project is installed, and the file contains information about installed libraries and headers.
But CMake provides also an export() command, which allows to export build tree.
export(PACKAGE <name>)
Store the current build directory in the CMake user package registry for package <name>. The find_package command may consider the directory while searching for package <name>.
Eigen's CMakeLists.txt uses export() command, so the project becomes detectable with find_package just after running cmake for Eigen.

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

CMake find_package not finding Find<package>.cmake

I made and installed the aruco library, which put a Findaruco.cmake file in the /usr/local/lib/cmake directory. In my CMakeLists.txt file I have
...
find_package(aruco REQUIRED)
and it always returns the standard error
By not providing "Findaruco.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "aruco", but
CMake did not find one.
Could not find a package configuration file provided by "aruco" with any of
the following names:
arucoConfig.cmake
aruco-config.cmake
Add the installation prefix of "aruco" to CMAKE_PREFIX_PATH or set
"aruco_DIR" to a directory containing one of the above files. If "aruco"
provides a separate development package or SDK, be sure it has been
installed.
I've set the environment variable $CMAKE_PREFIX_PATH to each of the following, and none work
/usr/local
/usr/local/lib
/usr/local/lib/cmake
The only thing that works is setting the following in CMakeLists
set(CMAKE_MODULE_PATH /usr/local/lib/cmake)
I'm not sure what I'm doing wrong
Try setting CMake variable called CMAKE_PREFIX_PATH, not the environment one. Use -D flag during cmake invocation:
cmake -D CMAKE_PREFIX_PATH=/usr/local/lib <path to source or build dir>
But AFAIR, CMake should look into /usr/local prefix as its default behavior.