config Eigen in CMakeLists.txt file - c++

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.

Related

CMake find_package for non-standard locations

I am trying to make a self contained library whose dependencies are self contained.All installs are installed to <repo_dir>/libs//build.
To find dependencies in cmake build system I use find_package. Because all installs are in a custom location I point I use HINTS to help out like so -
# CMake 3.16.3
# ../lib/Catch2/build is where Catch2Config.cmake is located
find_package(Catch2 REQUIRED HINTS "../lib/Catch2/build" NO_MODULE)
include_directories(tests)
add_executable(tests tests.cpp)
target_link_libraries(tests PRIVATE project_warnings project_options Catch2::Catch2)
I recognize that Catch2 likely doesn't need this as it is header only, however I am trying to learn what cmake is doing.
The CMakeLists.txt above prints out
include could not find load file:
<repo_dir>/lib/Catch2/build/Catch2Targets.cmake │
Call Stack (most recent call first):
tests/CMakeLists.txt:1 (find_package)
It looks like a target file is needed as well. I cannot find one in the Catch2 repo.
I am also trying the same process for folly, however it does not work for me either.
Using the following in module mode for Boost worked. Why does it work?
set(CMAKE_INCLUDE_PATH ${CMAKE_INCLUDE_PATH} "${PROJECT_SOURCE_DIR}/lib/boost_1_75_0/")
set(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} "${PROJECT_SOURCE_DIR}/lib/boost_1_75_0/libs")
find_package(Boost 1.60.0 REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})
target_link_libraries(${CMAKE_PROJECT_NAME} ${Boost_LIBRARIES})
Could someone help to uncover my problems and how I should go about using
Header only libraries
Static lib binaries
Self-Compiled Libraries
Whose resources are in unusual places?

How do I include the Eigen library in a CMakelist.txt on windows

I am trying to include the Eigen library to my CMakelist.txt. I have followed the CMake instructions on the Eigen Docs but I am using Jetbrain's Clion and not CMake directly. So I do not know how to use the Cmake commands provided. I have researched around but I don't have have a very good understanding of CMake to write Cmakelists, so I haven't been able to get anything to work yet.
this is what I have been using just to test the serup of the library:
cmake_minimum_required(VERSION 3.17)
project(Eigen_Test)
set(CMAKE_CXX_STANDARD 20)
find_package (Eigen3 3.3 REQUIRED NO_MODULE)
add_executable (example example.cpp)
target_link_libraries (example eigen)
add_executable(Eigen_Test main.cpp)
this is the error I have been receiving:
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.
I Have researched many ways to include the library but most methods use command lines which I am unfamiliar with. Also I do not have an Eigen3Config.cmake the only file I have Eigen3Config.cmake.in. I assume there is some install trick that I must not be aware of. If anyone has a way to include clion strictly using a CMakelist.txt, I would be greatly appreciative.
Here a working example with CMake on Windows using the MinGW environment with mingw32-make.exe and g++.exe compiler.
CMakeLists.txt :
# The following lines depends on your project :
cmake_minimum_required(VERSION 3.19)
project(PROJECT_NAME)
set(CMAKE_CXX_STANDARD 17)
# You have to set these variables as Windows environment variables:
# EIGEN3_INCLUDE_DIR <- %EIGEN3_ROOT%
# EIGEN3_DIR <- %EIGEN3_ROOT%\cmake
#
# EIGEN3_INCLUDE_DIR: variable needed for file %EIGEN3_ROOT%/cmake/FindEigen3.cmake
#
# CMAKE_MODULE_PATH: Search path for the module Eigen3 to be loaded by find_package
#
SET( EIGEN3_INCLUDE_DIR "$ENV{EIGEN3_INCLUDE_DIR}" )
SET( CMAKE_MODULE_PATH "$ENV{EIGEN3_DIR}" )
find_package( Eigen3 3.3 REQUIRED )
# include_directories is needed for the compiler to know where looking for Eigen3 header files to be included
include_directories( ${EIGEN3_INCLUDE_DIR} )
add_executable(PROJECT_NAME FILES...)
You can then call the Eigen3 libraries, such as:
#include <Eigen/Core>
Eigen is a header only library, so you don't have to add it to target_link_library, and you don't need a CMake Macro to detect it.
Instead just add the header file into your include path and you should be set.

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?

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

Unable to find Eigen3 with CMake

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)