I have a large amount of c++ code needs to be run on Android.
This code compiles with Visual C++ compiler, but it doesn't compile with gcc (which is used by the Android NDK
).
The problem is that source contains a lot of pieces what generates error under gcc. Is it possible to compile the source under VC++
and make it run on Android?
Thanks.
Asked
Active
Viewed 1,310 times
0

Vishal Raj
- 1,755
- 2
- 16
- 35

Lőrincz Tamás
- 75
- 1
- 8
-
*generates error under gcc* seems like the code is using some MS extenstion(or windows library) ... then answer is no ... – Selvin Jan 13 '16 at 09:35
-
Thanks! No, It does not use any of those. The root of the errors is that the source does not follow the c++ standards, but gcc does. – Lőrincz Tamás Jan 13 '16 at 09:48
1 Answers
0
The only way to run native C/C++ code in java (and so Android) is with JNI (Java Native Interface).
This is the best tutorial to date that helps you setting JNI with Android Studio
and the Android NDK
: http://ph0b.com/new-android-studio-ndk-support/
In your case you have to extract the source code of your C++ project (meaning all .cpp ; .h ; .hpp files) and add them in the JNI folder (once you've setup your JNI environment with the aforementioned tutorial (or any other guide). Gradle will compile them as long as you properly include the library.
NB:
- You need not a Makefile : instead use the cflags in your Gradle build file ;
- Make sure to understand correctly JNI so that you create the proper header files that will link your native code with your java code ;
I hope it helps !

Mackovich
- 3,319
- 6
- 35
- 73
-
Thanks! If I use the Java NDK (as you say), then it means I use GCC. But the code does not compile under GCC. I am trying to find a workaround instead of modifying the source code to make it compile under GCC. – Lőrincz Tamás Jan 13 '16 at 09:52
-
It is true that the Android NDK uses GCC. What are the compilation error(s) ? Do you have a Makefile ? I am not very knowledgable in C++ but I can help you if it possible. – Mackovich Jan 13 '16 at 10:02
-
There are a lot of errors because of the non standard source code... Really, a lot. Just a few of them for example: [shadows](http://stackoverflow.com/questions/20875033/clang-vs-vcerror-declaration-of-t-shadows-template-parameter); **missing typename errors**; **std::exception errors**; [extra qualification](http://stackoverflow.com/questions/5642367/extra-qualification-error-in-c) And so on. I've already fixed a couple, but it seems endless. – Lőrincz Tamás Jan 13 '16 at 10:14
-
Alright so if I understand correctly, you are using Visual C++. I should have gathered that earlier. Well in your case my friend I am sorry to tell you that you have to recode everything so that it is compliant with **standard C++ code**. You will not have to rewrite everything, per se, but you will have to remove/adapt everything that is from **Visual C++**. Do not also forget to check all includes for library that are part of visual C++ framework! Then and only then, should GCC work and you may import the code in your Android Studio Project. Good luck! – Mackovich Jan 13 '16 at 10:18
-
That is what I was afraid of! Sadly I don't understand the whole code, my job is "just" to make it run on Android. I've done things like this a several times, but I've never had a problem like this, but this time the author of the original source code relied on the non standard VC++ a lot. Thanks!!! – Lőrincz Tamás Jan 13 '16 at 10:23
-
Well I certainly don't envy you. My recommandation once you are done with the "reconversion" is **do not use Gradle Experimental** if you intend or have to **use external libraries** (compiled as `.so` files for example) because it is not a supported features at the moment. It is supported in previous versions of Gradle (or even in Eclipse...), though, but you will loose the new features such as debugging and real-time auto-completion (like in any proper IDE). More details in my aforementioned guide and my SO question where I am knee-deep in trouble http://stackoverflow.com/q/32717872/3535408 – Mackovich Jan 13 '16 at 10:29