Home » Android » Android NDK #define Problems

Android NDK #define Problems

Posted by: admin June 15, 2020 Leave a comment

Questions:

When I add #define to either main.cpp or one of my headers called from main.cpp, it does not seem to be defined in other files.

For example, in main.cpp I might do something like:

#define TEST_FOO 1

Then in one of my other files, for example secondfile.cpp, TEST_FOO is ignored as if it was never defined:

#if TEST_FOO
// do something <- this never gets reached
#endif

Even if in the Android.mk file I place secondfile.cpp after main.cpp:

LOCAL_SRC_FILES := main.cpp \
                   secondfile.cpp

Is there a way to #define values in Android NDK inside the actual code?

How to&Answers:

That is correct. The compiler only knows about one source file at a time. When you compile secondfile.cpp, it has completely forgotten about anything you may have defined in main.cpp.

If you want a #define to be visible in all your source files, you need to put it in a header which is included by all your files. Or, pass it on the command line; you can do this by adding something like this to your Android.mk:

LOCAL_CPPFLAGS := -DTEST_FOO=1

Answer:

Put it in the header file and include the header file in every .c file where you want it to be defined.