New user self-registration is currently disabled. Please email eigen-core-team @ if you need an account.
Bug 1319 - With CMake, import CMake as a target instead of setting the include directories
With CMake, import CMake as a target instead of setting the include directories
Product: Eigen
Classification: Unclassified
Component: General
All All
: Normal Feature Request
Assigned To: Nobody
Depends on:
Blocks: 3.3
  Show dependency treegraph
Reported: 2016-10-02 16:57 UTC by Alain Vaucher
Modified: 2016-12-01 15:20 UTC (History)
3 users (show)

Updated FindEigen3.cmake for setting Eigen as a target (3.70 KB, text/x-cmake)
2016-10-02 16:57 UTC, Alain Vaucher
no flags Details

Description Alain Vaucher 2016-10-02 16:57:16 UTC
Created attachment 742 [details]
Updated FindEigen3.cmake for setting Eigen as a target

The status quo for employing Eigen in a CMake project is to call
followed, for instance, by
or something similar.

However, for some time, CMake has allowed for a more handy way to import dependencies, which, in the case of Eigen, would look something like
  target_link_libraries(myLibrary Eigen)

So far, there is no big difference, but the advantages appear when "myLibrary" itself is used in other CMake projects, since CMake keeps track of the dependencies and is quite intelligent when installing targets. Then, one could for instance call
  target_link_libraries(myLibrary PUBLIC Eigen)
which would automatically include the Eigen header directories to all CMake projects that employ "myLibrary".

Note that as long as "myLibrary" is used on the computer where it was compiled, the status quo allows for 
  target_include_directories(myLibrary PUBLIC ${EIGEN3_INCLUDE_DIR})
which, however, is problematic when distributing "myLibrary" to other systems, or when different Eigen versions exist or when it is updated, since the path in ${EIGEN3_INCLUDE_DIR} is then hard-coded in the exported "myLibrary", unless Eigen is distributed together with it.
On the other hand, allowing for Eigen to be a CMake target would not hard-code the location of the header files, but would just require clients to also have Eigen on their systems.

There are other advantages to this new syntax and it is being applied by many projects such as Qt or Boost.
For Eigen, allowing for this would be pretty easy. One still needs to consider whether Eigen is imported by the FindEigen3.cmake file or if it is installed first with CMake and imported with the file Eigen3Config.cmake, but in both cases it would not require too much work.

I tested a modified version of FindEigen3.cmake to implement this and I added it here as an attachment.

I am happy to discuss the topic in more detail and/or to contribute to its implementation (also with the second import possibility with Eigen3Config.cmake).

Comment 1 Gael Guennebaud 2016-11-23 10:37:13 UTC
Thanks a lot for the detailed explanation.

There is a pull-request implementing such a feature:

It would be very helpful if you could quickly review it to assess it properly cover your issue.
Comment 2 Alain Vaucher 2016-11-23 13:38:25 UTC
I just commented on the pull request.

Note You need to log in before you can comment on or make changes to this bug.