简体   繁体   English

如何为使用线性代数的C ++模板库Eigen编写C ++项目的makefile?

[英]How to write a makefile for a C++ project which uses Eigen, the C++ template library for linear algebra?

I'm making use of Eigen library which promises vectorization of matrix operations. 我正在使用特征库,它承诺矩阵运算的矢量化。 I don't know how to use the files given in Eigen and write a makefile. 我不知道如何使用Eigen中给出的文件并编写一个makefile。 The source files which make use of Eigen include files as listed below, these are not even header files (They are just some text files)- 使用Eigen的源文件包括下面列出的文件,这些文件甚至不是头文件(它们只是一些文本文件) -

<Eigen/Core>
<Eigen/Dense>
<Eigen/Eigen>

and so on. 等等。 On Eigen's webpage, it's mentioned that, in order to use its functions I don't have to build the project, then how can I include these files in my makefile to build my project. 在Eigen的网页上,提到为了使用它的功能我不需要构建项目,那么如何在makefile中包含这些文件来构建我的项目。 My example main.c file looks like this. 我的示例main.c文件看起来像这样。 Can anyone show me how to write a makefile makefile for this file - 任何人都可以告诉我如何为这个文件编写一个makefile makefile -

#include <Eigen/Core>

// import most common Eigen types 
USING_PART_OF_NAMESPACE_EIGEN

int main(int, char *[])
{
  Matrix3f m3;
  m3 << 1, 2, 3, 4, 5, 6, 7, 8, 9;
  Matrix4f m4 = Matrix4f::Identity();
  Vector4i v4(1, 2, 3, 4);

  std::cout << "m3\n" << m3 << "\nm4:\n"
    << m4 << "\nv4:\n" << v4 << std::endl;
}

Help! 救命!

According to Eigen's website , this is a header-only library. 根据Eigen的网站 ,这是一个仅限标题的库。

This means there is nothing to compile or link it to. 这意味着没有任何东西可以编译或链接到它。 Instead, as long as you have the header files in a standard location ( /usr/local/include on *nix/Mac), then all you have to do is add that location to your preprocessor build step. 相反,只要您在标准位置( /usr/local/include on * nix / Mac)上有头文件,那么您所要做的就是将该位置添加到预处理器构建步骤中。

Assuming that you are running *nix/Mac, and assuming that you have everything installed to the default locations (eg #include <Eigen/Core> references the file /usr/local/include/Eigen/Core ), then a SUPER simple makefile would look like this: 假设您正在运行* nix / Mac,并假设您已将所有内容安装到默认位置(例如#include <Eigen/Core>引用文件/usr/local/include/Eigen/Core ),那么SUPER简单的makefile看起来像这样:

main: main.cpp
    g++ -I /usr/local/include main.cpp -o main

Which says, in English: 用英语说:

  • main depends on main.cpp main取决于main.cpp
  • to make main , use g++ to 要使用main ,请使用g++
    • compile main.cpp , 编译main.cpp
    • output file main , 输出文件main
    • looking in the directory /usr/local/include for any headers it doesn't know about 在目录/usr/local/include查找它不知道的任何头文件

NOTE: there is a TAB in front of the g++ line, NOT four spaces. 注意: g++行前面有一个TAB,而不是四个空格。

Hope that helps. 希望有所帮助。

They have this in their documentation . 他们在他们的文档中有这个。

g++ -I /path/to/eigen2/ my_program.cpp -o my_program 

There is no library to link to. 

It seems you just have to add the template(header) file path to your include directories inside your Makefile. 您似乎只需要将模板(标题)文件路径添加到Makefile中的include目录中。

Those are in fact header files. 这些实际上是头文件。 Eigen is a template library, and following common template practice includes definition and declaration all in header files, as apposed to non-template practice of keeping definitions and declarations in separate files. Eigen是一个模板库,以下常见的模板实践包括头文件中的定义和声明,与非模板实践相关,即将定义和声明保存在单独的文件中。 When decelerations and definitions are kept separate, you must build the source files containing definitions into library object files to be linked into your program. 当减速和定义保持独立时,必须将包含定义的源文件构建到要链接到程序中的库对象文件中。

This is already ostensibly done for you merely by the act of including the Eigen header files in the first place. 这表面上只是通过首先包含Eigen头文件的行为来完成的。

As long as you have installed the Eigen header files into your system include path, they will be compiled into your program without any customization on your part. 只要您已将Eigen头文件安装到系统包含路径中,它们就会被编译到您的程序中而无需您进行任何自定义。 If you have not installed them into your include path, simply provide the full path to g++ like so... 如果您还没有将它们安装到include路径中,只需提供g ++的完整路径,就像这样......

g++ -I /path/to/eigen2/ source_file -o output_file

In case you need some fortran library, here is the command I use 如果你需要一些fortran库,这是我使用的命令

g++ source.cpp -o output -I/../include -L/../lib -L/../lib64 -lcholmod -lmetis -lamd -lcamd -lccolamd -lcolamd -llapack -lgfortran -lblas g ++ source.cpp -o output -I /../ include -L /../ lib -L ​​/../ lib64 -lcholmod -lmetis -lamd -lcamd -lccolamd -lcolamd -llapack -lgfortran -lblas

I replace the actual path with .. 我用..替换实际路径

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM