0

I enabled dynamic_cast feature on android clang++ compiler by-frtti flag, but dynamic_cast always returns NULL. I tested on Linux with g++ compiler but works fine.

Here is the source code on Linux with g++ compiler:

#include <iostream>

using namespace std;

class A {
public:
  virtual ~A() {}
};

template <typename T>
class B :
  public A {
  T num;
};

int main()
{
  A *a1 = new B<int>();
  A *a2 = new B<float>();

  B<int> *b1 = dynamic_cast<B<int> *>(a1);
  B<int> *b2 = dynamic_cast<B<int> *>(a2);
  cout<<"b1="<<b1<<"  b2="<<b2<<endl;

  delete a1;
  delete a2;

  return 0;
}

The output will be:

b1=0x3581ed0  b2=0

I made little modification to replace "std::cout" with LOGE on Android. Here it is:

#include <iostream>

using namespace std;

class A {
public:
  virtual ~A() {}
};

template <typename T>
class B :
  public A {
  T num;
};

int ThreadT::callFunc() // This is a function will be called in Android app
{
  A *a1 = new B<int>();
  A *a2 = new B<float>();

  B<int> *b1 = dynamic_cast<B<int> *>(a1);
  B<int> *b2 = dynamic_cast<B<int> *>(a2);
  //cout<<"b1="<<b1<<"  b2="<<b2<<endl;
  LOGE("b1=%p b2=%p", b1, b2);

  delete a1;
  delete a2;

  return 0;
}

The output will be always like this:

b1=0 b2=0

I think this should have relationship with clang++ flags, here I listed them all,

-c    -fno-exceptions -Wno-multichar -msoft-float -ffunction-sections
-fdata-sections -funwind-tables -fstack-protector-strong -Wa,--noexecstack
-Werror=format-security -D_FORTIFY_SOURCE=2 -fno-short-enums -no-canonical-prefixes 
-mcpu=cortex-a7 -D__ARM_FEATURE_LPAE=1 -mfloat-abi=softfp -mfpu=neon -DANDROID 
-fmessage-length=0 -W -Wall -Wno-unused -Winit-self -Wpointer-arith -Werror=return-type 
-Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Werror=date-time 
-DNDEBUG -g -Wstrict-aliasing=2 -DNDEBUG -UDEBUG  -D__compiler_offsetof=__builtin_offsetof 
-Werror=int-conversion -Wno-reserved-id-macro -Wno-format-pedantic 
-Wno-unused-command-line-argument -fcolor-diagnostics -nostdlibinc  
-target arm-linux-androideabi    -target arm-linux-androideabi 
-Bprebuilts/gcc/linux-x86/arm/arm-linux-androideabi-4.9/arm-linux-androideabi/bin    
-fvisibility-inlines-hidden -Wsign-promo  -Wno-inconsistent-missing-override -nostdlibinc  
-target arm-linux-androideabi   -mthumb -Os -fomit-frame-pointer -fno-strict-aliasing  
-fno-rtti -std=c++11 -frtti -Wall -Wextra -Werror -DENABLE_LOGGER -DBUILD_ANDROID_AP 
-DMEMORY_POOL_MODE -fPIC -D_USING_LIBCXX -std=gnu++14  -Werror=int-to-pointer-cast 
-Werror=pointer-to-int-cast  -Werror=address-of-temporary -Werror=null-dereference 
-Werror=return-type    -MD -MF

Note1: there is -fno-rtti flag by default, which will cause compile failure, I enabled it with -frtti after default one.

Note2: There are many flags passed to Android clang++, but they are out of my control, it's added by Android or Qualcomm, I only added following flags,

-std=c++11 -frtti -Wall -Wextra -Werror -DENABLE_LOGGER -DBUILD_ANDROID_AP -DMEMORY_POOL_MODE

Thank you, your kind help is appreciated.

0 Answers0