2

I am trying to create a simple test app in Android Studio with C++ support. I also need to pass some extra linker flags to the native build. In the older Android.mk build format this was fairly simple and documented. However, Android Studio now defaults to using cmake for the native libraries, and its not clear how I can specify the extra linker flags to be passed to the NDK.

The specific flag I am trying to add is -fsanitize=address in order to enable ASAN. I have been able to pass this flag to the compiler by adding it to the cppFlags property in CMake as follows. Is there a similar way to pass in a linker flag in the gradle file?

externalNativeBuild {
   cmake {
      cppFlags "-std=c++11 -frtti -fexceptions -fsanitize=address"
   }
}
user821602
  • 31
  • 1
  • 4
  • the ```cppFlags``` are passed to the compiler which I believe is ```clang++```, sending parameters to it such as linker flag would be as simple as adding it to the ```cppFlags``` property. Is that what your looking for? – ahasbini Sep 19 '17 at 22:30
  • The `cppFlags` property already has the flag I am trying to pass (`-fsanitize=address`), but that doesn't get passed to the linker. – user821602 Sep 26 '17 at 22:58

2 Answers2

3

You could enable verbose output for cmake to check what actual linker flags are used, but normally all cppFlags are passed 'as is' to the link stage.

For me, the command to produce the *.so file has this -fsanitize=address.

The standard way to pass linker-specific parameters for externalNativeBuild is with -Wl, and cFlags or cppFlags, e.g.

externalNativeBuild {
   cmake {
      cFlags "-Wl,--fix-cortex-a8"
   }
}
Alex Cohn
  • 52,705
  • 8
  • 94
  • 269
0

I think You don't need to set any ld flags for ASAN to work in the app/native code. Just run the asan_device_setup (which is in the sdk/ndk tool chain) that will push the required asan lib to device. refer section "Running" @ https://github.com/google/sanitizers/wiki/AddressSanitizerOnAndroid

KiranK
  • 1