开发者

How to add a custom MACRO when using NDK?

I n开发者_开发知识库eed some custom MACROs in my NDK projects however I have no ideal how to add and I found no answers Could anyone please help? Many thanks!


If you would like to add a special definition when compiling your NDK code (jni) add the following into your Android.mk:

LOCAL_CFLAGS    := -DMYDEFINE

This will define the Macro MYDEFINE in your c/c++ code. Here an example

#ifdef MYDEFINE
// We build the project with this special macro
#else
// We build without setting the macro in the LOCAL_CFLAGS
#endif


If you understand you correctly, you need to look at how the C Preprocessor works.

Do you have any experience in writing C/C++-programs? First off, you should be really picky on what parts of your application you write in native code. Also, you need to keep in mind that the native code will behave totally different when it comes to memory allocation and such, so this really isn't the place for guesswork.

Spend some time getting to know C/C++, design your application wisely so that the parts you want to write in native code is clearly defined, and that it doesn't increase the chance of your application crashing. I'm guessing most projects can do without an native application part, so if you're using it, and don't know exactly what you're doing, you should really re-asses whether it's the right thing to do or not.


If you're talking about android makefile MACROs like

$(call my-dir)

It's exactly same as GNU makefile syntax

Gnu makefile manual

For instance:

reverse = $(2) $(1)

foo = $(call reverse,a,b)

$(warning $(foo))

results:

b a


To define marcro:

LOCAL_CFLAGS += -DYOUR_MACRO

to define macro with value:

LOCAL_CFLAGS += -DYOUR_MACRO='"string"'

or

LOCAL_CFLAGS += -DYOUR_MACRO=123

personally, I suggest to use += instead of := to avoid LOCAL_CFLAGS been overwritten.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜