包含 Eigen 时的多个定义

Multiple definitions when including Eigen

我刚开始使用 Eigen,并且 运行 遇到了多重定义问题。我已经找到了确切的问题,这里是一个最小的例子。

作品:

// File common1.hpp
#include <Eigen/Dense>
// File common2.hpp
#include "common1.hpp"
// File main.cpp
#include "common2.hpp"

clang++ main.cpp 用这个编译。

无效:

// File common1.hpp
#include <Eigen/Dense>
// File common2.hpp
#include "common1.hpp"
// File common2.cpp
#include "common2.hpp"
// File main.cpp
#include "common2.hpp"

clang++ main.cpp common2.cpp 失败并出现错误:

/tmp/common2-f75caf.o: In function `bool Eigen::numext::equal_strict<double, double>(double const&, double const&)':
common2.cpp:(.text+0x30): multiple definition of `bool Eigen::numext::equal_strict<double, double>(double const&, double const&)'
/tmp/main-8e7100.o:main.cpp:(.text+0x30): first defined here
/tmp/common2-f75caf.o: In function `bool Eigen::numext::equal_strict<float, float>(float const&, float const&)':
common2.cpp:(.text+0x0): multiple definition of `bool Eigen::numext::equal_strict<float, float>(float const&, float const&)'
/tmp/main-8e7100.o:main.cpp:(.text+0x0): first defined here
/tmp/common2-f75caf.o: In function `bool Eigen::numext::not_equal_strict<double, double>(double const&, double const&)':
common2.cpp:(.text+0x90): multiple definition of `bool Eigen::numext::not_equal_strict<double, double>(double const&, double const&)'
/tmp/main-8e7100.o:main.cpp:(.text+0x90): first defined here
/tmp/common2-f75caf.o: In function `bool Eigen::numext::not_equal_strict<float, float>(float const&, float const&)':
common2.cpp:(.text+0x60): multiple definition of `bool Eigen::numext::not_equal_strict<float, float>(float const&, float const&)'
/tmp/main-8e7100.o:main.cpp:(.text+0x60): first defined here
clang-7.0: error: linker command failed with exit code 1 (use -v to see invocation)

我非常喜欢 Eigen 的 C++ API,我真的很想继续使用它。我看过一些使用 Eigen 的项目,它们使用自己的命名空间来执行第二个代码块。我试过了,但得到了同样的错误。任何帮助将不胜感激。

编辑: 铿锵版本 7。 本征:在过去一个月左右从 Github 镜像中提取。

这是 https://github.com/eigenteam/eigen-git-mirror/commit/11a3c93ee327dd2be34bc56bc04a5ebcb3340256 的结果,其中错误中的相关函数尚未内联。

这通常可以通过使用 Eigen 的最新版本(在撰写本文时为 3.3.4)来解决,或者更具体地说,通过使用 https://github.com/eigenteam/eigen-git-mirror/commit/37ca6e8ee578bf490b04f4e977c4ecb081d35e9f

或之后的代码来解决

在 Bitbucket 上,37ca6e 是 https://bitbucket.org/eigen/eigen/commits/1f08827edccbe08648146cc584fcbd7f4bfb3e33

两个测试用例现在都可以工作,感谢所有帮助find/fix这个的人。