D
Dan Caugherty
Hey all --
I think I have a legitimate reason for using RTTI, particularly
dynamic_cast<>, and I'd like to use it to query instances created in a
shared library. No matter how I tweak GCC's visibility criteria with
#pragmas or linker flags, dynamic_cast always fails.
Here's a very brief idea about what I'm trying to do:
----------
// any_value.h
class any_value {
public:
any_value(); //defined in any_value.cpp
virtual ~any_value(); //also defined in any_value.cpp
};
----------
// value.h
template <typename T>
class value {
T t_;
public:
value(); //defined in value_def.h (t_ = T
())
value(const T & t); //defined in value_def.h
~value(); //defined in value_def.h
const T & item() const; //defined in value_def.h (never virtual)
};
----------- Only .cpp file used for shared lib:
// value.cpp
#include <typeinfo>
#include "value.h"
#include "value_def.h" //only include for value_def.h *anywhere*
template class value<int>;
-------------- Only .cpp file for test executable:
// my_exec.cpp
// .. linked with shared library libvalue.dylib (yes, this is on MacOS
X)
#include <typeinfo>
#include <iostream>
#include "value.h"
int main(int argc, char ** argv)
{
any_value * pa = new value<int>(42);
std::cout << "Dyn cast returns "
<< dynamic_cast< value<int> * >(pa) << std::endl;
return 0;
}
-------------------- END
The test executable always prints "Dyn cast returns 0".
Any ideas as to what I'm doing wrong? I make it a point to export all
symbols in the shared library. I think this has something to do with
the type_info objects created by GCC (multiple versions perhaps that
cause dynamic_cast to fail), but I don't know how to verify that. (In
any case, I'm not sure how I'd fix it!)
Thanks in advance,
-- Dan C.
I think I have a legitimate reason for using RTTI, particularly
dynamic_cast<>, and I'd like to use it to query instances created in a
shared library. No matter how I tweak GCC's visibility criteria with
#pragmas or linker flags, dynamic_cast always fails.
Here's a very brief idea about what I'm trying to do:
----------
// any_value.h
class any_value {
public:
any_value(); //defined in any_value.cpp
virtual ~any_value(); //also defined in any_value.cpp
};
----------
// value.h
template <typename T>
class value {
T t_;
public:
value(); //defined in value_def.h (t_ = T
())
value(const T & t); //defined in value_def.h
~value(); //defined in value_def.h
const T & item() const; //defined in value_def.h (never virtual)
};
----------- Only .cpp file used for shared lib:
// value.cpp
#include <typeinfo>
#include "value.h"
#include "value_def.h" //only include for value_def.h *anywhere*
template class value<int>;
-------------- Only .cpp file for test executable:
// my_exec.cpp
// .. linked with shared library libvalue.dylib (yes, this is on MacOS
X)
#include <typeinfo>
#include <iostream>
#include "value.h"
int main(int argc, char ** argv)
{
any_value * pa = new value<int>(42);
std::cout << "Dyn cast returns "
<< dynamic_cast< value<int> * >(pa) << std::endl;
return 0;
}
-------------------- END
The test executable always prints "Dyn cast returns 0".
Any ideas as to what I'm doing wrong? I make it a point to export all
symbols in the shared library. I think this has something to do with
the type_info objects created by GCC (multiple versions perhaps that
cause dynamic_cast to fail), but I don't know how to verify that. (In
any case, I'm not sure how I'd fix it!)
Thanks in advance,
-- Dan C.