5

I have the following test program using eigen:

#include <iostream>
#include <Eigen/Dense>

using namespace std;
using Eigen::MatrixXd;

int main() {
  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);
  cout << m << endl;
}

and I can compile it with g++ -I/usr/include/eigen3/ test1.cpp.

However, the compile command doesn't work if I don't specify the include flag. This seems strange to me because I thought that any headers under /usr/include will be picked up automatically by the compiler (e.g. Boost headers, also located under /usr/include, work perfectly fine without having to tell the compiler where to look for them). What changes do I need to make to the eigen setup so I don't have to specify the -I flag in the compile command?

4

1 回答 1

11

如果你更换

#include <Eigen/Dense>

经过

#include <eigen3/Eigen/Dense>

您的代码将编译。换句话说,问题在于您包含<Eigen/Dense>了目录中的内容/usr/include/eigen3,但编译器/usr/include默认只搜索。

我建议使用 include 标志,而不是 include <eigen3/Eigen/Dense>,因为这在发行版、操作系统等之间更具可移植性,而且通常更容易为其他环境配置编译。Eigen3 带有 pkg​​-config 文件,非常易于使用和移植。编译

g++ $(pkg-config --cflags eigen3) test1.cpp

将适用于 pkg-config 可用的所有平台,如果您想避免硬编码的包含路径,它对您来说是一个很好的选择。

于 2014-02-24T10:29:18.497 回答