Android NDK #define Problems

android, android-ndk, c-preprocessor

Solution

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

Problem

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?

Original source