[eigen] visibility of classes/dynamic_cast |
[ Thread Index |
Date Index
| More lists.tuxfamily.org/eigen Archives
]
Hi
I just discovered a rather nasty problem which affects both eigen2 and
eigen3.
When I compile my code using -fvisiblilty=hidden (which is recommended
nowadays on linux), symbols which aren't marked as visible are hidden.
This of course means, Eigen types are hidden.
Now lets say we have 2 dso's, and I have a class like this:
template<typename T>
class MyVector: public AbstractData
{
const std::type_info& getType() const { return typeid(T); }
};
I instantiate this template class with Eigen::Vector3f in both dso's.
Lets look at the following use case:
- dso 1 creates a MyVector<Eigen::Vector3f> *vector = new
MyVector<Eigen::Vector3f>();
- dso 2 gets the pointer, but in form of an AbstractData pointer. It
will do this now:
if(ptr->getType() == typeid(Eigen::Vector3f))
cout << "Halleluja" << endl;
The comparison here will fail in dso2 and I won't output anything. Why
is that so?
The templates get instantiated in each dso, and therewith the typeinfo
objects, which are hidden. Since gcc-3, typeinfo objects are compared by
address for speed reasons (see here: http://gcc.gnu.org/faq.html#dso).
The comparison of course fails because the type_info objects are
different. The solution is to export the template classes using
__attribute__((visibility("default"))).
the stl implementation of gcc solves this by doing that:
_GLIBCXX_BEGIN_NAMESPACE(std)
which expands to
namespace std __attribute__ ((__visibility__ ("default"))) {
I have (on a rather older release, a built-in copy of Eigen in our
software) tested something similar. I attached the diff for a sample
implementation and it solved my issues.
For a comparison, stl always exports the symbols to make sure things
always work. In my implementation let it up to the user to decide that
as this use case is rather rare. But may be you should throw in your
weight here
What's your opinion on this?
Cheers
Benjamin
diff -r 6951f877ddcf external/eigen2/Eigen/Array
--- a/external/eigen2/Eigen/Array Thu Jun 24 18:26:33 2010 +0200
+++ b/external/eigen2/Eigen/Array Fri Jun 25 11:03:17 2010 +0200
@@ -5,7 +5,7 @@
#include "src/Core/util/DisableMSVCWarnings.h"
-namespace Eigen {
+EIGEN_BEGIN_NAMESPACE(Eigen)
/** \defgroup Array_Module Array module
* This module provides several handy features to manipulate matrices as simple array of values.
@@ -32,7 +32,7 @@
#include "src/Array/Random.h"
#include "src/Array/Norms.h"
-} // namespace Eigen
+EIGEN_END_NAMESPACE
#include "src/Core/util/EnableMSVCWarnings.h"
diff -r 6951f877ddcf external/eigen2/Eigen/Cholesky
--- a/external/eigen2/Eigen/Cholesky Thu Jun 24 18:26:33 2010 +0200
+++ b/external/eigen2/Eigen/Cholesky Fri Jun 25 11:03:17 2010 +0200
@@ -14,7 +14,7 @@
#undef EIGEN_HIDE_HEAVY_CODE
#endif
-namespace Eigen {
+EIGEN_BEGIN_NAMESPACE(Eigen)
/** \defgroup Cholesky_Module Cholesky module
*
@@ -35,7 +35,7 @@
#include "src/Cholesky/LLT.h"
#include "src/Cholesky/LDLT.h"
-} // namespace Eigen
+EIGEN_END_NAMESPACE
#define EIGEN_CHOLESKY_MODULE_INSTANTIATE_TYPE(MATRIXTYPE,PREFIX) \
PREFIX template class LLT<MATRIXTYPE>; \
@@ -55,9 +55,9 @@
#ifdef EIGEN_EXTERN_INSTANTIATIONS
-namespace Eigen {
+EIGEN_BEGIN_NAMESPACE(Eigen)
EIGEN_CHOLESKY_MODULE_INSTANTIATE(extern);
-} // namespace Eigen
+EIGEN_END_NAMESPACE
#endif
#include "src/Core/util/EnableMSVCWarnings.h"
diff -r 6951f877ddcf external/eigen2/Eigen/Core
--- a/external/eigen2/Eigen/Core Thu Jun 24 18:26:33 2010 +0200
+++ b/external/eigen2/Eigen/Core Fri Jun 25 11:03:17 2010 +0200
@@ -4,6 +4,19 @@
// first thing Eigen does: prevent MSVC from committing suicide
#include "src/Core/util/DisableMSVCWarnings.h"
+
+#ifdef __GNUC__
+ #ifdef EIGEN_GCC_VISIBILITY_DEFAULT
+ #define EIGEN_BEGIN_NAMESPACE(name) namespace name __attribute__((visibility("default"))) {
+ #else
+ #define EIGEN_BEGIN_NAMESPACE(name) namespace name {
+ #endif
+ #define EIGEN_END_NAMESPACE }
+#else
+ #define EIGEN_BEGIN_NAMESPACE(name) namespace name {
+ #define EIGEN_END_NAMESPACE }
+#endif
+
#ifdef _MSC_VER
#include <malloc.h> // for _aligned_malloc -- need it regardless of whether vectorization is enabled
#if (_MSC_VER >= 1500) // 2008 or later
@@ -75,7 +88,7 @@
#error The preprocessor symbols 'min' or 'max' are defined. If you are compiling on Windows, do #define NOMINMAX to prevent windows.h from defining these symbols.
#endif
-namespace Eigen {
+EIGEN_BEGIN_NAMESPACE(Eigen)
/** \defgroup Core_Module Core module
* This is the main module of Eigen providing dense matrix and vector support
@@ -147,7 +160,7 @@
#include "src/Core/Part.h"
#include "src/Core/CacheFriendlyProduct.h"
-} // namespace Eigen
+EIGEN_END_NAMESPACE
#include "src/Core/util/EnableMSVCWarnings.h"
diff -r 6951f877ddcf external/eigen2/Eigen/Geometry
--- a/external/eigen2/Eigen/Geometry Thu Jun 24 18:26:33 2010 +0200
+++ b/external/eigen2/Eigen/Geometry Fri Jun 25 11:03:17 2010 +0200
@@ -12,7 +12,7 @@
#define M_PI 3.14159265358979323846
#endif
-namespace Eigen {
+EIGEN_BEGIN_NAMESPACE(Eigen)
/** \defgroup Geometry_Module Geometry module
*
@@ -44,7 +44,7 @@
#include "src/Geometry/ParametrizedLine.h"
#include "src/Geometry/AlignedBox.h"
-} // namespace Eigen
+EIGEN_END_NAMESPACE
#include "src/Core/util/EnableMSVCWarnings.h"
diff -r 6951f877ddcf external/eigen2/Eigen/LU
--- a/external/eigen2/Eigen/LU Thu Jun 24 18:26:33 2010 +0200
+++ b/external/eigen2/Eigen/LU Fri Jun 25 11:03:17 2010 +0200
@@ -5,7 +5,7 @@
#include "src/Core/util/DisableMSVCWarnings.h"
-namespace Eigen {
+EIGEN_BEGIN_NAMESPACE(Eigen)
/** \defgroup LU_Module LU module
* This module includes %LU decomposition and related notions such as matrix inversion and determinant.
@@ -22,7 +22,7 @@
#include "src/LU/Determinant.h"
#include "src/LU/Inverse.h"
-} // namespace Eigen
+EIGEN_END_NAMESPACE
#include "src/Core/util/EnableMSVCWarnings.h"
diff -r 6951f877ddcf external/eigen2/Eigen/LeastSquares
--- a/external/eigen2/Eigen/LeastSquares Thu Jun 24 18:26:33 2010 +0200
+++ b/external/eigen2/Eigen/LeastSquares Fri Jun 25 11:03:17 2010 +0200
@@ -8,7 +8,7 @@
#include "QR"
#include "Geometry"
-namespace Eigen {
+EIGEN_BEGIN_NAMESPACE(Eigen)
/** \defgroup LeastSquares_Module LeastSquares module
* This module provides linear regression and related features.
@@ -20,7 +20,7 @@
#include "src/LeastSquares/LeastSquares.h"
-} // namespace Eigen
+EIGEN_END_NAMESPACE
#include "src/Core/util/EnableMSVCWarnings.h"
diff -r 6951f877ddcf external/eigen2/Eigen/NewStdVector
--- a/external/eigen2/Eigen/NewStdVector Thu Jun 24 18:26:33 2010 +0200
+++ b/external/eigen2/Eigen/NewStdVector Fri Jun 25 11:03:17 2010 +0200
@@ -29,7 +29,7 @@
#include "Core"
#include <vector>
-namespace Eigen {
+EIGEN_BEGIN_NAMESPACE(Eigen)
// This one is needed to prevent reimplementing the whole std::vector.
template <class T>
@@ -85,7 +85,7 @@
#endif
-}
+EIGEN_END_NAMESPACE
namespace std {
diff -r 6951f877ddcf external/eigen2/Eigen/QR
--- a/external/eigen2/Eigen/QR Thu Jun 24 18:26:33 2010 +0200
+++ b/external/eigen2/Eigen/QR Fri Jun 25 11:03:17 2010 +0200
@@ -16,7 +16,7 @@
#undef EIGEN_HIDE_HEAVY_CODE
#endif
-namespace Eigen {
+EIGEN_BEGIN_NAMESPACE(Eigen)
/** \defgroup QR_Module QR module
*
@@ -66,7 +66,7 @@
EIGEN_QR_MODULE_INSTANTIATE(extern);
#endif // EIGEN_EXTERN_INSTANTIATIONS
-} // namespace Eigen
+EIGEN_END_NAMESPACE
#include "src/Core/util/EnableMSVCWarnings.h"
diff -r 6951f877ddcf external/eigen2/Eigen/SVD
--- a/external/eigen2/Eigen/SVD Thu Jun 24 18:26:33 2010 +0200
+++ b/external/eigen2/Eigen/SVD Fri Jun 25 11:03:17 2010 +0200
@@ -5,7 +5,7 @@
#include "src/Core/util/DisableMSVCWarnings.h"
-namespace Eigen {
+EIGEN_BEGIN_NAMESPACE(Eigen)
/** \defgroup SVD_Module SVD module
*
@@ -22,7 +22,7 @@
#include "src/SVD/SVD.h"
-} // namespace Eigen
+EIGEN_END_NAMESPACE
#include "src/Core/util/EnableMSVCWarnings.h"
diff -r 6951f877ddcf external/eigen2/Eigen/Sparse
--- a/external/eigen2/Eigen/Sparse Thu Jun 24 18:26:33 2010 +0200
+++ b/external/eigen2/Eigen/Sparse Fri Jun 25 11:03:17 2010 +0200
@@ -69,7 +69,7 @@
#include "umfpack.h"
#endif
-namespace Eigen {
+EIGEN_BEGIN_NAMESPACE(Eigen)
/** \defgroup Sparse_Module Sparse module
*
@@ -125,7 +125,7 @@
# include "src/Sparse/UmfPackSupport.h"
#endif
-} // namespace Eigen
+EIGEN_END_NAMESPACE
#include "src/Core/util/EnableMSVCWarnings.h"
diff -r 6951f877ddcf external/eigen2/Eigen/StdVector
--- a/external/eigen2/Eigen/StdVector Thu Jun 24 18:26:33 2010 +0200
+++ b/external/eigen2/Eigen/StdVector Fri Jun 25 11:03:17 2010 +0200
@@ -21,7 +21,7 @@
#include <vector>
#undef vector
-namespace Eigen {
+EIGEN_BEGIN_NAMESPACE(Eigen)
template<typename T> class aligned_allocator;
@@ -67,7 +67,7 @@
#endif
-}
+EIGEN_END_NAMESPACE
namespace std {