Could not find libgit2 in CMake - cmake

I am doing my first steps in CMake and I have a Hello World example where I would like to use libgit2. My directory structure is the following:
- main.cpp
- CMakeLists.txt
- cmake/Modules/Findlibgit2.cmake
- libs/libgit2/include/git2.h
- libs/libgit2/debug/git/git2.lib
- libs/libgit2/debug/git/git2.dll
I would like to link it either statically, or dynamically, at least it works for the beginning. But when I execute CMake, I receive the error message below. Could anyone help me with this?
Not searching for unused variables given on the command line.
[cmake] CMake Error at C:/Program Files/CMake/share/cmake-3.17/Modules/FindPackageHandleStandardArgs.cmake:164 (message):
[cmake] Could NOT find libgit2 (missing: GIT2_LIBRARY GIT2_INCLUDE_PATH)
[cmake] Call Stack (most recent call first):
[cmake] C:/Program Files/CMake/share/cmake-3.17/Modules/FindPackageHandleStandardArgs.cmake:445 (_FPHSA_FAILURE_MESSAGE)
[cmake] cmake/Modules/Findlibgit2.cmake:15 (find_package_handle_standard_args)
[cmake] CMakeLists.txt:6 (find_package)
Findlibgit2.cmake
# Find git2 Library
#
# GIT2_INCLUDE_DIRS - where to find git2.h, etc.
# GIT2_LIBRARIES - List of libraries when using libgit2.
# GIT2_FOUND - True if libgit2 is found.
# GIT2_INCLUDE_PATH
find_path(GIT2_INCLUDE_PATH NAMES git2.h)
# GIT2_LIBRARY
find_library(GIT2_LIBRARY NAMES git2)
# handle the QUIETLY and REQUIRED arguments and set GIT2_FOUND to TRUE if
# all listed variables are TRUE
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(libgit2 REQUIRED_VARS GIT2_LIBRARY GIT2_INCLUDE_PATH)
if (GIT2_FOUND)
set(GIT2_INCLUDE_DIR ${GIT2_INCLUDE_PATH})
set(GIT2_INCLUDE_DIRS ${GIT2_INCLUDE_PATH})
set(GIT2_LIBRARIES ${GIT2_LIBRARY})
endif()
mark_as_advanced(
GIT2_INCLUDE_PATH
GIT2_LIBRARY
)
CMakeLists.txt
cmake_minimum_required(VERSION 2.8.9)
project (backend)
SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules/")
find_package(libgit2 REQUIRED)
include_directories(${LIBGIT2_INCLUDE_DIR})
link_directories("backend/libs/libgit2/debug")
add_executable(backend main.cpp)

It seems like libgit2 doesn't provide a CMake package configuration file, so it would be up to you to write a Find Module or find one on the internet. The Findlibgit2.cmake file you have is a good start.
I found another libgit2 Find Module online here: Findlibgit2.cmake. This one utilizes pkg-config, leveraging the .pc file provided by libgit2 itself, but, considering you're using Windows, would require you to install pkg-config manually. This may be more trouble than it's worth.
Regarding your error message:
Unless the libgit2 package is in the standard system path, CMake will not know where to look for it. So, you must explicitly tell CMake where to look. One simple way to do this is by modifying your Findlibgit2.cmake file, using HINTS for the find_* commands:
# GIT2_INCLUDE_PATH
find_path(GIT2_INCLUDE_PATH NAMES git2.h
HINTS ${GIT2_INCLUDEDIR}
)
# GIT2_LIBRARY
find_library(GIT2_LIBRARY NAMES git2
HINTS ${GIT2_LIBRARYDIR}
)
Now, we can populate these GIT2_INCLUDEDIR and GIT2_LIBRARYDIR variables to point to the installed libgit2 package. After doing so, find_package() should then succeed in finding the package components, and you can link to git2.lib using ${GIT2_LIBRARIES}.
CMakeLists.txt:
cmake_minimum_required(VERSION 2.8.9)
project (backend)
SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules/")
# Set hints so CMake knows where libgit2 is located on your machine.
set(GIT2_INCLUDEDIR ${CMAKE_CURRENT_LIST_DIR}/libs/libgit2/include)
set(GIT2_LIBRARYDIR ${CMAKE_CURRENT_LIST_DIR}/libs/libgit2/debug/git)
find_package(libgit2 REQUIRED)
include_directories(${GIT2_INCLUDE_DIR})
# Don't need this line. Use of 'link_directories' is discouraged.
link_directories("backend/libs/libgit2/debug")
add_executable(backend main.cpp)
# Add this line to link against the libgit2 libraries.
target_link_libraries(backend PRIVATE ${GIT2_LIBRARIES})
A more portable way to set these HINTS variables is to instead set them when calling cmake on the command line:
cmake -DGIT2_INCLUDEDIR=C:/path/to/libgit2/include -DGIT2_LIBRARYDIR=D:/path/to/libgit2/lib ..
This way, if you have other developers working with the project, they can point to wherever they may have libgit2 installed on their machine, eliminating the need to modify the CMakeLists.txt file.

Related

Why is the toolchain file executed a few times in CMake?

In an attempt to create a cross-compilation CMake toolchain template with the SDCC compiler, I have come across a very weird issue.
As described in this link, if the toolchain.cmake file defines a CMAKE_SYSTEM_NAME, CMake will look for the file with the ${CMAKE_SYSTEM_NAME}.cmake under the Module/Platform directory. And this file should define platform-specific options. In my case, I am using it to find the sdcc compiler and setting some compiler flags.
This works just fine for me. Using cmake -DCMAKE_MODULE_PATH="${PATH_TO_MY_MODULES}" -DCMAKE_TOOLCHAIN_FILE="${PATH_TO_MY_TOOLCHAIN}" -DSDCC_SYSROOT="SOME_VALUE", CMake finds all the correct toolchain and platform files.
It seems like the toolchain and the platform file are executed (not sure if that's the correct term) a few times during the configuration process. In the first few times, the variable SDCC_SYSROOT I passed in the CMake command has the value SOME_VALUE as expected. However, the same variable SDCC_SYSROOT seems to lose the value in the last time these toolchain/platform files are executed. So they are empty. This causes my script to generate a fatal error.
toolchain.cmake has the following contents:
set(CMAKE_SYSTEM_NAME SDCC_PIC_16F877A)
# Finding resource settings
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
# Set default MCU family and model
if (NOT MICROCHIP_FAMILY)
set(MICROCHIP_FAMILY "pic16")
endif()
if (MICROCHIP_MODEL STREQUAL "pic16")
set(MICROCHIP_MODEL "16f877a")
endif()
# Need a better way to detect the supported models here
if (NOT MICROCHIP_FAMILY STREQUAL "pic16" AND NOT MICROCHIP_MODEL STREQUAL "16f877a")
message(FATAL_ERROR "Settings not supported. Please drop a request.")
endif()
if (NOT SDCC_ROOT)
message(FATA_ERROR "Need to provide the root (from toolchain.)")
endif()
# Cache those variables
set(SDCC_ROOT "${SDCC_ROOT}"
CACHE INTERNAL "Root directory of SDCC installation")
set(MICROCHIP_FAMILY "${MICROCHIP_FAMILY}"
CACHE INTERNAL "Family of the chip to compile for")
set(MICROCHIP_MODEL "${MICROCHIP_MODEL}"
CACHE INTERNAL "Model of the chip to compile for")
the Module/Platform/SDCC_PIC_16F877A.cmake file has the contents:
# Check if the shit exists
message("!!! The value of root is ${SDCC_ROOT}")
if (NOT SDCC_ROOT)
message(FATAL_ERROR
"SDCC_ROOT is not defined. Please set this variable e.g.\n"
"cmake -DSDCC_ROOT=\"C:/Program Files/sdcc\"")
endif()
# Finding the compilers
find_program(CMAKE_C_COMPILER
sdcc
PATHS ${SDCC_ROOT}
PATH_SUFFIXES "bin"
DOC "path to the SDCC C compiler.")
and my CMakeLists.txt is the following:
cmake_minimum_required(VERSION 3.10)
project(PicExample)
message("THE COMPILER IS ${CMAKE_C_COMPILER}")
add_executable(pic_example main.c)
what I invoke from my project/build directory and the error I get:
cmake -DCMAKE_MODULE_PATH:FILEPATH="/mnt/c/Users/mathe/Desktop/coding/sdcc-pic-template/Modules" -DCMAKE_TOOLCHAIN_FILE:FILEPATH="/mnt/c/Users/mathe/Desktop/coding/sdcc-pic-template/Modules/toolchain.cmake" -DSDCC_ROOT="testing/" ..
-- The C compiler identification is GNU 9.3.0
-- The CXX compiler identification is GNU 9.3.0
!!! The value of root is testing/
!!! The value of root is testing/
-- Check for working C compiler: /usr/bin/cc
FATA_ERRORNeed to provide the root (from toolchain.)
!!! The value of root is
CMake Error at /mnt/c/Users/mathe/Desktop/coding/sdcc-pic-template/Modules/Platform/SDCC_PIC_16F877A.cmake:4 (message):
SDCC_ROOT is not defined. Please set this variable e.g.
cmake -DSDCC_ROOT="C:/Program Files/sdcc"
Call Stack (most recent call first):
/usr/share/cmake-3.16/Modules/CMakeSystemSpecificInformation.cmake:26 (include)
/mnt/c/Users/mathe/Desktop/coding/sdcc-pic-template/build/CMakeFiles/CMakeTmp/CMakeLists.txt:3 (project)
CMake Error at /usr/share/cmake-3.16/Modules/CMakeTestCCompiler.cmake:44 (try_compile):
Failed to configure test project build system.
Call Stack (most recent call first):
CMakeLists.txt:2 (project)
-- Configuring incomplete, errors occurred!
See also "/mnt/c/Users/mathe/Desktop/coding/sdcc-pic-template/build/CMakeFiles/CMakeOutput.log".
Why do the toolchain files get "executed" more than once by CMake and has no access to cache in the latest runs? I've been finding CMake documentation for cross-compilation very difficult, especially if you are working with a non-standard compiler.
I am aware that other people have had same issues before, but I am not simply asking for a simple hacky solution (setting environment variables). I actually want to know why this happens (which the previous answers don't tackle).
Tsyvarev answered the why the toolchain is used multiple times in CMake. TLDR; CMake needs it for multiple try_compile() calls it uses internally for error checking and other things.
This works just fine for me.
-DCMAKE_MODULE_PATH="${PATH_TO_MY_MODULES}" -DCMAKE_TOOLCHAIN_FILE="${PATH_TO_MY_TOOLCHAIN}" -DSDCC_SYSROOT="SOME_VALUE",
To fix your problem here is what you need to do.
Essentially you are passing an argument to your toolchain file. And this argument SDCC_SYSROOT essentially goes out of scope.
To fix this problem here is what you need to do.
# Use list(APPEND) rather than set() so that any variables added by CMake aren't lost!
#
# Here is the docs for this variable:
# https://cmake.org/cmake/help/latest/variable/CMAKE_TRY_COMPILE_PLATFORM_VARIABLES.html
list(APPEND CMAKE_TRY_COMPILE_PLATFORM_VARIABLES ${SDCC_SYSROOT})
If you want to see how many times your toolchain script gets executed try putting in a message() call in there for fun.
And if you are really interested look inside your build folder and see what it is CMake is doing.
If you are wondering how I know this information it's because I read the toolchain section in Craig Scott's CMake book "Professional CMake:
A Practical Guide"
Here is a link: https://crascit.com/professional-cmake/
For determine, whether some feature is supported by the compiler or by some library, CMake uses try_compile approach: during the configuration phase, it creates separate CMake project and immediately configures and builds it. Because it is a separate project, its configuration has the same steps as the main project and it loads the toolchain file too.
try_compile could be used by the (user) project for check features of the library or of the compiler. There are many CMake modules which use try_compile in their implementation. E.g. CheckSymbolExists.
try_compile is also used by CMake itself, in platform files, when it perform basics checks for the compiler. In your log you could find the line:
CMake Error at /usr/share/cmake-3.16/Modules/CMakeTestCCompiler.cmake:44 (try_compile)
Aside from try_compile, the new CMake project is created in ExternalProject_Add command. That creation is also accompanied by the reading of the toolchain file. (More correctly, the new project is created not when ExternalProject_Add invocation is processed but when corresponding project is configured. This configuration is performed on the build stage of the main project.)

Can't seem to include Glut to my cmake project Ubuntu [duplicate]

I've been stuck for a while now and I can't figure out how to get freeglut working. I thought I knew what it was asking me to do, so I added that set(prefix_path) line but it didn't do anything. Am I supposed to write my own freeglut-config.cmake or what?
Note: I am using the freeglut for MinGW package from this website
CMake File:
cmake_minimum_required(VERSION 3.7)
project(HW1)
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES Triangle.cpp)
set(CMAKE_PREFIX_PATH "C:/freeglut")
find_package(GLEW REQUIRED STATIC)
find_package(FREEGLUT REQUIRED)
find_package(OPENGL REQUIRED)
include_directories(${FREEGLUT_INCLUDE_DIRS} ${GLEW_INCLUDE_DIRS} ${OPENGL_INCLUDE_DIRS})
link_directories(${FREEGLUT_LIBRARY_DIRS} ${GLEW_LIBRARY_DIRS} ${OPENGL_LIBRARY_DIRS})
add_definitions(${FREEGLUT_DEFINITIONS} ${GLEW_DEFINITIONS} ${OPENGL_DEFINITIONS})
add_executable(HW1 ${SOURCE_FILES})
target_link_libraries(HW1 ${FREEGLUT_LIBRARIES} ${GLEW_LIBRARIES} ${OPENGL_LIBRARIES})
Full error:
CMake Error at CMakeLists.txt:8 (find_package):
By not providing "FindFREEGLUT.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "FREEGLUT",
but CMake did not find one.
Could not find a package configuration file provided by "FREEGLUT" with any
of the following names:
FREEGLUTConfig.cmake
freeglut-config.cmake
Add the installation prefix of "FREEGLUT" to CMAKE_PREFIX_PATH or set
"FREEGLUT_DIR" to a directory containing one of the above files. If
"FREEGLUT" provides a separate development package or SDK, be sure it has
been installed.
If your application is GLUT-compatible, that it doesn't use any extension of freeglut, then it is better to search GLUT instead of FREEGLUT:
find_package(GLUT REQUIRED)
"Find" script used by this command is already shipped into CMake distro, and it searches freeglut too.
(Note, that with that command variables for include directories and linking libraries are GLUT_INCLUDE_DIR and GLUT_LIBRARY correspondingly).
If your application requires exactly freeglut (that is, uses some of its extensions incompatible with other GLUT implementations), you need to ship your package with FindFREEGLUT.cmake script and adjust CMAKE_MODULE_PATH variable correspondingly:
# Assuming you have <source-dir>/cmake/FindFREEGLUT.cmake
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
find_package(FREEGLUT REQUIRED)
You may find existing script in the net, or write it by yourself, like here.
In any case, if you have freeglut installed into non-system location, you need to hint CMake about that. E.g., by adjusting CMAKE_PREFIX_PATH.

Building a VS2015 x64 project using glew 1.13.0 and CMake 3.4.0

How do I build a VS2015 x64 project using glew 1.13.0 and CMake 3.4.0?
I prepared a minimal demo that can be found here: https://bitbucket.org/Vertexwahn/cmakedemos/src/2fbbc02b2c0567319d7be070b34391b1ef35048d/GlewDemo/?at=default
CMakeLists.txt:
cmake_minimum_required ( VERSION 2.8)
project ( GlewDemo )
find_package(GLEW REQUIRED)
set ( SRCS main.cpp )
add_executable(GlewDemo ${SRCS})
target_link_libraries(GlewDemo glew32s)
I downloaded the prebuilt binaries from here: http://sourceforge.net/projects/glew/files/glew/1.13.0/glew-1.13.0-win32.zip/download
And set the path of GLEW_INCLUDE_DIR to the corresponding directory ("C:\Users\no68koc\Downloads\glew-1.13.0\include")
But CMake gives me some errors:
CMake Error at C:/Program Files (x86)/CMake/share/cmake-3.4/Modules/FindPackageHandleStandardArgs.cmake:148 (message):
Could NOT find GLEW (missing: GLEW_LIBRARY)
Call Stack (most recent call first):
C:/Program Files (x86)/CMake/share/cmake-3.4/Modules/FindPackageHandleStandardArgs.cmake:388 (_FPHSA_FAILURE_MESSAGE)
C:/Program Files (x86)/CMake/share/cmake-3.4/Modules/FindGLEW.cmake:44 (find_package_handle_standard_args)
CMakeLists.txt:4 (find_package)
Configuring incomplete, errors occurred!
See also "C:/build/vs2015/GlewDemo/CMakeFiles/CMakeOutput.log".
How does it work properly?
Error message
Could NOT find GLEW (missing: GLEW_LIBRARY)
is a standard message generated by FindXXX.cmake script (called via find_package(XXX)), that it is failed to set(deduce) some CMake variables, so whole package is assumed to be not found.
Most of FindXXX.cmake scripts descibes(in the comment), which variables are set by the script for user.
But only several of them describes, how user can help script in case when the script failed to find needed package at all, or if user wants script to find specific package installation instead of default one.
There is no universal approach in helping to the FindXXX.cmake script, in most cases one should analize code of the script for know variables, which can help.
In the given case (with GLEW package) analizing CMake-provided FindGLEW.cmake script reveals, that both find_path() call (which set GLEW_INCLUDE_DIR variable) and find_library() call (which set GLEW_LIBRARY variable) use no hints (HINT or PATH options) for search. But there are standard hints, which are used by both of this commands. One of these hints is ${CMAKE_PREFIX_PATH}/include directory for find_path and similar directory for find_library.
So you can use
list(APPEND CMAKE_PREFIX_PATH "C:\Users\no68koc\Downloads\glew-1.13.0")
for hint to find_library() and find_path() to search under this directory too.
Alternatively, you may set CMAKE_PREFIX_PATH variable in CMake cache either in GUI (e.g. inside Visual Studio) or via command line:
cmake -DCMAKE_PREFIX_PATH:PATH=C:\Users\no68koc\Downloads\glew-1.13.0
(Note, that using list(APPEND ...) instead of set(...) within CMakeLists.txt does not override variable in case it is set in cache too).
You may use another, 3d-party FindGLEW.cmake script
You can download it into your project (e.g., to cmake/FindGLEW.cmake) and issue
set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)
for tell find_package() to use this script instead of default one.
Given script uses
${GLEW_LOCATION}
directory (and its subdirectories) as hint for searching in find_path() and find_library() (under PATH command's option). So you can set GLEW_LOCATION variable to installation directory:
set(GLEW_LOCATION "C:\Users\no68koc\Downloads\glew-1.13.0")
for make things work. Alternatively(and preferrably), this variable can be set in cache.
Also, given FindGLEW.cmake script uses
$ENV{GLEW_LOCATION}
directory as hint. This means that setting GLEW_LOCATION environment variable will also helps.
CMake cannot locate your GLEW. Thus you have to hint CMake.
Either include GLEW to some place, where CMake looks for components. Installing GLEW comes to mind.
Or you define the variables manually. You already did that for GLEW_INCLUDE_DIR. You have to define GLEW_LIBRARY, too.
It must be the path to the library named glew32, glew, or glew32s. With Unices it should be lib*.so maybe with some additional version numbers. With Windows it should be *.dll or *.lib.

CMake find_package() Eclipse, OS X

I managed to create Make Target in Eclipse and add a CMakeListst.txt to a very simple project and it worked.
Now, my next step is to use two external libraries, Boost and Eigen.
My project is in /Users/MyUser/Documents/workspace/Test
The libraries are /Users/MyUser/Documents/MyLib/Libraries
Now, in the CMakeLists.txt file I try to find Boost and Eigen, which are in the libraries folder, but always the returned message is
CMake Error at CMake/TPLs/FindBoost.cmake:1126 (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:23 (FIND_PACKAGE)
-- Configuring incomplete, errors occurred! CMake Error at /Applications/CMake
2.8-11.app/Contents/share/cmake-2.8/Modules/FindPackageHandleStandardArgs.cmake:108
(message): Could NOT find Eigen3 (missing: EIGEN3_INCLUDE_DIRS
EIGEN3_VERSION_OK) (Required is at least version "2.91.0") Call
Stack (most recent call first): /Applications/CMake
2.8-11.app/Contents/share/cmake-2.8/Modules/FindPackageHandleStandardArgs.cmake:315
(_FPHSA_FAILURE_MESSAGE) CMake/TPLs/FindEigen3.cmake:76
(find_package_handle_standard_args) CMakeLists.txt:30 (FIND_PACKAGE)
I am new to CMake, so I must be missing some way to tell CMake to search in my library folder. How can I find the packages with CMake when building the project?
BTW I'm working under Mac OS X Mavericks.
EDIT
Reading through the file FindEigen3.cmake there should be a variable called EIGEN3_INCLUDE_DIRS that points to the include directory of Eigen, is this true?
Now, from the following message
Could NOT find Eigen3 (missing: EIGEN3_INCLUDE_DIRS EIGEN3_VERSION_OK)
I would think that cmake is actually finding that directory as it says EIGEN3_VERSION_OK but then why can't cmake find the rest of the include files?
Am I still missing something? I created the environment variable within Eclipse and then add a line to the FindEigen3.cmake to test the value of the environment variable EIGEN3_INCLUDE_DIRS
message(STATUS "Eigen version: ${EIGEN3_INCLUDE_DIRS}")
But the message I'm getting is
--Eigen version: EIGEN3_INCLUDE_DIRS-NOTFOUND
Any advise?
EDIT
I tried with the question-related answers but none was able to make Eclipse+CMake find Boost and Eigen libraries. I guess the problem here is how to make Eclipse recognize the system variables.
When you do not believe in setting environment variables in Eclipse, you can pass Cmake-defines either directly to cmake command:
cmake -E chdir Release/ cmake -G "Unix Makefiles" ... -DBOOST_ROOT:PATHNAME=boost/install/dir ...
or define them in your CMakeLists.txt before find boost/eigen3
SET(BOOST_ROOT boost/install/dir)
...
FIND_PACKAGE(BOOST ... )
or use environment variables within your OS/shell. You must define them before launching the Eclipse. E.g. from bash, do
export BOOST_ROOT=boost/install/dir
eclipse
Note that you have to define all variables needed for both boost and eigen3. You can place some debugging messages in your CMakeLists.txt to confirm that you pass them correctly:
MESSAGE("Boost root: ${BOOST_ROOT}")

Find package Eigen3 for CMake

CMake cannot find my Eigen3 package. I set an environment variable called
EIGEN3_INCLUDE_DIR
pointing to the path where FindEigen3.cmake is.
Then in the CMakelists.txt I wrote:
find_package( Eigen3 REQUIRED )
include_directories( EIGEN3_INCLUDE_DIR )
I get next message of error:
CMake Error at C:/Program Files (x86)/CMake 2.8/share/cmake-2.8/Modules/FindPackageHandleStandardArgs.cmake:91 (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):
C:/Program Files (x86)/CMake 2.8/share/cmake-2.8/Modules/FindPackageHandleStandardArgs.cmake:252 (_FPHSA_FAILURE_MESSAGE)
C:/Program Files (x86)/CMake 2.8/share/cmake-2.8/Modules/FindEigen3.cmake:76 (find_package_handle_standard_args)
test/test_quaternion/CMakeLists.txt:25 (find_package)
Any idea on what I am missing or doing wrong?
Since Eigen3 is completely header only, all you ever need is the path to the include directory. And this one, you are already defining manually anyway. So there is no real need for a FindEigen3.cmake or FIND_PACKAGE call.
Simply use
INCLUDE_DIRECTORIES ( "$ENV{EIGEN3_INCLUDE_DIR}" )
or
SET( EIGEN3_INCLUDE_DIR "$ENV{EIGEN3_INCLUDE_DIR}" )
IF( NOT EIGEN3_INCLUDE_DIR )
MESSAGE( FATAL_ERROR "Please point the environment variable EIGEN3_INCLUDE_DIR to the include directory of your Eigen3 installation.")
ENDIF()
INCLUDE_DIRECTORIES ( "${EIGEN3_INCLUDE_DIR}" )
A few notes:
If you want to access the content of a CMake variable, make sure to use ${...}
$ENV{....} accesses environment variables.
The second example will stop with an error if the environment variable is not set (and, thus, EIGEN3_INCLUDE_DIR cmake variable is empty)
Be careful to use quotation marks around (evaluated) variables if they could contain whitespace. Otherwise, CMake will interpret it as a list.
If you want to use custom find modules, make sure to either place them in you CMake installation or, as #Fraser pointed out above, make sure to point CMAKE_MODULE_PATH to the directory where it is. Not sure, but it could be that CMake checks the current directory as well automatically (where your CMakeLists.txt resides. Anyhow, setting EIGEN3_INCLUDE_DIR is totally unrelated to the location of FindEigen3.cmake
However, it could be that your FindEigen3 script evaluates this variable to determine the location of your Eigen3 installation.
Alternatively, self-built CMake-based projects often provide a <PackageName>Config.cmake. If you point a variable called <PackageName>_DIR to the directory containing this file, you can use FIND_PACKAGE( <PackageName> ...) as normal. See documentation of FIND_PACKAGE for details.
Eigen >= 3.3 has direct CMake integration, so it seems yours isn't set up correctly.
Assume Eigen has been installed to /opt/eigen/3.3 with default settings. Append or prepend the the location /opt/eigen/3.3 to the environment variable CMAKE_PREFIX_PATH e.g in bash:
export CMAKE_PREFIX_PATH="$CMAKE_PREFIX_PATH:/opt/eigen/3.3"
Then CMake should pick it up.
If you are writing your own CMakeLists.txt that uses Eigen I suggest using code like this:
find_package (Eigen3 3.3 REQUIRED)
add_executable (example example.cpp)
target_link_libraries (example Eigen3::Eigen)
You should not use include_directories since CMake 3 was released -- the targets approach should be preferred whenever available.
First, make sure Eigen is properly installed. Refer to the INSTALL file that comes with the tarball.
Second, copy the cmake/FindEigen3.cmake file from the tarball to the directory containing your CMakeLists.txt.
In your CMakeLists.txt add:
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR})
find_package(Eigen3 REQUIRED)
include_directories(${EIGEN3_INCLUDE_DIR})
Now you should be able to do e.g. #include <Eigen/Core>.
All of this comes (mostly) from this source.
This approach has the advantage over e.g. include_directories("$ENV{EIGEN3_INCLUDE_DIR}") that it the uses CMake's standard mechanism for finding external dependencies, making it easier for someone else (or your future self) to pick up the project, possibly on another platform.
(However, it would be nice if Eigen itself installed an EigenConfig.cmake file, making it accessible through the find_package mechanism without any extra paths.)
I found another solution here (which referred to here) which uses the pkg-config file :
find_package(PkgConfig)
pkg_search_module(Eigen3 REQUIRED eigen3)
As a detailed explanation of the note 7 from the top answer of Johannes. By using the "Config mode" rather than "Module mode" of CMake find_packate(), only writing
find_package( Eigen3 REQUIRED )
include_directories( EIGEN3_INCLUDE_DIR )
in CMakeLists.txt is enough.
Please refer to the INSTALL guidance text file contained in the eigen source code directory (eg. extracted from eigen-3.3.7.tar.gz downloaded from official website), which said:
Method 2. Installing using CMake
********************************
Let's call this directory 'source_dir' (where this INSTALL file is).
Before starting, create another directory which we will call 'build_dir'.
Do:
cd build_dir
cmake source_dir
make install
The "make install" step may require administrator privileges.
You can adjust the installation destination (the "prefix")
by passing the -DCMAKE_INSTALL_PREFIX=myprefix option to cmake, as is
explained in the message that cmake prints at the end.
Just like installing usual CMake projects to your machine.
The difference from directly including the /usr/include/eigne3 or /usr/local/include/eigen3 directory is that, a configuration directory called eigen3/cmake will also be installed to /usr/share or usr/local/share. This configuration directory contains the Eigen3Config.cmake file, which can automatically be found by CMake to locate the eigen3 directory.
If you have not added Eigen Library to environment variable then do the following in CMakeLists.txt:
cmake_minimum_required(VERSION 3.9)
project(ProjectName)
set(CMAKE_CXX_STANDARD 11)
# set EIGEN_DIR variable to Eigen Library Path
set(EIGEN_DIR "C:\\Eigendir\\Eigen")
# include the directory
include_directories(${EIGEN_DIR})
add_executable(ProjectName main.cpp)
target_link_libraries(ProjectName ${EIGEN_DIR})
Another simple way that doesn't require adding an environment variable is to simply find eigen with the cmake find_path function https://cmake.org/cmake/help/v3.6/command/find_path.html. The example code finds the directory "Eigen" in paths "/usr/include/" and "/usr/local/include" with an additional search in subdirectory "eigen3".
find_path(EIGEN3_INCLUDE_DIRS "Eigen" paths "/usr/include" "/usr/local/include" path_suffixes "eigen3")
message(${EIGEN3_INCLUDE_DIRS})
if ( NOT EIGEN3_INCLUDE_DIRS )
message(FATAL_ERROR "CMake variable EIGEN3_INCLUDE_DIRS not found.")
endif()
include_directories ( "${EIGEN3_INCLUDE_DIRS}" )
You could try setting the CMAKE_MODULE_PATH to the location of Eigen subdirectory named "cmake":
cmake . -DCMAKE_MODULE_PATH=<Eigen root dir>/cmake/
I had a similar problem when run cmake .. && make on Google Colab on a project clone from github. I fixed it by editing CMakeLists.txt file and adding this line to it:
set(EIGEN3_INCLUDE_DIR "/usr/include/eigen3")
As you can see, my eigen3 was in /usr/include/eigen3 path (and I had installed it using !sudo apt-get install build-essential cmake libeigen3-dev)
When installing on Kubuntu 20.04 following steps from INSTALL:
Do:
cd build_dir
cmake source_dir
make install
using -DCMAKE_INSTALL_PREFIX=/usr/local/eigen-3.4.0 and sudo
FindEigen3.cmake is not installed. However, using
set(Eigen3_DIR "/usr/local/eigen/share/eigen3/cmake")
seem to work. FindEigen3.cmake is in the build directory
but it is not copied to the installation directory tree.
This worked:
Changed c++11 to c++14
Changed find_package(Eigen3 QUIET) to find_package(Eigen3 CONFIG)
Another simple way is:
if the Eigen library is in /opt/eigen/3.3/Eigen
include_directories(/opt/eigen/3.3)
which is equivalent to what Jai suggested
set(EIGEN_DIR "/opt/eigen/3.3")
include_directories(${EIGEN_DIR})
You need to read official documentation:
Click here
After install Eigen from tar and compile it correctly.
CMakeLists.txt will looks like this:
find_package(Eigen3 3.3 REQUIRED NO_MODULE)
add_executable(${PROJECT_NAME} something.cpp)
target_link_libraries(${PROJECT_NAME} Eigen3::Eigen)
It will be fine work.