Unable to find Eigen3 with CMake - c++

I am kind of desperate:
For my studies I need to work with Eigen and CMake. I'm able to use Eigen if I copy the whole library in the directories where my compiler looks by default but as soon as I try to find it via
find_package(Eigen3 REQUIRED)
I get the following error:
CMake Error at /usr/share/cmake/Modules/FindPackageHandleStandardArgs.cmake:148 (message):
Could NOT find Eigen3 (missing: EIGEN3_INCLUDE_DIR EIGEN3_VERSION_OK)
(Required is at least version "2.91.0")
Call Stack (most recent call first):
/usr/share/cmake/Modules/FindPackageHandleStandardArgs.cmake:388 (_FPHSA_FAILURE_MESSAGE)
FindEigen3.cmake:76 (find_package_handle_standard_args)
CMakeLists.txt:8 (find_package)
-- Configuring incomplete, errors occurred!
Now I searched for solutions but all I those I tried (also those available on stackoverflow:
Find package Eigen3 for CMake
or
CMake Can't find Eigen3 )
did not work.
My Eigen Version (according to the Macros in Core/util/Macros.h) is 3.2.5.
I keep the Eigen directory in /usr/local/include, I use the FindEigen3.cmake which comes with the Eigen library and my CMakeLists.txt looks as follows:
cmake_minimum_required(VERSION 2.8)
project(Test)
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR})
find_package(Eigen3 REQUIRED)
include_directories(${EIGEN3_INCLUDE_DIR})
message("Found Eigen3 in: ${EIGEN3_INCLUDE_DIR}")
add_executable(main test.cpp)
Has anyone an idea what's going wrong?
Kind regards,
Julien

Turning my comment into an answer
The find package scripts - like FindEigen3.cmake - normally use the find_path() command to detect the package's include directory (see it's documentation for the full details).
FindEigen3.cmake uses the following code snippet:
find_path(EIGEN3_INCLUDE_DIR NAMES signature_of_eigen3_matrix_library
PATHS
${CMAKE_INSTALL_PREFIX}/include
${KDE4_INCLUDE_DIR}
PATH_SUFFIXES eigen3 eigen
)
So it looks in CMAKE_INSTALL_PREFIX which on Unix/Linux hosts is /usr/local by default.
The following has worked for me:
Go to the Eigen source directory and run the CMake and installation steps
> mkdir build
> cd build
> cmake ..
> make install
Then copy - as you have done - FindEigen3.cmake to your projects source directory.
Now your code does find Eigen (just changed to list(APPEND ...))
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}")
find_package(Eigen3 REQUIRED)
References
Eigen & CMake
CMake: Of what use is find_package() if you need to specify CMAKE_MODULE_PATH anyway?
Cmake doesn't find Boost

Add the path of FindEigen3.cmake before find_package(Eigen3 REQUIRED), like this:
LIST(APPEND CMAKE_MODULE_PATH "/usr/share/cmake-2.8/Modules/")
find_package(Eigen3)

Related

Howto use cmake with boost asio?

I cannot get boost::asio to work with cmake in my c++ program. I have actually tried and googled for many hours, but I cannot get it to work!
I want to include boost::asio in my c++ Project under Ubuntu 18.04 with a cmake file.
So I installed the newest CMake (cmake version 3.19.4), and I downloaded boost version 1.74 and executed
./bootstrap.sh --prefix=/usr/
sudo ./b2 install
The install directory is /home/boost/boost_1_74_0. My CMake file looks like this:
cmake_minimum_required (VERSION 3.1.0)
# Project name
project (machine_tryout VERSION 1.0)
# Boost (header only)
#set(Boost_DEBUG 1)
set(BOOST_ROOT "$ENV{HOME}/boost/boost_1_74_0")
set(Boost_INCLUDE_DIR "$ENV{HOME}/boost/boost_1_74_0")
set(Boost_LIBRARY_DIR "$ENV{HOME}/boost/boost_1_74_0/libs")
find_package(Boost REQUIRED Components asio)
# Set Executable
add_executable(${PROJECT_NAME} source/tryout.cpp)
But everything I get is the following:
vm-umic#vm:~/Projects/tryout/build$ cmake ..
CMake Warning at /snap/cmake/775/share/cmake-3.19/Modules/FindBoost.cmake:2034 (message):
No header defined for asio; skipping header check (note: header-only
libraries have no designated component)
Call Stack (most recent call first):
CMakeLists.txt:27 (find_package)
CMake Error at /snap/cmake/775/share/cmake-3.19/Modules/FindPackageHandleStandardArgs.cmake:218 (message):
Could NOT find Boost (missing: asio) (found version "1.74.0")
Call Stack (most recent call first):
/snap/cmake/775/share/cmake-3.19/Modules/FindPackageHandleStandardArgs.cmake:582 (_FPHSA_FAILURE_MESSAGE)
/snap/cmake/775/share/cmake-3.19/Modules/FindBoost.cmake:2193 (find_package_handle_standard_args)
CMakeLists.txt:27 (find_package)
-- Configuring incomplete, errors occurred!
See also "/home/vm-umic/Projects/tryout/build/CMakeFiles/CMakeOutput.log".
What in the world am I doing wrong? Isnt CMake telling me that it found Boost 1.74? CMake does NOT throw any errors if I try find_package(Boost REQUIRED), but then linking does also not work. I explicitly tell CMake where to find the libraries, so why can't CMake finde Boost?
Try this.
cmake_minimum_required (VERSION 3.1.0)
# Project name
project (machine_tryout VERSION 1.0)
# Boost (header only)
#set(Boost_DEBUG 1)
set(BOOST_ROOT "$ENV{HOME}/boost/boost_1_74_0")
set(Boost_INCLUDE_DIR "$ENV{HOME}/boost/boost_1_74_0")
set(Boost_LIBRARY_DIR "$ENV{HOME}/boost/boost_1_74_0/libs")
find_package(Boost REQUIRED Components system)
# Set Executable
add_executable(${PROJECT_NAME} source/tryout.cpp)
target_link_libraries(${PROJECT_NAME}
${Boost_LIBRARIES})
I just had a similar problem, and found that I had missed a small but important step in the Boost installation: adding the installation directory (i.e. the PREFIX used in b2 install --prefix=PREFIX) to the PATH environment variable.
For me, this solved it – hopefully will for you too!

(C++17; Boost) CMake unable to find the requested Boost libraries

I am new to C++ and want to include the boost library (specifically the filesystem part which needs to be built) in my project. I tried many solutions from other stackoverflow users but they didn't help me at all. I am using CLion with CMake.
The main.cpp is calling the other .cpp files inside the modules/ folder.
The file structure:
ProjectName
>boost
>lots of folders and .hpp files
>cmake-build-debug
>modules
encryption.cpp
encryption.h
output.cpp
output.h
CMakeLists.txt
main.cpp
The boost folder doesn't contain the entirety of boost when you download and extract it. I dragged the boost folder inside of boost_1_72_0 in my project (just so you know that there's no libs folder, etc. inside)
The CMakeLists.txt
cmake_minimum_required(VERSION 3.14)
project(ProjectName)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libstdc++ -static-libgcc")
set(SOURCE_FILES
main.cpp
modules/encryption.cpp modules/encryption.h modules/output.cpp modules/output.h
)
set(Boost_ARCHITECTURE -x64)
set(BOOST_ROOT boost/)
set(Boost_INCLUDE_DIRS boost/filesystem)
find_package(Boost COMPONENTS system filesystem REQUIRED)
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
endif()
add_executable(ProjectName ${SOURCE_FILES})
target_link_libraries(ProjectName ${Boost_LIBRARIES})
output.cpp
// some includes //
#define BOOST_FILESYSTEM_NO_DEPRECATED
#include "../boost/filesystem.hpp"
// some code //
The Error message:
CMake Error at C:/Program Files/JetBrains/CLion 2019.1.4/bin/cmake/win/share/cmake-3.14/Modules/FindBoost.cmake:2147 (message):
Unable to find the requested Boost libraries.
Unable to find the Boost header files. Please set BOOST_ROOT to the root
directory containing Boost or BOOST_INCLUDEDIR to the directory containing
Boost's headers.
Call Stack (most recent call first):
CMakeLists.txt:15 (find_package)
-- Configuring incomplete, errors occurred!
See also "C:/Users/username/Desktop/C++/ProjectName/cmake-build-debug/CMakeFiles/CMakeOutput.log".
mingw32-make.exe: *** [cmake_check_build_system] Error 1
Makefile:235: recipe for target 'cmake_check_build_system' failed
I know that it's basically telling me what I have to do but I don't know what's exactly meant by the "root directory" of boost, by the directory "containing Boost's headers" and how to put everything together.
Many thanks in advance!
I dragged the boost folder inside of boost_1_72_0 in my project
Looks like you just copied boost source into your project dir.
You have to compile boost since you need filesystem. Or you can get boost from:
vcpkg - it's the easiest way for you. I am highly recommended this way.
Sourceforge.
Conan
I don't know what's exactly meant by the "root directory"...
Since you are using boost by calling find_package(Boost) - CMake uses FindBoost module. It will try to find your boost installation inside system PATH variable or in some other "standard" places. Your boost "installation" is not common, so you have to specify where boost is with BOOST_ROOT variable. set(BOOST_ROOT boost/) is incorrect way to do this. You have to specify absolute path like set(BOOST_ROOT "C:/lib/boost/boost17.2")
or relative to current CMakeList.txt - set(BOOST_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/boost}.
With right installed boost all you have to do is:
find_package(Boost REQUIRED [COMPONENTS <libs>...])
target_link_libraries(main PRIVATE ${Boost_LIBRARIES})
target_include_directories(main PRIVATE ${Boost_INCLUDE_DIRS})
Usually, you don't need to set Boost_ARCHITECTURE and Boost_INCLUDE_DIRS CMake does it for you.
When you use find_package with REQUIRED option you don't need to check whether the library found or not since CMake raises an error when a library isn't found.
BOOST_ROOT is a directory when boost installed or unpacked.
BOOST_INCLUDEDIR is a directory with boost headers (usually it's BOOST_ROOT/boost). So try to set the full path to your boost_1_72_0 directory to BOOST_ROOT CMake variable.
Also, I had a problem with COMPONENTS option. Try to remove it if errors remain.

CMake cannot find GLEW

I'm trying to link GLEW to my CMake project with little success.
Apparently, it can't find GLEW_LIBRARIES.
I'm using CLion 2019.3.4 and MinGW.
So far I've tried these things:
Defining GLEW_LIBRARIES
Defining GLEW_STATIC_LIBRARIES and GLEW_SHARED_LIBRARIES, as the FindGLEW.cmake file documents that (Line 41 to 45).
Doing both of the above.
Doing 1 and 2 except instead of LIBRARIES it is LIBRARY.
I don't know what else to do.
Heres the CMake lists for reference:
cmake_minimum_required(VERSION 3.15)
project(myProject)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++")
set(CMAKE_MODULE_PATH "${CMAKE_HOME_DIRECTORY}/cmake_modules/")
# This part of the code is actually in a separate file,
# called LibrarySetup.cmake
#
# include(LibrarySetup.cmake)
if(WIN32)
set(LIB_PREFIX "")
set(LIB_SUFFIX ".dll")
elseif(UNIX)
set(LIB_PREFIX "lib")
set(LIB_SUFFIX ".lib")
endif()
set(GLFW_INCLUDE_DIR "include/glfw/include/")
set(GLFW_LIBRARY "include/glfw/lib/${LIB_PREFIX}glfw3${LIB_SUFFIX}")
set(GLEW_INCLUDE_DIR "include/glew/include/")
set(GLEW_SHARED_LIBRARIES "include/glew/lib/Release/Win32/glew32.lib")
set(GLEW_STATIC_LIBRARIES "include/glew/lib/Release/Win32/glew32s.lib")
set(GLEW_VERBOSE)
# And here the external file ends.
find_package(GLFW REQUIRED)
find_package(GLEW REQUIRED)
include_directories(${GLFW_INCLUDE_DIR} ${GLEW_INCLUDE_DIRS})
add_subdirectory(include/glfw)
add_executable(myProject main.cpp)
target_link_libraries(myProject ${GLFW_LIBRARY} ${GLEW_LIBRARIES})
And the errors:
~\AppData\Local\JetBrains\Toolbox\apps\CLion\ch-0\193.6494.38\bin\cmake\win\bin\cmake.exe -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - MinGW Makefiles" "~\Documents\CLion Projects\myProject"
CMake Error at ~/AppData/Local/JetBrains/Toolbox/apps/CLion/ch-0/193.6494.38/bin/cmake/win/share/cmake-3.15/Modules/FindPackageHandleStandardArgs.cmake:137 (message):
Could NOT find GLEW (missing: GLEW_LIBRARIES)
Call Stack (most recent call first):
~/AppData/Local/JetBrains/Toolbox/apps/CLion/ch-0/193.6494.38/bin/cmake/win/share/cmake-3.15/Modules/FindPackageHandleStandardArgs.cmake:378 (_FPHSA_FAILURE_MESSAGE)
~/AppData/Local/JetBrains/Toolbox/apps/CLion/ch-0/193.6494.38/bin/cmake/win/share/cmake-3.15/Modules/FindGLEW.cmake:207 (find_package_handle_standard_args)
CMakeLists.txt:12 (find_package)
-- Configuring incomplete, errors occurred!
See also "~/Documents/CLion Projects/myProject/cmake-build-debug/CMakeFiles/CMakeOutput.log".
[Failed to reload]
The version of CMake your script requires ships with FindGLEW which should do the work of finding the library for you (i.e. set up a Glew target, define include and library paths, etc). You can see the documentation for this module by running:
cmake --help-module findglew
Providing include paths and library definitions of GLEW to your executable should be as simple as:
find_package(GLEW REQUIRED)
add_executable(myProject main.cpp)
target_link_libraries(myProject GLEW::GLEW)
This will provide include and lib paths via the transitive dependency of the GLEW::GLEW target. You should not need to set the paths manually as your example does. The find module will search in the default system locations for the library. If it can't find it you can provide it with a hint via setting the GLEW_ROOT variable to point to your local install location.
set(GLEW_ROOT <my location of GLEW>)
How did you install GLEW? Can you provide an indication of where it is installed on your system and that might make it easier to see why the find module failed?

config Eigen in CMakeLists.txt file

When I config Eigen library in CMakeLists.txt file as follows:
cmake_minimum_required(VERSION 3.14)
project(helloworld)
add_subdirectory(tests)
add_subdirectory(deps/eigen)
set(SRC_LIST main.cpp)
add_executable(hello ${SRC_LIST})
find_package(Eigen3 3.3 REQUIRED NO_MODULE)
target_link_libraries(hello eigen)
I got the cmake error as
CMake Error at build/deps/eigen/Eigen3Config.cmake:20 (include):
The file
/Users/joe/codecplus/build/deps/eigen/Eigen3Targets.cmake
was generated by the export() command. It may not be used as the argument
to the include() command. Use ALIAS targets instead to refer to targets by
alternative names.
Call Stack (most recent call first):
CMakeLists.txt:9 (find_package)
Can anybody help me out? Don't know what is going wrong here. Thanks very much.
You use two ways for including 3d-party project (Eigen) at the same time:
add_subdirectory()
find_package()
This is wrong. Resort to a single way:
With add_subdirectory only:
add_subdirectory(deps/eigen)
# ...
target_link_libraries(hello Eigen3::eigen)
With find_package() only:
find_package(Eigen3 3.3 REQUIRED NO_MODULE)
target_link_libraries(hello Eigen3::eigen)
Note, that both approaches uses Eigen3::eigen target instead of eigen for link with. Only this name works with the second approach, and it is described in the documentation for Eigen usage.

Use libbitcoin in CLion

I am not a C++ programmer, only have made a course a while ago. Using homebrew I installed libbitcoin and was hoping that I can reference the library like I was able to reference the boost libraries. I also realized that there are no links in /usr/local/bin to the Cellar.
I think I could get it working by using the absolute paths but I am looking for the proper way of handling this constellation that I just mentioned.
Current CMake:
cmake_minimum_required(VERSION 2.8.4)
project(cplusplus)
message(STATUS "start running cmake...")
find_package(boost 1.65.1 COMPONENTS system filesystem REQUIRED)
find_package(libbitcoin 3.3.0 COMPONENTS system filesystem REQUIRED)
message("system: ${CMAKE_SYSTEM_PREFIX_PATH}")
find_library(LIB_BITCOIN libbitcoin)
message("bitcoin: ${LIB_BITCOIN}")
if(Boost_FOUND)
message(STATUS "Boost_INCLUDE_DIRS: ${Boost_INCLUDE_DIRS}")
message(STATUS "Boost_LIBRARIES: ${Boost_LIBRARIES}")
message(STATUS "Boost_VERSION: ${Boost_VERSION}")
include_directories(${Boost_INCLUDE_DIRS})
endif()
add_executable(cplusplus main.cpp)
if(Boost_FOUND)
target_link_libraries(cplusplus ${Boost_LIBRARIES})
endif()
Currently I get these errors:
/Applications/CLion.app/Contents/bin/cmake/bin/cmake -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - Unix Makefiles" /Users/johndow/Documents/Workspace/bitcoin-code/cplusplus
-- start running cmake...
-- Boost version: 1.65.1
CMake Error at CMakeLists.txt:8 (find_package):
By not providing "Findlibbitcoin.cmake" in CMAKE_MODULE_PATH this project
has asked CMake to find a package configuration file provided by
"libbitcoin", but CMake did not find one.
Could not find a package configuration file provided by "libbitcoin"
(requested version 3.3.0) with any of the following names:
libbitcoinConfig.cmake
libbitcoin-config.cmake
Add the installation prefix of "libbitcoin" to CMAKE_PREFIX_PATH or set
"libbitcoin_DIR" to a directory containing one of the above files. If
"libbitcoin" provides a separate development package or SDK, be sure it has
been installed.
-- Configuring incomplete, errors occurred!
See also "/Users/johndoe/Documents/Workspace/bitcoin-code/cplusplus/cmake-build-debug/CMakeFiles/CMakeOutput.log".
[Finished]
You seem to have double lookup for libbitcoin library in your CMakeLists file. You are first looking for it by:
find_package(libbitcoin ...)
and then by
find_library(LIB_BITCOIN libbitcoin)
Cmake is not happy (as your error message says) with the find_package() clause as libbitcoin does not provide cmake configuration by itself. You have many ways how to fix it, just two of them:
remove find_package() and use only find_library(), I think this is the simpler way and your project should work this way
provide cmake configuration for libbitcoin by yourself. Good introduction how to do this is here (and good to read anyway):
https://cmake.org/Wiki/CMake:How_To_Find_Libraries
As far as I know, currently libbitcoin does not export any <libbitcoin>Config.cmake package.
But it does export a libbitcoin.pc file for generic use with pkg-config.
ie: /usr/local/lib/pkgconfig/libbitcoin.pc
If you get results from invoking pkg-config --cflags libbitcoin then it's there.
And then you can put something like this in your CMakeLists.txt:
#use this if libbitcoin is installed to some custom location
set(ENV{PKG_CONFIG_PATH} "/path/to/libbitcoin/pkgconfig/:$ENV{PKG_CONFIG_PATH}")
#then later..
find_package(PkgConfig REQUIRED)
pkg_check_modules(LIB_BITCOIN REQUIRED libbitcoin)
#then later..
target_link_libraries(${PROJECT_NAME} PRIVATE ${LIB_BITCOIN_LIBRARIES})
target_include_directories(${PROJECT_NAME} PRIVATE ${LIB_BITCOIN_INCLUDE_DIRS})
That should pull in boost, make the libbitcoin includes visible and solve all manner of compiler and linker woes.
(Or if you are feeling mad, you could always make use of this gist).