Search code examples
cmakeversioneigen

Check Eigen version in cmake with header only


I want to use Eigen in one of my projects.

The user directly decides to turn Eigen ON/OFF and configures the path to the includes. So far, CMakeLists.txt looks like:

set(EIGEN_MODULE "OFF" CACHE BOOL "Enabled EIGEN MODULE ?")
if (EIGEN_MODULE)
        include_directories(${EIGEN_INCLUDE_DIR})
        set(EIGEN_INCLUDE_DIR /usr/local CACHE PATH "eigen include dir")
        if(NOT EXISTS ${EIGEN_INCLUDE_DIR})
            message(FATAL_ERROR "Bad eigen include dir")
        endif()
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DEIGEN_MODULE")  
        include_directories(${EIGEN_INCLUDE_DIR})
endif(EIGEN_MODULE)

However, I don't know how to check the version of Eigen (I need to ensure 3.4.0 at least), knowing that I want to avoid find_package (Eigen3 3.4 REQUIRED NO_MODULE) which would require the user to compile Eigen.

Is there any way to do that ?


Solution

  • First a comment about your question: Eigen is a header only library, it means that the user will have to compile the library, no matter what.

    Then, to answer your question: you shouldn't be scared to use find_package(Eigen3), actually the documentation of Eigen specifically recommends to use find_package before performing a target_link_libraries. So you can validate that Eigen has the proper version with find_package (Eigen3 3.4 REQUIRED), this is the best way to do it. find_package will read the file Eigen3Config.cmake found in the CMAKE_PREFIX_PATH, and that will contain the proper version.

    It can seem a little confusing to use target_link_libraries to compile Eigen, since it is header-only (you could think that all you have to do is to include the directories, since Eigen is merely composed of header files, like you have done in your example). The reason is that CMake supports what is called interface library, and this is what is recommended by Eigen.