Adding Eigen library to c++ project using cmake - c++

Unfortunately, i have some problems adding the headers only Eigen 3.3.7 Library to my Makefile with Cmake on my Ubuntu 18.04.4 LTS system.
I can compile my code using the library by just copying the library folder in the include directory and using
include_directories(./include/eigen3) in the CMakeLists.txt file. However, i would much prefer not having the library path hard-coded but being able set it dynamically in the CMakeLists.txt file to make sharing the project with other people easier. Unfortunately, none of the instructions i found worked for me.
I prepared the following minimum code example:
main.cpp:
#include <eigen3/Eigen/Dense>
#include <iostream>
int main()
{
Eigen::Vector3d test_vec(1.0f, 2.0f, 3.0f);
std::cout << test_vec << std::endl;
return 0;
}
CMakeLists.txt:
cmake_minimum_required(VERSION 3.0)
project(Eigen-Cmake-Test VERSION 1.0) # set the project name
find_package (Eigen3 3.3 REQUIRED NO_MODULE)
include_directories(${EIGEN_INCLUDE_DIR})
# add the executable
add_executable("${PROJECT_NAME}" ./main.cpp)
target_link_libraries("${PROJECT_NAME}" Eigen3::Eigen)
I downloaded the headers only Eigen 3.3.7 Library and renamed the folder to eigen3. The folder was then moved to:
/usr/local/share/eigen3
when i run cmake CMakeLists.txt i get the following error:
CMake Error at CMakeLists.txt:5 (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.
-- Configuring incomplete, errors occurred!
As i checked the Eigen library folder i realized that /usr/local/share/eigen3/cmake only contained a file named Eigen3Config.cmake.in instead of Eigen3Config.cmake which was required. Why is this the case?
I tried renaming the file to Eigen3Config.cmake. Here the error was the following:
CMake Error at CMakeLists.txt:5 (find_package):
Could not find a configuration file for package "Eigen3" that is compatible
with requested version "3.3".
The following configuration files were considered but not accepted:
/usr/local/share/eigen3/cmake/Eigen3Config.cmake, version: unknown
-- Configuring incomplete, errors occurred!
Furthermore, i also tried the solutions explained stackoverflow: CMake find_package not working for Eigen? without success.
How can i make this work properly? Also i think i do not quite understand the underlying system yet. Any explanation would be appreciated.

The problems you are running into are caused by downloading the Eigen source code but not actually building the project. You may think since its a header only library that there is no build step. Well there is; it builds the package config .pc and Eigen3Config.cmake files. One of which you are trying to use.
From my previous comment the file Eigen3Config.cmake.in is a template and will be used to generate the Eigen3Config.cmake which then would be usable.
Its probably easier to install the libeigen3-dev package, it is packaged with /usr/lib/cmake/eigen3/Eigen3Config.cmake. If you insist on using Eigen from source then build and install it
If you DO want to download and install as source maybe take a look at the INSTALL file which has a "Method 2" suggesting how to use it in cmake.

Related

Cmake find_package does not detect path in windows

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.

How can I configure CMake with CGAL?

I am working on the installation of CGAL for my 3D project.
I installed CGAL using the vcpkg package (library) and tried to configure a compiler/linker CMake with CGAL.
Unfortunately I am getting an error to configure CGAL with CMake (see below). How is it possible to link these two together? What does the error mean?
****CMake Error at CMakeLists.txt:22 (find_package):
By not providing "FindCGAL.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "CGAL", but
CMake did not find one.
Could not find a package configuration file provided by "CGAL" with any of
the following names:
CGALConfig.cmake
cgal-config.cmake
Add the installation prefix of "CGAL" to CMAKE_PREFIX_PATH or set
"CGAL_DIR" to a directory containing one of the above files. If "CGAL"
provides a separate development package or SDK, be sure it has been
installed.****
Configuring incomplete, errors occurred!
See also "C:/Users/Desktop/CGAL-5.0.2/examples/Triangulation_2/build/CMakeFiles/CMakeOutput.log".

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 compile fails for project synergy-through-usb on windows

I am trying to build the following c++ project from github on windows 64bit system:
synergy-through-usb.
It says I need to use cmake, so I have installed cmake latest version.
I then downloaded and unzipped the source code.
Then I ran cmake . in the folder, but I got the following error:
C:\local\projects\synergy-usb\synergy-through-usb-master>cmake .
You have called ADD_LIBRARY for library cryptopp without any source files. This typically indicates a problem with your CMakeLists.txt file
CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
LIBUSB_1_INCLUDE_DIR
used as include directory in directory C:/local/projects/synergy-usb/synergy-through-usb-master/src/lib/arch
used as include directory in directory C:/local/projects/synergy-usb/synergy-through-usb-master/src/lib/net
LIBUSB_1_LIBRARY
linked by target "arch" in directory C:/local/projects/synergy-usb/synergy-through-usb-master/src/lib/arch
-- Configuring incomplete, errors occurred!
See also "C:/local/projects/synergy-usb/synergy-through-usb-master/CMakeFiles/CMakeOutput.log".
I am not familiar with cmake and also the links to the instructions / guide for this git repo seem to be removed:
http://synergy-project.org/projects/synergy/wiki/Readme/?hl=pm
http://synergy-project.org/projects/synergy/wiki/Compiling/?hl=pm
Can anyone help me compile this - i.e. suggest what I need to do to get around these errors? or has anyone tried to use this project?

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 ..