Installing Eigen with Cmake FetchContent
### Describe the feature you would like to be implemented.
In the tutorial there isn't a way to leverage newer CMake features to include Eigen, I would like to use FetchContent_Declare to automatically install Eigen for me through CMake.
### Would such a feature be useful for other users? Why?
- Lowers barrier to entry, copying and pasting a few lines into an existing CMake project is simpler than going through the process of installing something
- Prevents different versions of eigen on different machines problem -- one person has version X, another has Y, and the project builds on one machine but not the other, burning valuable developer time just to find out it's an eigen version problem-- this feature will always compile from a SHA or git tag that is checked into the git repo, so no one is out of date
- Allows the user to side step the need for admin privileges
- Allows for different versions of eigen to be used on the same machine if needed by having different project use it
### Any hints on how to implement the requested feature?
I actually have an MVP of how this might be done in the next section
### Additional resources
Make a folder and put the following three files in it: CMakeLists.txt, fastImageProcessing.txt, compile.sh
# compile.sh
```
#!/bin/bash
# To run: source compile.sh
mkdir -p CMakeFiles
cd CMakeFiles
cmake ../
make -j16
cd ..
```
# CMakeLists.txt
```
cmake_minimum_required(VERSION 3.18)
project(sci CXX)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # creates compile_commands.json, which the linter needs
include(FetchContent)
FetchContent_Declare(Eigen3 GIT_REPOSITORY "https://gitlab.com/libeigen/eigen.git" GIT_TAG "3.4.0")
FetchContent_MakeAvailable(Eigen3)
add_executable(fastImageProcessing fastImageProcessing.cpp)
target_link_libraries( fastImageProcessing Eigen3::Eigen)
```
# fastImageProcessing.cpp
```
#include <vector>
#include <iostream>
#include <Eigen/Dense>
template<typename T>//!< This function creates a quick truth image that is a grayscale image that starts at 0 in the upper left and scales to 256 at the lower right
T truth_eigen(size_t rows, size_t cols){
if(rows % 2){
throw std::runtime_error("[generate_image][truth_eigen] rows variable is not divisible by 2");
}
if(cols % 2){
throw std::runtime_error("[generate_image][truoh_eigen] cols variable is not divisible by 2");
}
T mat(rows, cols);
float horizontal_slope{127.0f/float(cols)};
float vertical_slope{127.0f/float(rows)};
for(int row = 0; row < rows; ++row){
for(int col = 0; col < cols; ++col){
mat(row, col) = float(row) * vertical_slope + float(col) * horizontal_slope;
}
}
return mat;
}
int main(){
int rows = 400;
int cols = 400;
auto mat_eigen{truth_eigen<Eigen::MatrixXd>(rows, cols)};
std::cout<<"End of processing"<<std::endl;
return 0;
}
```
## What I would like to see in the installation tutorial:
If your project already uses CMake V3.14+, you can copy paste the following lines into your project root CMakeLists.txt:
```
include(FetchContent)
FetchContent_Declare(Eigen3 GIT_REPOSITORY "https://gitlab.com/libeigen/eigen.git" GIT_TAG "3.4.0")
FetchContent_MakeAvailable(Eigen3)
```
And for the files that will actually be using Eigen, in those file's CMakeList.txt:
```
target_link_libraries( yourFile Eigen3::Eigen)
```
And to include an Eigen Library such as Eigen Dense:
```
#include <Eigen/Dense>
```
issue