Looking for Basic C++ NDK Example

82 views
Skip to first unread message

Danny Brain

unread,
Jun 26, 2009, 9:30:06 AM6/26/09
to android-ndk
Hey,

I'm toying with the NDK hooking into C++ code. So far I'm getting no
love. LogCat tells me the library has loaded, but the native method
cannot be found.
The (important pants of the) code I'm testing with is:
-- thing.cpp
#include <jni.h>
jstring
Java_com_package_app_Main_testPlayingWithEmptyObject( JNIEnv* env,
jobject thiz )
{
return env->NewStringUTF("Oh.. I borked !");
}

-- com.package.app.Main
static {
System.loadLibrary("thing");
}

private native String testPlayingWithEmptyObject();
Log.e("AAAA -- ", testPlayingWithEmptyObject());

Has anyone got a working example of NDK usage with C++ ?

Thanks,
Danny.

David Turner

unread,
Jun 26, 2009, 9:45:44 AM6/26/09
to andro...@googlegroups.com
I believe your native method must use extern "C" linkage, otherwise its name is being mangled in horrible ways by the compiler, and the VM can't find it at first invokation.

Danny Brain

unread,
Jun 26, 2009, 10:13:17 AM6/26/09
to android-ndk
Ah! That did the trick.. thanks heaps :)

On Jun 26, 11:45 pm, David Turner <di...@android.com> wrote:
> I believe your native method must use extern "C" linkage, otherwise its name
> is being mangled in horrible ways by the compiler, and the VM can't find it
> at first invokation.
>

Greg Copeland

unread,
Jun 26, 2009, 10:53:20 AM6/26/09
to android-ndk


On Jun 26, 9:13 am, Danny Brain <tehdan...@gmail.com> wrote:
> Ah! That did the trick.. thanks heaps :)
>

Is it safe to assume they have gutted the C++ language, in that
exceptions are still completely missing?

Greg

Jack Palevich

unread,
Jun 26, 2009, 12:31:10 PM6/26/09
to andro...@googlegroups.com
That's not a very polite way of putting it.

But yes, you are correct the NDK does not support exceptions, nor does
it provide a standard template library.

Eurico Inocêncio

unread,
Jun 26, 2009, 1:05:14 PM6/26/09
to andro...@googlegroups.com
I my view C++ exceptions and STL should be avoided in mobile/embedded
environments. Both are great for desktop/server programming, but on
mobile their cost is just too high, so the NDK approach makes perfect
sense for mobile application development.

Eurico


2009/6/26 Jack Palevich <jac...@google.com>:

Tim Hutt

unread,
Jun 26, 2009, 1:25:50 PM6/26/09
to andro...@googlegroups.com
2009/6/26 Eurico Inocêncio <eurico.i...@gmail.com>:

> I my view C++ exceptions and STL should be avoided in mobile/embedded
> environments. Both are great for desktop/server programming, but on
> mobile their cost is just too high, so the NDK approach makes perfect
> sense for mobile application development.

I don't see how they could be any more expensive than Java's containers.

You can build STLport without exceptions, and there is an Android port
of the STL-like library uSTL here:
http://zenthought.org/content/file/ustl-1-2-android-port

Greg Copeland

unread,
Jun 26, 2009, 1:19:42 PM6/26/09
to android-ndk


On Jun 26, 11:31 am, Jack Palevich <jack...@google.com> wrote:
> That's not a very polite way of putting it.
>

Dare remove exceptions from Java and Dalvik and I bet you'll have
funeral pyres for the implicated Google employees by the end of the
day. ;)

> But yes, you are correct the NDK does not support exceptions, nor does
> it provide a standard template library.
>

There are lots of alternatives to STL. STL was an afterthought for C++
anyways. Exceptions were not.

Greg

Greg Copeland

unread,
Jun 26, 2009, 1:43:12 PM6/26/09
to android-ndk
On Jun 26, 12:25 pm, Tim Hutt <tdh...@gmail.com> wrote:
> You can build STLport without exceptions, and there is an Android port
> of the STL-like library uSTL here:http://zenthought.org/content/file/ustl-1-2-android-port

Nice link. Thanks.

Greg

Greg Copeland

unread,
Jun 26, 2009, 1:36:44 PM6/26/09
to android-ndk
On Jun 26, 12:05 pm, Eurico Inocêncio <eurico.inocen...@gmail.com>
wrote:
> I my view C++ exceptions and STL should be avoided in mobile/embedded
> environments. Both are great for desktop/server programming, but on
> mobile their cost is just too high, so the NDK approach makes perfect
> sense for mobile application development.
>

Yes, you wouldn't want reliable code running in mobile/embedded
environments. That would be a bad thing. ;)

Exceptions are a fundamental implementation detail. They can
drastically improve code robustness. And they certainly set error
handling strategies. One only needs to look at some of the code
reviews for Android to realize this (locking errors). Classic issues
like locking are often, simply a no-brainer with the use of
exceptions. C++ without exceptions is a gutted language resulting in a
glorified C + objects implementation.

As a side note, I have used C++ and exceptions in embedded projects.
They do add some bloat but their benefits far, far out weighs their
memory disadvantages; so long as code quality and robustness is the
least bit important to your project. That of course assumes you have
the memory to spare. On the other hand, its common for compilers to
have a flag which prevents emitting exception code. Which means, if
you don't need, want, or use exceptions, you have that option with no
penalty. So supporting exceptions typically means supporting the best
of both worlds. Removing exceptions means gutting the language which
immediately excludes lots of existing code and/or libraries. And given
the direction the mobile market is headed, it certainly seems it was a
very short sighted decision.

I am not looking to start a C++ flame war here - but removing
exceptions is a significant gutting of the C++ language. If you
disagree, go ahead, announce to the world exceptions are to be removed
from Java because they don't have a place in mobile/embedded
environments. I promise to sing, "Oh Danny Boy", on your special
day. ;)

Greg

Eurico Inocêncio

unread,
Jun 26, 2009, 1:55:18 PM6/26/09
to andro...@googlegroups.com
>
> I don't see how they could be any more expensive than Java's containers.
>
Rite, but the reason we move to C++ is to get better performance,
staying away from Java containers and all the rest. Don't get me wrong
I have great respect for STL but in my experience the problem is big
memory consumption and code size (speed is quite good).


> You can build STLport without exceptions, and there is an Android port
> of the STL-like library uSTL here:
> http://zenthought.org/content/file/ustl-1-2-android-port
>

And as you mentioned anyone is free to use this or other STL replacement

Danny Brain

unread,
Jun 26, 2009, 8:51:30 PM6/26/09
to android-ndk
Were you able to get this compiling without modifications?
I'm getting:
sources/ustl-1.2/uexception.h:68: error: expected class-name before
'{' token
sources/ustl-1.2/uexception.h:114: error: expected class-name before
',' token
Reply all
Reply to author
Forward
0 new messages