I just started using Eigen and I've run into a multiple definition issue. I've tracked down the precise issue, and here's a minimal example.
Works:
// File common1.hpp
#include <Eigen/Dense>
// File common2.hpp
#include "common1.hpp"
// File main.cpp
#include "common2.hpp"
clang++ main.cpp
compiles with this.
Doesn't work:
// 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
fails with this with the error:
/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)
I really like Eigen's C++ API, and I'd really like to keep using it. I've looked at a few projects that use Eigen, and they do the second block of code using their own namespaces. I tried that, but got the same error. Any help would be appreciated.
Edit: Clang version 7. Eigen: Pulled from Github mirror in the past month or so.