Reputation: 125
Is there a way to use nested functions on the the Android NDK? It should be possible, since the NDK uses gcc right?
Well I tried defining a function inside main, and the compiler simply doesn't recognize it. Here's the error
error: a function-definition is not allowed here before '{' token error: expected '}' at end of input
Upvotes: 2
Views: 594
Reputation: 1
Nested functions require an executable stack, which is disabled by default in Android.
It took me an unbelievable amount of time to figure it out - but you need to add the following to your CFLAGS:
-z execstack -Wa,--execstack
All the best!
Upvotes: 0
Reputation: 999
The nested functions example from here, where a foo()
contains a square()
, compiles and runs fine (on a Tegra 3). I use the NDK r8 android toolchain (with android-cmake, if that makes any difference, which shouldn't be the case). Maybe you should try with r8 in case you use an older version?
Upvotes: 1
Reputation: 11277
I don't know exactly. But as far as NDK uses arm
gcc compiler and that nested functions may be broken on ARM architecture - my best guess is that nested functions may be disabled in NDK toolset. In any way - nested functions is GCC extension, so if you want your code to be portable you better don't use gcc extensions at all and use C89/C90 compatible code instead.
Upvotes: 1
Reputation: 14633
Yes, it should be possible. You may need to add the -fnested-functions flag to gcc calls.
Upvotes: 0