Rajveer
Rajveer

Reputation: 857

Visual Studio 2015, Android NDK with libc++ and cmath issues

When creating a native Android NDK project with the new Visual Studio 2015 Preview and changing the STL to "LLVM libc++ static library (c++_static)", simply including <cmath> in the default "main.cpp" file and compiling with the default Clang 3.4 causes the following issues:

C:\Program Files (x86)\Microsoft Visual Studio 14.0\Apps\android-ndk-r10\sources\cxx-stl\llvm-libc++\libcxx\include\cmath(652,8): error : no member named 'float_t' in the global namespace
1>  using ::float_t;
1>        ~~^
1>C:\Program Files (x86)\Microsoft Visual Studio 14.0\Apps\android-ndk-r10\sources\cxx-stl\llvm-libc++\libcxx\include\cmath(653,8): error : no member named 'double_t' in the global namespace
1>  using ::double_t;
1>        ~~^
1>C:\Program Files (x86)\Microsoft Visual Studio 14.0\Apps\android-ndk-r10\sources\cxx-stl\llvm-libc++\libcxx\include\cmath(680,85): error : use of undeclared identifier 'acosl'
1>  inline _LIBCPP_INLINE_VISIBILITY long double acos(long double __x) _NOEXCEPT {return acosl(__x);}

...and more.

This never caused an issue when using Eclipse, so I'm not sure what I'm doing wrong or if there is an issue with the default project?

EDIT: For now I've added a bugfix ticket to Microsoft Connect:

https://connect.microsoft.com/VisualStudio/feedback/details/1031464/compiling-ndk-project-when-including-cmath-causes-issues-with-clang-and-libc

Upvotes: 1

Views: 4242

Answers (2)

Rajveer
Rajveer

Reputation: 857

The reason for this is that the <math.h> that exists for android platforms 19 and below has these typedefs enclosed by an

#if 0
#endif

block. For these platforms, these extra defines are in

$(VS_NdkRoot)/sources/android/support/include

Simply including this folder in Visual Studio's project settings leads to a successful compile.

As an aside, the <math.h> file for platform 21 in the newer NDK-10c looks fixed (so you don't need to include the support folder), however the Visual Studio 2015 Preview installs version 10. When replacing the installed version 10 with 10c, we then get the option in the project explorer to target platform 21 (although officially unsupported). This compiles fine, and although it targets platform 21, should be ok until this issue is resolved.

Here is the relevant Android bug report (it's actually a Visual Studio problem as it should include this folder in the default project):

https://code.google.com/p/android/issues/detail?id=79890&thanks=79890&ts=1416335194

EDIT: Changed old NDKRoot macro with newer VS_NdkRoot macro.

Upvotes: 4

joshcomley
joshcomley

Reputation: 28818

I had issues using cos and sin etc. with the Android NDK and Visual Studio 2015. The solution was to force include the math library in the build for Android in the linker project settings for the Android project:

enter image description here

Upvotes: 3

Related Questions