I am trying to build a project using Eigen3 library of headers. My repo has the following structure
Eigen
Dense
...
pricing
CMakeLists.txt
options.cpp
options.h
CMakeLists.txt
main.cpp
main.cpp:
#include "pricing/options.h"
int main()
{
fun();
}
CMakeLists.cpp:
cmake_minimum_required(VERSION 3.22.0)
project(my_application VERSION 1.0.0 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_executable(quant_finance_app
main.cpp
)
add_subdirectory(pricing)
target_link_libraries(quant_finance_app pricing_lib)
pricing/options.h:
#pragma once
#include <iostream>
#include <Eigen/Dense>
using Eigen::MatrixXd;
void fun();
pricing/options.cpp:
#include "options.h"
void fun()
{
MatrixXd m(2,2);
m(0,0) = 3;
m(1,0) = 2.5;
m(0,1) = -1;
m(1,1) = m(1,0) + m(0,1);
std::cout << m << std::endl;
}
pricing/CMakeLists.txt:
add_library(pricing_lib
options.cpp)
target_include_directories(pricing_lib PRIVATE ../)
I run CMake and I get the following error when compiling "options.cpp" file:
fatal error: Eigen/Dense: No such file or directory
4 | #include <Eigen/Dense>
Why can't the compiler find the included headers necessary to create "pricing_lib" library?
Solution
The solution is to rewrite the file "pricing/CMakeLists.txt" as follows:
add_library(pricing_lib
options.cpp)
target_include_directories(pricing_lib PUBLIC ../)
Note that the only difference is that instead of PRIVATE I am using the keyword PUBLIC.