문제

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

도움이 되었습니까?

해결책

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.

다른 팁

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?

Yes, it should be possible. You may need to add the -fnested-functions flag to gcc calls.

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!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top