The project needs to unpack the Eigen archive and set the include directories for the target accordingly:
- Let us first declare a C++11 project:
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
project(recipe-01 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
- We add a custom target to our build system. The custom target will extract the archive inside the build directory:
add_custom_target(unpack-eigen
ALL
COMMAND
${CMAKE_COMMAND} -E tar xzf ${CMAKE_CURRENT_SOURCE_DIR}/eigen-eigen-5a0156e40feb.tar.gz
COMMAND
${CMAKE_COMMAND} -E rename eigen-eigen-5a0156e40feb eigen-3.3.4
WORKING_DIRECTORY
${CMAKE_CURRENT_BINARY_DIR}
COMMENT
"Unpacking Eigen3 in ${CMAKE_CURRENT_BINARY_DIR}/eigen-3.3.4"
)
- We add an executable target for our source file:
add_executable(linear-algebra linear-algebra.cpp)
- Since the compilation of our source file depends on the Eigen header files, we need to explicitly specify the dependency of the executable target on the custom target:
add_dependencies(linear-algebra unpack-eigen)
- Finally, we can specify which include directories we need to compile our source file:
target_include_directories(linear-algebra
PRIVATE
${CMAKE_CURRENT_BINARY_DIR}/eigen-3.3.4
)